sparql> PREFIX ab: <http://learningsparql.com/ns/addressbook#>
Executing update...
Update executed in 160 ms
sparql> SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
Malformed query: org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException:
QName 'ab:firstName' uses an undefined prefix
sparql> SELECT ?craigEmail WHERE { ?person
<http://learningsparql.com/ns/addressbook/firstName> "Craig" }
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (64 ms)

- 21,642
- 4
- 51
- 73

- 71
- 1
- 5
2 Answers
Try putting the prefix on the same line as the query (so everything is on one line).

- 76
- 1
The RDF4J console by default only takes a single-line SPARQL query, so like James suggested in his answer: enter the query as a single line.
Alternatively, you can use the sparql
command to enter into multiline mode:
memory> sparql
enter multi-line SPARQL query (terminate with line containing single '.')
PREFIX ab: <http://learningsparql.com/ns/addressbook#>
SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
.
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (93 ms)
memory>
This is particularly useful when you're copy-pasting larger queries.
As an aside, the first "query" that you do:
sparql> PREFIX ab: <http://learningsparql.com/ns/addressbook#>
Executing update...
Update executed in 160 ms
The output is misleading, since 'Update executed' suggests you've just added the new namespace prefix. But that's not what's happening. Instead, the console interprets this as a SPARQL update that's missing a "body", and just executes it as an empty update. The prefix is ignored. Arguably this is a fault in the SPARQL parser as just a prefix declaration without a body is, strictly speaking, not a legal query.
There is no "official" way to add a new namespace prefix to an RDF4J store via the Console, but there is a trick you can use (though this will only work if you're executing it on a store or SPARQL endpoint to which you have write access):
PREFIX ab: <http://learningsparql.com/ns/addressbook#> INSERT DATA {}
This will execute a SPARQL update to insert new triples, but with an empty block (so no actual triples are inserted). However, this will add the namespace prefix to the repository, after which you no longer have to declare it for every query you enter into the console:
memory> show n
--no namespaces found--
memory> PREFIX ab: <http://learningsparql.com/ns/addressbook#> INSERT DATA {}
Executing update...
Update executed in 3 ms
memory> show n
+----------
|ab http://learningsparql.com/ns/addressbook#
|rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#
|owl http://www.w3.org/2002/07/owl#
|xsd http://www.w3.org/2001/XMLSchema#
|fn http://www.w3.org/2005/xpath-functions#
|rdfs http://www.w3.org/2000/01/rdf-schema#
|sesame http://www.openrdf.org/schema/sesame#
+----------
memory> SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (47 ms)
memory>

- 21,642
- 4
- 51
- 73