General material on SPARQL can be found on the MASWS Wiki SPARQL/ARQ page.
ARQ-2.8.8 should also be available on DICE in the directory /usr/share/java/arq/bin/. If you want to use this version, you will have to set a different environmental variable, namely ARQROOT:
export ARQROOT=/usr/share/java/arq
export PATH="${ARQROOT}/bin:${PATH}"
However, these notes all work for the earlier version of ARQ available /usr/share/java/jena/bin/.
Here’s an example of a simple SPARQL query (downloadable as example-01.rq):
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
FROM <http://homepages.inf.ed.ac.uk/ewan/foaf.n3>
WHERE {
?person1 foaf:knows ?person2 .
?person1 foaf:name ?name1 .
?person2 foaf:name ?name2 .
}
Most of this should be familiar to you, but the FROM clause may be new. This says that the query should be run against the RDF data to be found at http://homepages.inf.ed.ac.uk/ewan/foaf.n3. Note that the URI has to be addressable via HTTP when the query is executed.
To call the ARQ SPARQL query engine using the SPARQL query in example-01.rq, we use an instruction like the following on the command-line,
dice:~> arq --query example-01.rq
Given my FOAF file, arq will print the following output to the terminal:
---------------------------------
| name1 | name2 |
=================================
| "Ewan Klein" | "Harry Halpin" |
---------------------------------
We are allowed to have more than one FROM clause in a query, and the resulting graphs are merged. This is shown in the next example (downloadable as example-02.rq), where we query both my FOAF file and Harry Halpin’s.
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
FROM <http://homepages.inf.ed.ac.uk/ewan/foaf.n3>
FROM <http://www.ibiblio.org/hhalpin/foaf.rdf>
WHERE {
?person1 foaf:name ?name1 ;
foaf:knows [ foaf:name ?name2 ];
}
As you can observe here, SPARQL triple patterns allow the same abbreviatory syntax as we have already seen for N3 / Turtle.
Running the query gives the following result set:
--------------------------------------
| name1 | name2 |
======================================
| "Harry Halpin" | "Daniel Weitzner" |
| "Harry Halpin" | "Tim Berners-Lee" |
| "Harry Halpin" | "Dan Connolly" |
| "Harry Halpin" | "Ian Davis" |
| "Harry Halpin" | "Paolo Bouquet" |
| "Ewan Klein" | "Harry Halpin" |
--------------------------------------
The next query is run against a couple of data files, namely knows.n3 and cafes.n3. In this case, I want to recover cafes that are loved by people who know someone or else who someone knows (recall that we are not treating foaf:knows as symmetric). In order to match these two alternatives, I use the UNION keyword, as shown here (downloadable as example-03.rq):
PREFIX : <http://inf.ed.ac.uk/ont#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?cafe ?x
WHERE
{
?cafe :lovedBy ?x .
{
?x foaf:knows ?y .
}
UNION
{
?y foaf:knows ?x .
}
}
Notice that I don’t have a FROM clause in the SPARQL query. In order to get the data, I can specify one or more local files on the command line using the --data option:
arq --query=example-03.rq --data=../rdf/knows.n3 --data=../rdf/cafes.n3
Here are the results:
--------------------
| cafe | x |
====================
| :vittoria | :stu |
| :pyard | :amy |
| :aroast | :stu |
| :ebagel | :amy |
| :ebagel | :bea |
| :ebagel | :bea |
--------------------
As you can see, there’s a duplicate line at the bottom of the results. To eliminate this, we can use the distinct keyword.
PREFIX : <http://inf.ed.ac.uk/ont#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?cafe ?x
WHERE
{
?cafe :lovedBy ?x .
{
?x foaf:knows ?y .
}
UNION
{
?y foaf:knows ?x .
}
}