问题
I ran the following query and got the data for movies and their corresponding DBpedia URI from linkedmdb.
SELECT ?film ?label ?dbpediaLink
WHERE {
?film rdf:type movie:film .
?film rdfs:label ?label .
?film owl:sameAs ?dbpediaLink
FILTER(regex(str(?dbpediaLink), "dbpedia", "i"))
}
LIMIT 100
I want to get the category for these movies from DBpedia using the ?dbpediaLink
URI. Also, I need to get the value of the dcterms:subject
property for the movie from DBpedia. I am not able to understand how to connect them? Can I do it via SPARQL or do I need to write code for this?
回答1:
You want a federated SPARQL query. You do not need to write code (other than the SPARQL query). You'll need an endpoint that supports them, and since it requires querying multiple endpoints, you might need to run your own endpoint (that might not contain any actual data, but can do the federation for you). Based on the requirements you outlined above, you probably want a query like the following. Notice that I removed ?dbpediaLink
from the projected variables; that is, this query does not SELECT ?dbpediaLink
, since you're probably not interested in seeing that value so much as using it to retrieve data from DBpedia. You can add it back if you want it, of course.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?film ?label ?subject WHERE {
SERVICE <http://data.linkedmdb.org/sparql> {
?film a movie:film .
?film rdfs:label ?label .
?film owl:sameAs ?dbpediaLink
FILTER(regex(str(?dbpediaLink), "dbpedia", "i"))
}
SERVICE <http://dbpedia.org/sparql> {
?dbpediaLink dcterms:subject ?subject
}
}
LIMIT 50
You cannot run that directly against the DBpedia SPARQL endpoint, but you can use Jena's ARQ to run the query locally, and ARQ will do the queries to the other endpoints for you. I created an empty file, data.n3
to pass to ARQ as the --query
argument, and was able to run the query like this (where query.sparql
is the query above):
$ arq --query query.sparql --data data.n3
The results look like the following. Note that each DBpedia resource can be related to a number of categories by the dcterms:subject
property, so even though we have a LIMIT 50
there, we're not actually getting fifty movies, but fifty result rows.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| film | label | subject |
================================================================================================================================================================================
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:1960s_horror_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Films_directed_by_Jun_Fukuda> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Giant_monster_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Monster_movies> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:1966_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Godzilla_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Japanese_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Kaiju_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Mothra> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Terrorism_in_fiction> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:1960s_science_fiction_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Sequel_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Japanese-language_films> |
| <http://data.linkedmdb.org/resource/film/11> | "Godzilla vs. the Sea Monster" | <http://dbpedia.org/resource/Category:Mystery_Science_Theater_3000_episodes> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:1930s_musical_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:American_musical_comedy_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:American_romantic_comedy_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:American_romantic_musical_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:1935_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:American_screwball_comedy_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:Black-and-white_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:Films_directed_by_Mark_Sandrich> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:1930s_romantic_comedy_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:United_States_National_Film_Registry_films> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:Films_set_in_London> |
| <http://data.linkedmdb.org/resource/film/105> | "Top Hat" | <http://dbpedia.org/resource/Category:RKO_Pictures_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:American_children's_fantasy_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Best_Original_Music_Score_Academy_Award_winners> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:E.T._the_Extra-Terrestrial> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:English-language_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:1980s_science_fiction_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:1982_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Alien_visitation_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Amblin_Entertainment_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Films_directed_by_Steven_Spielberg> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:1985_novels> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:American_science_fiction_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Films_produced_by_Steven_Spielberg> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Films_set_in_the_San_Fernando_Valley> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Films_that_won_the_Best_Sound_Mixing_Academy_Award> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:United_States_National_Film_Registry_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Universal_Pictures_films> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Best_Drama_Picture_Golden_Globe_winners> |
| <http://data.linkedmdb.org/resource/film/154> | "E.T. the Extra-Terrestrial" | <http://dbpedia.org/resource/Category:Films_that_won_the_Best_Visual_Effects_Academy_Award> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:1960s_drama_films> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:American_films> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:1969_films> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:Films_shot_in_New_Orleans,_Louisiana> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:Directorial_debut_films> |
| <http://data.linkedmdb.org/resource/film/155> | "Easy Rider" | <http://dbpedia.org/resource/Category:Films_about_drugs> |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
来源:https://stackoverflow.com/questions/14980386/connecting-linkedmdb-and-dbpedia-via-federated-sparql-queries