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

问题描述

| 我使用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 \”? 您能帮我吗,我应该将其传递给构造函数的正确参数是什么?     

解决方法

您显示的方法仅用于通过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);
}
有关更多文档,请参阅使用SPARQL查询,其中介绍了进行SPARQL查询的各种方法。 如果您有多个图形,则需要将IInMemoryQueryableStore或LeviathanQueryProcessor与ISparqlDataset一起使用。 您总是可以在邮件列表上寻求帮助-dotNetRDF-support@lists.sourceforge.net-如果遇到麻烦     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...