Skip to content

How to Query across different Databases(Different RDF Data Source)

Junjun Cao edited this page Aug 19, 2024 · 2 revisions

Using FROM, GRAPH, SERVICE and SPONGE...(To be Finished)

1. Query across different graphs in a local Virtuoso SPARQL endpoint

Key word of SPARQL: FROM or GRAPH

1.1 Using FROM

SELECT *
FROM <named graph>
#FROM <another named graph>
WHERE {...}

1.2 Using GRAPH

For example, examine how many triples with wdt:P136 in all across musicbrainz and simssa? See as below:

WHERE {
  {GRAPH <http://sample/musicbrainz/reconciled> {
    ?s <http://www.wikidata.org/prop/direct/P136> ?o .
  }}
  UNION
  {GRAPH <http://sample/simssa/reconciled> {
    ?s <http://www.wikidata.org/prop/direct/P136> ?o .
  }}
}

2. Query against one graph or across different graphs from an external SPARQL endpoint

Key words of SPARQL: GRAPH SERVICE

SELECT ?subject ?predicate ?object
WHERE {
  SERVICE <IRI of a SPARQL Endpoint> {
    GRAPH <IRI of a Graph> {
      ?subject ?predicate ?object .
    }
  }
}
  • In addition, one can also query from between some internal graph(s) and some external graph(s),just as this format:
SELECT *
WHERE {
  GRAPH <IRI> {...}
  SERVICE <URL> {...}
}

3. Query across different external SPARQL endpoints

Federated Queries

Key word of SPARQL: SERVICE If one wants to query similar entities from different internal or external databases,

3.1 From one internal Graph and the other external SPARQ Endpoint

for example, to ...

SELECT *
WHERE {
  GRAPH ?g {
    ?s ?p ?o .
    FILTER(STRSTARTS(STR(?o), "http://www.wikidata.org/entity/")) 
  }
  SERVICE <https://query.wikidata.org/sparql> {
    ?wikidataBook wdt:P50 ?wikidataAuthor .
    ?wikidataBook wdt:P136 ?wikidataGenre .
    ?wikidataBook wdt:P1476 "Pride and Prejudice"@en .
  }
}
LIMIT 10

3.2 From no less than 2 external SPARQL Endpoints

for example, to check Pride and Prejudice across DBPedia and Wikidata:

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE {
  SERVICE <http://dbpedia.org/sparql> {
    ?dbpediaBook a dbpedia-owl:Book .
    ?dbpediaBook dbpedia-owl:author ?dbpediaAuthor .
    ?dbpediaBook dbpedia-owl:literaryGenre ?dbpediaGenre .
    ?dbpediaBook rdfs:label "Pride and Prejudice"@en .
  }
  SERVICE <https://query.wikidata.org/sparql> {
    ?wikidataBook wdt:P50 ?wikidataAuthor .
    ?wikidataBook wdt:P136 ?wikidataGenre .
    ?wikidataBook wdt:P1476 "Pride and Prejudice"@en .
  }
}
LIMIT 10

--As to the codes as above,we change those into:

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE {
  SERVICE <http://dbpedia.org/sparql> {
    ?dbpediaBook a dbpedia-owl:Book .
    ?dbpediaBook dbpedia-owl:author ?dbpediaAuthor .
    ?dbpediaBook dbpedia-owl:literaryGenre ?dbpediaGenre .
    ?dbpediaBook rdfs:label ?Title_dbp .
    FILTER CONTAINS(LCASE(?Title_dbp), "pride")
  }
  SERVICE <https://query.wikidata.org/sparql> {
    ?wikidataBook wdt:P50 ?wikidataAuthor .
    ?wikidataBook wdt:P136 ?wikidataGenre .
    ?wikidataBook wdt:P1476 ?Title_wiki .
    FILTER CONTAINS(LCASE(?Title_wiki), "prejudice")
  }
  FILTER (STR(?Title_dbp) = STR(?Title_wiki))
}
LIMIT 100

4. Use Sponger to...

Please refer to Virutoso-Setup_Guide> Other configurations