-
Notifications
You must be signed in to change notification settings - Fork 4
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)
Key word of SPARQL: FROM
or GRAPH
SELECT *
FROM <named graph>
#FROM <another named graph>
WHERE {...}
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 .
}}
}
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> {...}
}
Key word of SPARQL: SERVICE
If one wants to query similar entities from different internal or external databases,
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
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
Please refer to Virutoso-Setup_Guide> Other configurations