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

发布于 2024-11-01 03:39:12 字数 411 浏览 4 评论 0原文

我使用 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”?

您能帮我吗,我应该将其传递给构造函数来执行此操作的正确参数是什么?

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/sparql"), "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-11-08 03:39:12

您显示的方法仅用于通过 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 或带有 ISparqlDataset 的 LeviathanQueryProcessor。

您可以随时在邮件列表上寻求帮助 - [电子邮件受保护] -如果你被卡住了

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 - [email protected] - if you get stuck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文