SPARQL-Gremlin is a compiler used to transform SPARQL queries into Gremlin traversals. It is based on the Apache Jena SPARQL processor ARQ, which provides access to a syntax tree of a SPARQL query.
The current version of SPARQL-Gremlin only uses a subset of the features provided by Apache Jena. The examples below show each implemented feature.
The project contains a console application that can be used to compile SPARQL queries and evaluate the resulting Gremlin traversals. For usage examples simply run ${PROJECT_HOME}/bin/sparql-gremlin.sh
.
To use Gremlin-SPARQL as a Gremlin shell plugin, run the following commands (be sure sparql-gremlin-xyz.jar
is in the classpath):
gremlin> :install com.datastax sparql-gremlin 0.1
==>Loaded: [com.datastax, sparql-gremlin, 0.1]
gremlin> :plugin use datastax.sparql
==>datastax.sparql activated
Once the plugin is installed and activated, establish a remote connection to execute SPARQL queries:
gremlin> :remote connect datastax.sparql graph
==>SPARQL[graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]]
gremlin> :> SELECT ?name ?age WHERE { ?person v:name ?name . ?person v:age ?age }
==>[name:marko, age:29]
==>[name:vadas, age:27]
==>[name:josh, age:32]
==>[name:peter, age:35]
SPARQL-Gremlin supports the following prefixes to traverse the graph:
Prefix | Purpose |
---|---|
|
out-edge traversal |
|
property traversal |
|
property-value traversal |
Note that element IDs and labels are treated like normal properties, hence they can be accessed using the same pattern:
SELECT ?name ?id ?label WHERE { ?element v:name ?name . ?element v:id ?id . ?element v:label ?label }
person
.SELECT * WHERE {
?person v:label "person"
}
name
and age
for each person
vertex.SELECT ?name ?age
WHERE {
?person v:label "person" .
?person v:name ?name .
?person v:age ?age
}
SELECT ?name ?age
WHERE {
?person v:label "person" .
?person v:name ?name .
?person v:age ?age .
?person e:created ?project
}
SELECT ?name ?age
WHERE {
?person v:label "person" .
?person v:name ?name .
?person v:age ?age .
?person e:created ?project .
FILTER (?age > 30)
}
SELECT DISTINCT ?name
WHERE {
?person v:label "person" .
?person e:created ?project .
?project v:name ?name .
FILTER (?age > 30)
}
SELECT DISTINCT ?name
WHERE {
?person v:label "person" .
?person e:created ?project .
?project v:name ?name .
?project v:lang ?lang .
FILTER (?age > 30 && ?lang == "java")
}
SELECT ?name
WHERE {
?person v:label "person" .
?person v:name ?name .
FILTER EXISTS { ?person e:created ?project }
}
SELECT ?name
WHERE {
?person v:label "person" .
?person v:name ?name .
FILTER NOT EXISTS { ?person e:created ?project }
}