coderdecoder
coderdecoder

Reputation: 11

Unable to execute SPARQL Select Query using GraphDB REST interface

I have a graphDB instance running on a VPS and i want to get the list of Organisations with hasUnit relation. The following query executes fine on the SPARQL execution page:

PREFIX org: <http://www.w3.org/ns/org#>
SELECT (?s AS ?Organization) (COUNT(?o) AS ?Count)
WHERE{
    ?s org:hasUnit ?o  .
} GROUP BY ?s

but when i try to get the results using the REST interface i get an error "MALFORMED QUERY: org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException: QName 'org:hasUnit' uses an undefined prefix"

here is my request uri:

http://23.101.230.37:7200/repositories/CSIRO?query= SELECT (?s AS ?Organization) (COUNT(?o) AS ?Count) WHERE{
    ?s org:hasUnit ?o  . } GROUP BY ?s

The following query executes fine though:

http://23.101.230.37:7200/repositories/CSIRO?query= SELECT (?s AS ?Organization) (COUNT(?o) AS ?Count) WHERE{
    ?s ?p ?o  . } GROUP BY ?s

Upvotes: 1

Views: 1408

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22042

The problem is, as the error message indicates, that the namespace prefix org: is undefined in your query.

Notice that in the query you tried in the Workbench UI this is the first line:

PREFIX org: <http://www.w3.org/ns/org#> 

But this line is missing in the request you do via the REST API call. To fix, start your SPARQL query with this line when you do the REST call:

http://23.101.230.37:7200/repositories/CSIRO?query=PREFIX org: <http://www.w3.org/ns/org#> SELECT (?s AS ?Organization) (COUNT(?o) AS ?Count) WHERE{ ?s org:hasUnit ?o  . } GROUP BY ?s

Edit note that the actual SPARQL query needs to be in urlencoded form when you put it in a URL as a parameter like this - although some client tools may handle this for you. I've left it unencoded here for readability.

The reason you don't get this error with the second query, by the way, is that in that second query you don't use any predicate with the org: prefix.

Every resource in RDF and SPARQL is identified by an IRI. For example, in your query you use the property identifier http://www.w3.org/ns/org#hasUnit. A namespace prefix is a way to introduce a shorthand, so that you don't have the write down the full IRI every time. In this example, org: becomes a shorthand for http://www.w3.org/ns/org#, so the property identifier can be written in shorthand (as a prefixed name) as org:hasUnit.

Upvotes: 1

Related Questions