开发者

how I can use sparql to query from my own rdf file using dotNetRDF library?

I use dotNetRDF library to write sparql queries. I Define a remote endpoint using "http://dbpedia.org/sparql" as DBPedia SPARQL endpoint and "http://dbpedia.org" as Default Graph URI:

 SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sp开发者_C百科arql"), "http://dbpedia.org");

this works well But I need to use my rdf file as Default Graph URI "myuniversity.rdf" I added it to the web site what is the parameter will be instead of "http://dbpedia.org"?

Could you please help me, what is the right parameter I shoud pass it to the constructor to do that?


The method you have shown is only for querying remote endpoints via HTTP.

If you just want to query a local file use something like the following:

//Define your Graph here - it may be better to use a QueryableGraph if you plan
//on making lots of Queries against this Graph as that is marginally more performant
IGraph g = new Graph();

//Load some data into your Graph using the LoadFromFile() extension method
g.LoadFromFile("myfile.rdf");

//Use the extension method ExecuteQuery() to make the query against the Graph
try
{
  Object results = g.ExecuteQuery("SELECT * WHERE { ?s a ?type }");
  if (results is SparqlResultSet)
  {
     //SELECT/ASK queries give a SparqlResultSet
     SparqlResultSet rset = (SparqlResultSet)results;
     foreach (SparqlResult r in rset)
     {
       //Do whatever you want with each Result
     }
  } 
  else if (results is IGraph)
  {
     //CONSTRUCT/DESCRIBE queries give a IGraph
     IGraph resGraph = (IGraph)results;
     foreach (Triple t in resGraph.Triples)
     {
        //Do whatever you want with each Triple
     }
  }
  else
  {
     //If you don't get a SparqlResutlSet or IGraph something went wrong 
     //but didn't throw an exception so you should handle it here
     Console.WriteLine("ERROR");
  }
}
catch (RdfQueryException queryEx)
{
   //There was an error executing the query so handle it here
   Console.WriteLine(queryEx.Message);
}

For more documentation see Querying with SPARQL which covers the various ways you can make a SPARQL Query.

If you have multiple graphs then you will want to use either a IInMemoryQueryableStore or the LeviathanQueryProcessor with an ISparqlDataset.

You can always ask for help on the mailing lists - dotNetRDF-support@lists.sourceforge.net - if you get stuck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