Firstly, I found this relevant question and answer:
Unresolved prefixed name: rdfs:subClassOf in SPARQL query
If I add the PREFIX in the SPARQL query it works. However, I don't want to duplicate all the prefixes in all my SPARQL queries, but to define them only once. I tried to do it programmatically for rdfs::
model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
and it works, but if I try to do it with my own ontologies it doesn't work:
model.setNsPrefix("myOnt", "http://example.com/ontologies/myOnt#");
query.setPrefix("myOnt", "http://example.com/ontologies/myOnt#");
You can set up a Prologue to contain you prefix mappings and then pass that into the QueryFactory with your specific SPARQL query.
Something like the following worked for me using Apache Jena 3.0.0:
Prologue queryPrologue = new Prologue();
queryPrologue.setPrefix("skos", "http://www.w3.org/2004/02/skos/core#");
String sparql = "SELECT (COUNT ?s) WHERE { ?s a skos:Concept . }"
Query query = QueryFactory.parse(new Query(queryPrologue), sparql, null, null);
try(QueryExecution queryExec = QueryExecutionFactory.create(query, dataset)) {
// ...
}
Related
I'm trying to construct a SPARQL query using the rdf4j documentation: https://rdf4j.org/documentation/tutorials/sparqlbuilder/
I am a newbie to java (and stackoverflow, apologies if this is poorly written), but I think I have included the right beginning steps. I have instantiated a select query, a prefix and a variable in the following way:
SelectQuery draftQuery = Queries.SELECT();
Prefix default = SparqlBuilder.prefix("dc", Rdf.iri("url"));
Variable draftCount = SparqlBuilder.var("draftCount");
url has been substituted with the right prefix
The query I am trying to write is: SELECT ?x WHERE { :team_1 :draftCount ?x}, but ?x has no rdf:type and is simply a value that is attached to :draftCount. I have no idea how to write it as a SPARQL query within java, because from what I understand within the docs, the example included where the product is a book, the product has rdf:type "book". I don't want to query multiple variables (e.g. :team_1 ?x ?y) because there are other triples attached to the team and I want to query them separately. I want to have another SPARQL query later that is similar but is SELECT ?x WHERE { :team_1 :completedCount ?x},
How can I write this query? This is what I have so far:
draftQuery.prefix(default).select(draftCount)
You made a good start, but you are mixing up variables and IRIs: :team_1 and :draftCount are IRIs in your query, not variables. Only ?x is a variable. Since you are using the default namespace for your IRs, just create a Prefix object for the default namespace, like so:
Prefix defaultNs = SparqlBuilder.prefix(Rdf.iri("http://example.org/"));
and then you can use that to create Iri objects for use in the query, like so:
Iri team1 = defaultNs.iri("team_1");
To add a relation other than rdf:type using the SparqlBuilder, use the .has method.
Putting it all together, to produce this SPARQL query string:
PREFIX: <http://example.org/>
SELECT ?x WHERE { :team_1 :draftCount ?x}
You do this:
Prefix defaultNs = SparqlBuilder.prefix(Rdf.iri("http://example.org/"));
Iri team_1 = defaultNs.iri("team_1"), draftCount = defaultNs.iri("draftCount");
Variable x = SparqlBuilder.var("x");
SelectQuery query = Queries.SELECT()
.prefix(defaultNs)
.select(x)
.where(team_1.has(draftCount, x));
System.out.println(query.getQueryString());
I want to reproduce the example from the Babelnet SPARQL Endpoint in DBpedia/Snorql.
I enter the prefixes and the query as such:
PREFIX bn-lemon: <http://babelnet.org/model/babelnet#>
PREFIX lemon: <http://www.lemon-model.net/lemon#>
SELECT ?entries WHERE {
?entries a lemon:LexicalEntry .
?entries lemon:language ?lang .
FILTER(?lang = "IT")
} LIMIT 30
and I get no result. If I run exactly the same query (without the 2 prefixes) in the Babelnet SPARQL endpoint (https://babelnet.org/sparql/) I get several results. What may be the reason for this? I have hat similar situations when using other prefixes on different subjects outside of DBpedia.
Thank you for the valuable feedback. I have modified my code using SERVICE as such:
PREFIX lemon: <http://lemon-model.net/lemon#>
PREFIX lexinfo: <http://www.lexinfo.net/ontology/2.0/lexinfo#>
SELECT DISTINCT ?entries WHERE {
SERVICE <http://babelnet.org/rdf/> {
?entries a lemon:LexicalEntry .
?entries lemon:language ?lang .
FILTER(?lang = "IT")
}
}
LIMIT 30
However the query is blocked for security reasons: Virtuoso 42000 Error SQ070:SECURITY: Must have select privileges on view DB.DBA.SPARQL_SINV_2 SPARQL query
I assume that people using a public SPARQL accesspoint like DBpedia sparl have no chance. Are there any alternatives beside installing your own accesspoint?
I want to make a query that does the following: Select all triples (s,p,o) if there exists a path with the length of at least 2 edges from s to o with the property p. So all edges of the path have to be labelled with p.
I tried the following:
select ?s <http://dbpedia.org/ontology/isPartOf> ?o
WHERE {
?s <http://dbpedia.org/ontology/isPartOf>{2,} ?o.
?s <http://dbpedia.org/ontology/isPartOf> ?o
}
I executed it with the Jena API:
ParameterizedSparqlString parameterizedSparql = new ParameterizedSparqlString(model);
parameterizedSparql.setCommandText(sparql);
Query query = QueryFactory.create(parameterizedSparql.asQuery().toString(), Syntax.syntaxARQ);
QueryExecutionFactory.create(query, model).execSelect();
I used Syntax.syntaxARQ so that it should understand property paths.
It gives me the following error:
Exception in thread "main" org.apache.jena.query.QueryParseException: Encountered " "{" "{ "" at line 3, column 42.
Was expecting one of:
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
<BLANK_NODE_LABEL> ...
<VAR1> ...
<VAR2> ...
Can you please show me how I can make the query correctly?
Also, as #AKSW noted, the {2,} syntax from the SPARQL 1.1 Working Draft didn't make it into the final SPARQL 1.1 spec, so you can't rely on it being supported by every SPARQL processor.
You can use the {2,} syntax with Virtuoso, which is the engine powering the public DBpedia endpoint, but to do so through Jena, you have to either use "extended syntax" (Syntax.syntaxARQ) or bypass the ARQ parser.
It appears that your immediate issue comes down to a bug in Jena, where ParameterizedSparqlString.asQuery() does not currently support "extended syntax" (Syntax.syntaxARQ) queries; parameterizedSparql.toString() should be sufficient, as commented by #AndyS.
I am new to SPARQL and trying to fetch a movie adapted from specific book from dbpedia. This is what I have so far:
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
<http://dbpedia.org/page/2001:_A_Space_Odyssey> a ?type.
?type onto:basedOn ?book .
?book a onto:Book
}
I can't get any results. How can I do that?
When using any web resource, and in your case the property :basedOn, you need to make sure that you have declared the right prefix. If you are querying from the DBpedia SPARQL endpoint, then you can directly use dbo:basedOneven without declaring it, as it is among predefined. Alternatively, if you want to use your own, or if you are using another SPARQL client, make sure that whatever short name you choose for this property, you declare the prefix for http://dbpedia.org/ontology/.
Then, first, to get more result you may not restrict the type of the subject of this triple pattern, as there could be movies that actually not type as such. So, a query like this
select distinct *
{
?movie dbo:basedOn ?book .
?book a dbo:Book .
}
will give you lots of good results but not all. For example, the resource from your example will be missing. You can easily check test the available properties between these two resource with a query like this:
select ?p
{
{<http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey> }
UNION
{ <http://dbpedia.org/resource/2001:_A_Space_Odyssey> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)>}
}
You'll get only one result:
http://www.w3.org/2000/01/rdf-schema#seeAlso
(note that the URI is with 'resource', not with 'page')
Then you may search for any path between the two resource, using the method described here, or find a combination of other patterns that would increase the number of results.
I am trying to retrieve the value of the dbpedia-owl:influenced in this page e.g: Andy_Warhol
The query I write is:
PREFIX rsc : http://dbpedia.org/resource
PREFIX dbpedia-owl :http://dbpedia.org/ontology
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:infuenced ?o .
}
but it is EMPTY.
Strange is that when I have the same query for another property from the ontology type like "birthPlace", the sparql engine gives the result back:
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:birthplace ?o .
}
which is a link to another resource:
dbpedia.org/resource/Pittsburgh
I am just confused how to write this query?
besides several formal errors addressed in the answer of #Joshua, there is also the semantic problem that the properties you are looking for - in this case - seem to be found on the entities that were influenced.
this query might give you the desired results
PREFIX rsc: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?s WHERE {
?s dbpedia-owl:influencedBy rsc:Andy_Warhol .
}
run query
There are a few issues here. One is that the SPARQL, as presented, isn't correct. I edited to make the prefix syntax legal, but the prefixes were still wrong (they didn't end with a final slash). You don't want to be querying for http://dbpedia.org/resourceAndy_Warhol after all; you want to query for http://dbpedia.org/resource/Andy_Warhol. Some standard namespaces for DBpedia are listed on their SPARQL endpoint. Using those namespaces and the SPARQL endpoint, we can ask for all the triples that have http://dbpedia.org/resource/Andy_Warhol as the subject with this query:
SELECT * WHERE {
dbpedia:Andy_Warhol ?p ?o .
}
In the results produced there, you'll see the one using http://dbpedia.org/ontology/birthPlace (note the captial P in birthPlace), but you won't see any triples with the predicate http://dbpedia.org/ontology/infuenced, so it makes sense that your first query has no results. Do you have some reason to suppose that there should be some results?