Query example 1: Retrieve the value of a known property of a known resource
SELECT ?x
WHERE (<http://somewhere/res1>, <http://somewhere/pred1>, ?x)
Query example 2: constraints
SELECT ?a, ?b
WHERE (?a, <http://somewhere/pred1>, ?b)
AND ?b < 5
Query example 3: paths in the graph
SELECT ?a, ?b
WHERE (?a, <http://somewhere/pred1>, ?c) ,
(?c, <http://somewhere/pred2>, ?b)Usage
There are two ways to use RDQL : from the command line and in Java programs.
Command Line
The main class is jena.rdfquery:
java -cp ... jena.rdfquery [--data modelFile] [--query File | queryString]
With no arguments, it prints a usage message.
Java
Example code fragment 1 (basic template)
Query query = new Query(queryString) ; // Need to set the source if the query does not. // This provided this override any query named source. query.setSource(model); QueryExecution qe = new QueryEngine(query) ; QueryResults results = qe.exec() ; for ( Iterator iter = results ; iter.hasNext() ; ) { ResultBinding res = (ResultBinding)iter.next() ; // Return from get is null if not found Object obj = res.get("x") ; // obj will be a Jena object: resource, property or RDFNode. } results.close() ;The ResultBinding class allows an alternative form which can be used to get quoted strings:
Value v = env.getValue("x") ; String s = (v == null) ? "<null>" : v.asQuotedString() ; Example code fragment 2 (using the results formatter)
Query query = new Query(queryString) ; query.setSource(model); QueryExecution qe = new QueryEngine(query) ; QueryResults results = qe.exec() ; QueryResultsFormatter fmt = new QueryResultsFormatter(results) ; PrintWriter pw = new PrintWriter(System.out) ; fmt.printAll(pw, " | ") ; pw.flush() ; fmt.close() ; results.close() ;The command line usage is a wrapper around the Java interface.
Query example 4: contents of a bag
SELECT ?x, ?y
WHERE (<http://never/bag>, ?x, ?y)
AND ! ( ?x eq <rsyn:type> && ?y eq <rsyn:Bag>)
USING rsyn FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Query example 5: identifying the RDF model in the query
SELECT ?a, ?b
FROM <http://somewhere/model1.rdf>
WHERE (?a, <http://somewhere/pred1>, ?b)
AND ?b < 5
The source for the query would be overidden by the Java call query.setSource, or from the command line using --data URI.
Relative URIs are interpreted as relative filenames.
The syntax of a source, if not given elsewhere, is detemined bythe extension on the URI. Currently, the type recognized are: