simple sparql query from dbpedia - sparql

I have a question about a SPARQL query that I'm trying to build from a tutorial. I want to generate triples that return a list of band members and the bands that they are in using a DBPedia endpoint.
my query
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?bandname where {
?person foaf:name ?name .
?band dbo:bandMember ?person .
?band dbo:genre dbpedia:Punk_rock .
?band dbp:name ?bandname .
}
I'm also using a [SPARQL query validator][2] to try to figure out my problem and it seems I'm using an incorrect prefix for "dbp:name ?bandname"I just want the triples returned in JSON if possible.
Once I can get this to run, I'd like to add another prefix, from GeoNames, to see the places associated with the bands, if possible (but that part is in the future). Any insights would be greatly appreciated!

There are some issues with your query.
The "name" property for a band has the following URI : http://dbpedia.org/property/name. It is a property, and not a resource, but you defined it as such in the prefixes. You shoud define it as follows :
PREFIX dbp: <http://dbpedia.org/property/>
I just had a quick check at what a band page has as properties, and saw that, apart from the dbo:bandMember one you are using, there is another property, currentMembers, that seems to retrieve more information. This seems logical though, in so far as dbo:bandMember only retrieves entities (members URIs), whereas dbp:currentMembers also retrieves literals. It depends on your use case here...
You use the dbpedia:prefix in your query, that does not seem to have been defined beforehand.
Here is the query I used to retrieve a list of bands associated with their members :
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?members ?bandName where {
?band dbo:genre dbr:Punk_rock .
?band dbp:currentMembers ?members.
?band foaf:name ?bandName
FILTER(langMatches(lang(?bandName), "en"))
}
The filter part allows us to avoid duplicates in case the literals are also defined in other languages.
But if you still want to use the dbo:bandMember property, this query also does the job :
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?personName ?bandName where {
?band dbo:bandMember ?person .
?person foaf:name ?personName .
?band dbo:genre dbr:Punk_rock .
?band foaf:name ?bandName
FILTER(langMatches(lang(?bandName), "en"))
FILTER(langMatches(lang(?personName), "en"))
}
Hope that helps !

The problem is with undefined prefix dbpedia. Also I think you have to replace dbp:name with foaf:name:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?bandname where {
?person foaf:name ?name .
?band dbo:bandMember ?person .
?band dbo:genre dbp:Punk_rock .
?band foaf:name ?bandname .
}
Alas, with SELECT you won't get triples, but just the names (or other stuff) in tabular results. Much like SQL. If you want triples, you need CONSTRUCT

Related

SPARQL Federated Query Not Returning All Solutions

