3 - SPARQL queries
Learn to query RDF.
SPARQL (a recursive acronym for SPARQL Protocol and RDF Query Language) is the query language to retrieve and manipulate data in an RDF database. SPARQL combines elements from SQL queries with the RDF triple patterns. A SPARQL query applies triple patterns (subject, predicate, object) that can be used to match against the RDF data. SPARQL allows to read and to write to the database. It is primarily a query language, but it also supports operations for modifying RDF data through specific query forms and protocols.
In this article I try to discuss some of the basic search functions. I am using as a source SPARQL Query Language: Basic Concepts and Features sparql.dev.
Essential elements for queries are SELECT, WHERE, and FILTER clauses.
SELECT
The SELECT clause specifies which variables will returned by the query as results. The following query retrieves all the predicates and objects associated with the subject “Maria”:
SELECT ?predicate ?object
WHERE {
<http://example.org/Maria> ?predicate ?object
}
Query elements that start with a question mark are the variables. So, in this case we have the variables ?predicate and ?object.
WHERE
The Where clause allows to add constraints to the query in order to return data the meets certain criteria. SPARQL uses a SELECT … WHERE { … } structure. Variables start with ?.
The WHERE clause is used to specify the triple patterns to match against the RDF data. For example, the following query retrieves all triples where the subject is “Maria” and the object is “project3”:
SELECT ?predicate
WHERE {
<http://example.org/Maria> ?predicate <http://example.org/project3>
}
In this query, the triple pattern http://example.org/Maria ?predicate <http://example.org/project3 > matches against all triples where the subject is “Maria” and the object is “project3”.
FILTER
The Filter clause ensures that only data is returned that meets the filter criteria. It works as a filter on the amount data to be returned. The FILTER clause is used to apply additional constraints to the query results. For example, the following query retrieves all predicates where the subject is “Maria” and the object is a string that contains the word “Community Voices”:
SELECT ?predicate
WHERE {
<http://example.org/Maria> ?predicate ?object
FILTER (CONTAINS(str(?object), "Community Voices"))
}
In this query, the FILTER clause applies a constraint to the ?object variable, ensuring that it contains the words “Community Voices”.
ORDER BY
The ORDER BY clause is used to sort the query results based on a specific variable. For example, the following query retrieves all predicates where the subject is “Maria” and orders the results by the predicate:
SELECT ?predicate
WHERE {
<http://example.org/Maria> ?predicate ?object
}
ORDER BY ?predicate
In this query, the ORDER BY clause sorts the results by the ?predicate variable in ascending order.
Run SPARQL
To run SPARQL queries I use Apache Fuseki. This runs as a server on my local machine while accessing the database I am workng on.
First step is to install Fuseki. I run a Debian based Linux distribution, and I downloaded the latest Fuseki distribution from Apache.
wget https://downloads.apache.org/jena/binaries/apache-jena-fuseki-6.2.0.tar.gz
Then:
tar -xzf apache-jena-fuseki-*.tar.gz
cd apache-jena-fuseki-*
Suppose my Turtle file is:
~/Development/rdf-course/cultural-graph.ttl
You can start Fuseki with a dataset called cultural:
./fuseki-server --file ~/Development/rdf-course/cultural-graph.ttl /cultural
Now Fuseki is running locally.
Practical work
I use an RDF/SPARQL environment to query the graph I made during previous lessons. Here are some examples of basic queries:
Which organisations exist?
PREFIX ex: <https://example.org/kg/>
PREFIX schema: <https://schema.org/>
SELECT ?organisation
WHERE { ?organisation a schema:Organisation .}
Which projects exist?
PREFIX ex: <https://example.org/kg/>
PREFIX schema: <https://schema.org/>
SELECT ?project
WHERE { ?project a ex:Project .}
Which organisations participate in which projects?
PREFIX ex: <https://example.org/kg/>
PREFIX schema: <https://schema.org/>
SELECT ?organisation ?project
WHERE { ?project ex:hasPartner ?organisation .}
Which people work for which organisations?
PREFIX ex: <https://example.org/kg/>
PREFIX schema: <https://schema.org/>
SELECT ?person ?name ?organisation
WHERE { ?person ex:worksAt ?organisation ;
schema:name ?name .}
Which projects belong to which organisation - just return the names?
PREFIX ex: <https://example.org/kg/>
PREFIX schema: <https://schema.org/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?projectName ?organisationName
WHERE { ?project ex:hasPartner ?organisation ;
dcterms:title ?projectName.
?organisation schema:name ?organisationName .}
Which projects started after a particular date?
Example:
PREFIX ex: <https://example.org/kg/>
SELECT ?project ?organisation
WHERE { ?project ex:hasPartner ?organisation .}