SPARQL Federated Query takes too long to respond - sparql

I have a SPARQL federated query where I join data from wikidata and dbpedia. When I run the first two queries it takes reasonable time. However, when I add the 3rd service it takes too much time. In the 3rd query I fetch the entities obtained from first two queries and filter by looking at if they are 'subclass of' 'percussion instrument'.
Here is my query (Query for returning percussion instruments in middle east):
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT DISTINCT
?instrument
(?countryDbpediaID)
(?country as ?wikidataID)
(?countryLabel as ?origin)
WHERE {
SERVICE <https://query.wikidata.org/sparql>
{
SELECT DISTINCT ?country ?countryLabel
WHERE {
?country wdt:P361 wd:Q7204 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
}
SERVICE <https://dbpedia.org/sparql>
{
SELECT DISTINCT ?intermediateEntityDbpediaID
?intermediateEntityWikidataUri
?intermediateEntityWikidataID
?countryDbpediaID ?description
FROM <http://dbpedia.org>
WHERE { ?countryDbpediaID owl:sameAs ?country;
rdfs:label ?label ;
foaf:depiction ?image;
rdfs:comment ?description .
?intermediateEntityDbpediaID dbp:origin ?countryDbpediaID;
rdfs:label ?intermediateEntityLabel ;
owl:sameAs ?intermediateEntityWikidataUri .
FILTER (LANG(?label) = "en")
FILTER (LANG(?intermediateEntityLabel) = "en")
FILTER (STRSTARTS(STR(?intermediateEntityWikidataUri), STR('http://www.wikidata.org')))
FILTER (LANG(?description) = "en")
BIND(REPLACE(STR(?intermediateEntityWikidataUri),"http://www.wikidata.org/entity/","","i") AS ?intermediateEntityWikidataID)
}
}
SERVICE <https://query.wikidata.org/sparql>
{
SELECT DISTINCT ?instrument
WHERE {
?instrument wdt:P279 wd:Q133163 .
FILTER (?instrument in (URI(?intermediateEntityWikidataUri)))
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
}
}
I found this related question but it didn't help me : SPARQL Speed up federated query
Is there any way to optimize this query?

The three federated queries here do not have any shared join variables. The only variables being returned from them (in the sub-queries' SELECT DISTINCT clauses) are all disjoint. That means that evaluation is performing two cartesian joins.
The three sub-queries return 21, 504, and 0 results, respectively. So I think the end result would be zero rows returned. But the query engine may be taking a very sub-optimal route towards that answer and timing out.
Update:
Given the repeated use of variables like ?intermediateEntityWikidataUri, I suspect these are intended to be used to join data across the federated sub-queries. But as written, the query can't do that. For example, given SPARQL's bottom-up semantics, you can't use ?intermediateEntityWikidataUri in the FILTER of the third query without that variable being bound in the same scope.
No matter what else is in the query, this:
SERVICE <https://query.wikidata.org/sparql>
{
SELECT DISTINCT ?instrument
WHERE {
?instrument wdt:P279 wd:Q133163 .
FILTER (?instrument in (URI(?intermediateEntityWikidataUri)))
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
}
will result in zero results, because the filter expression is evaluated using an unbound variable (which will filter out all results).

Related

Sparql in combination with DBpedia

i want to display the countries with their net value/income or something similiar through accessing the data through DBPedia. and further insert them into an already created table.
Unfortunately i dont get any results with my code.
Try the following query to list the type of entities that has a dbp:income as a property. you will notice that dbo:Country is not the list, that's why the result you got was empty.
prefix dbp: <http://dbpedia.org/property/>
select distinct ?type
where {
?s a ?type.
?s dbp:income ?income
}
My suggestion is to use dbp:gdpNominal instead of the dbp:income property in your query :
prefix dbp: <http://dbpedia.org/property/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?country_name ?income
where {
?country a dbo:Country.
?country rdfs:label ?country_name. FILTER (lang(?country_name) = 'en')
?country dbp:gdpNominal ?income.
}

Simple SPARQL Query

I am working from this example, and I want to archieve the same, however with a different topic - Climate change
All i need to output is the abstract from this page: http://dbpedia.org/page/Climate_change
PREFIX dbp-res: <http://dbpedia.org/resource/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
PREFIX dbp-prop: <http://dbpedia.org/property/>
SELECT *
WHERE
{
?Resource a dbp-ont:Agent .
?Resource dbp-ont:abstract ?Description .
?Resource rdfs:label ?Label .
FILTER( STR(?Label) = 'Climate_change' )
FILTER (langMatches(lang(?Description),'en'))
FILTER (langMatches(lang(?Label),'en'))
}
The problem seems to be the Agent, but I have no clue, as to what to replace it with.
My query

Why is this sparql query not returning any rows on dbpedia?

This is my query below, querying the country names with a certain minimum population, executing on http://dbpedia.org/sparql.
even though i change the population variable to a tiny amount. there are no rows being returned. why?
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
?country a type:LandlockedCountries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population > 15000000 && langMatches(lang(?country_name), "en")) .
} ORDER BY DESC(?population)
Because there is no class http://dbpedia.org/class/yago/LandlockedCountries in DBpedia - I don't know why you think that there is such a class?
There is a Wikipedia category Landlocked_countries, thus, the URI would be http://dbpedia.org/resource/Category:Landlocked_countries and the property that relates resources to a category is http://purl.org/dc/terms/subject:
PREFIX prop: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT *
WHERE {
?country dct:subject dbc:Landlocked_countries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population > 15000000 && langMatches(lang(?country_name), "en")) .
} ORDER BY DESC(?population)
In general, "debugging" a SPARQL query can be done by starting with just a single triple pattern and checking if this returns the expected resp. any result.

How to combine the results using SPARQL (dbpedia.org with linkedmdb.org or freebase.com)

I need to get all awarded movies on 80th Award Ceremony
I tried to write SPARQL query:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
prefix movie: <http://data.linkedmdb.org/resource/movie/>
prefix award: <http://data.linkedmdb.org/page/film_awards_ceremony/180/>
select distinct ?film ?award where {
{ ?film a movie:film.
?award a movie:film_awards_ceremony.
} union
{ ?film a dbpedia-owl:Film }
?film rdfs:label ?label .
}
But the result is full movies list.
I found the data I need also here: https://www.freebase.com/m/02pgky2
How to combine (union) these entities in a right way ?
If is not possible - How to get the result from freebase using SPARQL and dbpedia.org?

SPARQL query does not give all the results that are on dbpedia

I'm executing this query in http://dbpedia.org/snorql/:
Query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
?country a type:LandlockedCountries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
}
The query looks for all land looked countries.
I don't understand why in the results there are not some countries which are classified on
"/dbpedia.org/class/yago/LandlockedCountries". For example, Paraguay (/dbpedia.org/page/ParaguAy) is classified but does not appear in the query result set. Can somebody explain me why?
Unfortunately, there are a small number of landlocked countries that do not have values for at least one of the country_name and populationEstimate properties. This is why they will not be returned in your query. If you run the following query, those countries will come up (those two attributes are set as OPTIONAL).
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country ?country_name ?population
WHERE {
?country a type:LandlockedCountries .
OPTIONAL {?country rdfs:label ?country_name Filter(lang(?country_name) = 'en')} .
OPTIONAL {?country prop:populationEstimate ?population} .
}
run query
For (slightly) better results, since some countries seem to be duplicated with erroneous capitalization (e.g. ParaguAy and Paraguay), the following query uses ?country dcterms:subject category:Landlocked_countries instead of the yago class.
run query