0

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Aziz Mumtaz
  • 95
  • 1
  • 6
  • For federated query you need the `SERVICE` keyword. As you also want triples, you need to combine this with `CONSTRUCT` – UninformedUser May 29 '18 at 17:19
  • On the other hand, I don'r see the need for a federated query in your example. What's wrong with simply doing two queries, one on DBpedia and one on Wikidata? Especially as your examüple would try to join on different URIs bound to the variable `?film` – UninformedUser May 29 '18 at 17:20
  • @AKSW my task is to use 1.0 sparql so I have to leave out the "service". Although I made some slight mistake on the query. I will need to modify it. – Aziz Mumtaz May 29 '18 at 20:22
  • SPARQL 1.0 does neither support federated querying, nor does it support sub-queries as you used in the query. Do two separate `CONSTRUCT` queries per knowledge base, then merge the triples. That's all – UninformedUser May 30 '18 at 03:32
  • By the way, in your question you said *"I have to design federated **SPARQL 1.1** query over Wikidata and Dbpedia."* - now you say you want to use **SPARQL 1.0** ... – UninformedUser May 30 '18 at 03:32
  • Looks like homework ... and like [Federation query on Dbpedia and Wikidata](https://stackoverflow.com/questions/50603227/). – TallTed Jun 08 '18 at 20:34

1 Answers1

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())
}
Gilles-Antoine Nys
  • 1,481
  • 16
  • 21