我如何使用sparql使用dotNetRDF库从我自己的rdf文件中查询?

时间:2011-04-13 21:58:09

标签: c# asp.net sparql rdf dotnetrdf

我使用dotNetRDF库来编写sparql查询。 我使用“http://dbpedia.org/sparql”作为DBPedia SPARQL端点定义远程端点,并将“http://dbpedia.org”定义为默认图形URI:

 SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");

这很有效 但我需要使用我的rdf文件作为默认图形URI“myuniversity.rdf”我将它添加到网站的参数将是什么,而不是“http://dbpedia.org”?

你可以帮助我,我应该把它传递给构造函数的正确参数是什么?

1 个答案:

答案 0 :(得分:1)

您显示的方法仅用于通过HTTP查询远程端点。

如果您只想查询本地文件,请使用以下内容:

//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);
}

有关更多文档,请参阅Querying with SPARQL,其中介绍了进行SPARQL查询的各种方法。

如果您有多个图表,那么您将需要使用IInMemoryQueryableStore或带有ISparqlDataset的LeviathanQueryProcessor。

您可以随时在邮件列表上寻求帮助 - dotNetRDF-support@lists.sourceforge.net - 如果您遇到问题