1

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
1
  • do you really don't understand the error message "QName 'org:hasUnit' uses an undefined prefix"? I find it very clear and helpful. But you're by far not the first one with missing prefix declarations in a SPARQL query. From my point of view, I'd always try to add all used prefixes to make a SPARQL self-contained such that you can run it in any environment without the need for pre-defined prefixes. Commented Jan 15, 2019 at 5:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply. I was able to execute the query by escaping the "#" in PREFIX. Here is the query that worked 23.101.230.37:7200/repositories/CSIRO?query= PREFIX org: <w3.org/ns/org%23> SELECT (?s AS ?Organization)WHERE{ ?s a org:OrganizationalUnit .} The "#' character in the request URL was causing error. Escaping it solved the issue.
well you need to escape/urlencode all of it really, not just the hash, but yeah
@coderdecoder - Note that the query you put here in a comment is unreadable, and may have missing characters, because you didn't wrap it in backticks -- which tell the markdown interpreter not to interpret what they wrap. I believe you'll find this works exactly as-is -- http://23.101.230.37:7200/repositories/CSIRO?query=PREFIX%20org%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Forg%23%3E%20SELECT%20(%3Fs%20AS%20%3FOrganization)%20(COUNT(%3Fo)%20AS%20%3FCount)%20WHERE%7B%20%3Fs%20org%3AhasUnit%20%3Fo%20%20.%20%7D%20GROUP%20BY%20%3Fs
Hi @JeenBroekstra, I got missing parameter: query when run curl http://localhost:7200/repositories/#repo-test-1?query=CONSTRUCT+%7B%3Fs+%3Fp+%3Fo%7D+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+a10 in ontotext graphdb, could you please advice? I use GraphDB-Free/9.0.0 RDF4J/3.0.0
The answer above this comment is correct and should be accepted; I would just add that curl makes it easy to urlencode sparql queries and test via REST: curl -s -G --header "Accept: application/sparql-results+json" --data-urlencode "query=$QUERY" http://localhost:7200/repositories/$REPOSITORY where QUERY is your plain text SPARQL query, and REPOSITORY is the GraphDB repository where you want the query to execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.