Construct RDF graph on federated query?

雨燕双飞 提交于 2020-01-06 07:27:27

问题


I'm new to SPARQL and RDF. Bascially I have to design federated SPARQL 1.1 query over Wikidata and Dbpedia.

Here's my simple query. This will select the films starring Leonardo Di Caprio.

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX dbr: <http://dbpedia.org/resource/> 
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?film WHERE {
  {
    SELECT ?film WHERE {
        ?film  wdt:P161 wd:Q38111.
    }
  }
  UNION 
  {
    SELECT ?film WHERE {
       ?film rdf:type dbo:Film .
       ?film dbo:starring dbr:Leonardo_DiCaprio .
    }
  }
}

I have to construct a graph on that. Can anyone help me on this type of query? I'm testing it on Apache Jena.


回答1:


In SPARQL, SELECT queries will give you an answer formalized as a table.

To get triples, you need to use a CONSTRUCT query and therefore create a graph on them.

With the response of the CONSTRUCT query, you simply need to add the Statement to the Graph.

Graph graph = GraphFactory.createDefaultGraph(); // Create an empty graph

String queryString = "CONSTRUCT { ?s ?p ?o } WHERE ..."

// ... Depends on which way you want to query the dataset
QueryExecution qexec = QueryExecutionFactory.create(queryString, ...) 

Iterator<Triple> triples = qexec.execConstructTriples();

while(triples.hasnext()){
  graph.add(triples.next())
}


来源:https://stackoverflow.com/questions/50584568/construct-rdf-graph-on-federated-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!