使用SPARQL查询RDF

时间:2012-09-11 12:37:18

标签: sparql dotnetrdf

您好我参考http://www.dotnetrdf.org/content.asp?pageID=Querying%20with%20SPARQL上的资料, 我需要一种方法来使用SPARQL读取RDF文件的内容。

如何设置现有RDF文件的路径?

非常感谢,

2 个答案:

答案 0 :(得分:4)

正如@cygri所说,您应该查看Reading RDF文档。

以下是Querying with SPARQL页面中的第一个示例,其中显示了加载要查询的文件:

using System;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;

public class InMemoryTripleStoreExample
{
    public static void Main(String[] args)
    {
        TripleStore store = new TripleStore();

        //Load data from a file
        store.LoadFromFile("example.rdf");

        //Execute a raw SPARQL Query
        //Should get a SparqlResultSet back from a SELECT query
        Object results = store.ExecuteQuery("SELECT * WHERE {?s ?p ?o}");
        if (results is SparqlResultSet)
        {
            //Print out the Results
            SparqlResultSet rset = (SparqlResultSet)results;
            foreach (SparqlResult result in rset)
            {
                Console.WriteLine(result.ToString());
            }
        }

        //Use the SparqlQueryParser to give us a SparqlQuery object
        //Should get a Graph back from a CONSTRUCT query
        SparqlQueryParser sparqlparser = new SparqlQueryParser();
        SparqlQuery query = sparqlparser.ParseFromString("CONSTRUCT { ?s ?p ?o } WHERE {?s ?p ?o}");
        results = store.ExecuteQuery(query);
        if (results is IGraph)
        {
            //Print out the Results
            IGraph g = (IGraph)results;
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine("Query took " + query.QueryExecutionTime.ToString());
        }
    }
}

答案 1 :(得分:3)

答案可能在这里:Reading RDF