This is an evolution of this question.
Basically I am having trouble getting all the solutions to a SPARQL query from a remote endpoint. I have read through section 2.4 here because it seems to describe a situation almost identical to mine.
The idea is that I want to filter my results from DBPedia based on information in my local RDF graph. The query is here:
PREFIX ns1:
<http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT *
WHERE {
?p ns1:displayName ?name .
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
}
}
And the only result I get is dbpedia:John_McCain (for ?s). I think this is because John McCain is the only match in the first 'x' results, but I can't figure out how to get the query to return all matches. For example, if I add a filter like:
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
FILTER(?name = "John McCain"#en || ?name = "Jamie Oliver"#en)
}
Then it correctly returns BOTH dbpedia:Jamie_Oliver and dbpedia:John_McCain. There are dozens of other matches like Jamie Oliver that do not come through unless I specifically add it to a Filter like this.
Can someone explain a way to extract the rest of the matches? Thanks.
It looks like the cause of this issue is that the SERVICE block is attempting to pull all foaf:Persons from DBPedia, and then filter them based on my local Stardog db. Since there is a 10,000 result limit when querying DBPedia, only matches which occur in that set of 10,000 arbitrary Persons will be found. To fix this, I wrote a script to put together a FILTER block containing every string name in my Stardog db and attached it to the SERVICE block to filter remotely and thereby avoid hitting the 10,000 result limit. It looks something like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX ns1: <http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
CONSTRUCT{
?s rdf:type ns1:Person ;
ns1:Politician .
}
WHERE {
?s rdfs:label ?name .
?s rdf:type dbo:Politician .
FILTER(?name IN ("John McCain"#en, ...)
}

SPARQL query not returning any data

I am new to RDF, so it will be very nice if you can help me with this!
I am trying to query the subject of pickles called "Umeboshi"(It is japanese pickles) as follows:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT ?label ?subject
WHERE {
?Thing
rdfs:label ?label;
prop:subject?subject.
FILTER (?label = "Umeboshi")
}
This query doesn't give me any data.
As I don't know where to find available properties I am referring to the Umeboshi page on dbpedia http://live.dbpedia.org/page/Umeboshi.
Thank you very much for your help!
Two things I found:
In the page you give, the label is given in English, but in your query you omit the language.
subject has a different namespace. It is a dcterm concept and not a dbpedia property.
This leads to the following, changed query, which results in three bindings:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?label ?subject
WHERE {
?Thing
rdfs:label ?label;
dct:subject ?subject.
FILTER (?label = "Umeboshi"#en)
}

Sparql result not containing specified property included in results

I have this query
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbpedia_property: <http://dbpedia.org/property/>
PREFIX dbpedia_ontology: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX schema: <http://schema.org/>
SELECT * WHERE
{
{
SELECT ?school
WHERE
{
?school rdf:type yago:EducationalInstitution108276342 .
FILTER ( contains(str(?school), "Australia") )
}
ORDER BY ?school
}
}
Don't mind the extra brackets as this is part of a larger query.
What I want to know is why thi http://dbpedia.org/page/Academic_structure_of_the_Australian_National_University is included in the results since I specify rdf:type yago:EducationalInstitution108276342. This property is not included in the resource page. I'm using this endpoint: http://dbpedia.org/sparql
Looks like a bug in the Pubby Web interface or in the query that is used to get the data that will be shown.
The query
SELECT * WHERE{
<http://dbpedia.org/resource/Academic_Structure_of_the_Australian_National_University> ?p ?o
}
returns the necessary rdf:type statement.
The other strange thing is that even a SPARQL DESCRIBE query does not return the rdd:type triples:
DESCRIBE <http://dbpedia.org/resource/Academic_Structure_of_the_Australian_National_University>
Although DESCIBE is not really defined in the specs, a user would expect those triples for sure. And maybe this kind of query is used to retrieve the data for the Web pages of resources.

Querying polymorphism in RDF database using SPARQL

I have a question about polymorphism in RDF schema.
I created a class "MobilePhone":
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix schema: <http://schema.org/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix my: <http://localhost/> .
my:MobilePhone rdf:type rdfs:Class .
my:MobilePhone rdfs:subClassOf schema:Product .
my:MobilePhone rdfs:label "MobilePhone" .
Then I executed two queries:
PREFIX : <http://schema.org/>
PREFIX my: <http://localhost/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?instance
WHERE {
?instance a my:MobilePhone .
}
and
PREFIX : <http://schema.org/>
PREFIX my: <http://localhost/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?instance
WHERE {
?instance a :Product .
}
I expected the second query to retrieve all Products. Even "MobilePhone" products. But that didn't happen. I had to use:
PREFIX : <http://schema.org/>
PREFIX my: <http://localhost/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?instance
WHERE {
?class rdfs:subClassOf* :Product .
?instance rdf:type ?class .
}
Does anyone know if it is possible to update my schema so the first query for searching all Products would work?
It is possible to update your data (not your schema [or ontology]) to deliver the desired result, by "forward chaining" -- i.e., by explicitly including statements that each entity which is a my:MobilePhone is also a :Product.
It's also possible to use "backward chaining" to deliver the desired result; the specifics of this will vary with your triple/quad store and other software. (My employer's solution, Virtuoso, uses a DEFINE input:inference pragma to activate an inference rule set for each query.)
Both of these may be considered as "inferencing" or "reasoning", though common usage generally applies these terms only to the dynamic action -- which happens with every query in "backward chaining", and once, pre-query, typically during data load, in "forward chaining".

What is wrong with my SPARQL query and how can I fix it?

I am learning how to create SPARQL queries. Currently, I am using Dbpedia datasets.
I tried to query about "What are the airports that are in Canada" with the following query:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?name ?country WHERE {
?name rdf:type <http://dbpedia.org/ontology/Airport>;
?name rdf:type <http://dbpedia.org/ontology/Country>
}
LIMIT 20
I am still confused about building SPARQL queries especially with resources and RDF graphs.
What I need is what is the mistake with the above query?
Thanks
The query you are looking for is something like:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/ontology/Airport>;
rdfs:label ?label;
dbpedia-owl:location <http://dbpedia.org/resource/Canada> .
}
This query however doesn't return much results and you would be better of with something like:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?airport ?label WHERE {
?airport rdf:type <http://dbpedia.org/class/yago/AirportsInOntario> ;
rdfs:label ?label .
}
There are various things wrong in your initial query that imply that you should get a better understanding of SPARQL. You need to revise the way you construct the triple patterns. I recommend you to have a look at the following tutorial:
http://www.cambridgesemantics.com/2008/09/sparql-by-example/
Also you will find exploratory SPARQL queries tremendously helpful:
exploratory SPARQL queries?
Your query is currently asking for objects (resources) that have type Airport AND have type Country. Needless to say, there are no results.
You query is also asking for ?country which is completely undefined.
See msalvadores' answer for a correct example...