Skip to content

Instantly share code, notes, and snippets.

@rbramley
Created January 7, 2014 17:01
Show Gist options
  • Save rbramley/8302522 to your computer and use it in GitHub Desktop.
Save rbramley/8302522 to your computer and use it in GitHub Desktop.
An executable Groovy script to create and query some simple RDF statements within a Neo4j database using the Blueprints GraphSail ouplementation. Based on sample code from the Tinkerpop wiki: https://github.com/tinkerpop/blueprints/wiki/Sail-Ouplementation It sources dependencies from Maven Central using Grape.
@Grab(group='com.tinkerpop.blueprints', module='blueprints-neo4j-graph', version='2.4.0')
@Grab(group='com.tinkerpop.blueprints', module='blueprints-graph-sail', version='2.4.0')
@Grab(group='org.openrdf.sesame', module='sesame-repository-sail', version='2.7.8')
@Grab(group='org.openrdf.sesame', module='sesame-queryparser-sparql', version='2.7.8')
import com.tinkerpop.blueprints.*
import com.tinkerpop.blueprints.oupls.sail.GraphSail
import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph
import info.aduna.iteration.*
import org.openrdf.model.*
import org.openrdf.query.*
import org.openrdf.query.impl.EmptyBindingSet
import org.openrdf.query.parser.sparql.*
import org.openrdf.sail.*
def graph = new Neo4jGraph("/tmp/neotest")
graph.setCheckElementsInTransaction(true)
def sail = new GraphSail(graph)
sail.initialize()
def sc = sail.getConnection()
def vf = sail.getValueFactory()
sc.begin()
sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#knows"), vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com"))
sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("marko"), vf.createURI("http://tinkerpop.com"))
sc.addStatement(vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("josh"), vf.createURI("http://tinkerpop.com"))
sc.commit()
println("get statements: ?s ?p ?o ?g")
CloseableIteration<? extends Statement, SailException> results = sc.getStatements(null, null, null, false)
while(results.hasNext()) {
println(results.next())
}
println("\nget statements: http://tinkerpop.com#3 ?p ?o ?g")
results = sc.getStatements(vf.createURI("http://tinkerpop.com#3"), null, null, false)
while(results.hasNext()) {
println(results.next())
}
def parser = new SPARQLParser()
CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults
def queryString = "SELECT ?x ?y WHERE { ?x <http://tinkerpop.com#knows> ?y }"
def query = parser.parseQuery(queryString, "http://tinkerPop.com")
println("\nSPARQL: " + queryString)
sparqlResults = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false)
while (sparqlResults.hasNext()) {
println(sparqlResults.next())
}
// normal graph operations
println()
for (Vertex v : graph.getVertices()) {
println("------")
println(v)
for (String key : v.getPropertyKeys()) {
println(key + "=" + v.getProperty(key))
}
}
for (Edge e : graph.getEdges()) {
System.out.println("------")
System.out.println(e)
for (String key : e.getPropertyKeys()) {
println(key + "=" + e.getProperty(key))
}
}
sc.close()
graph.shutdown()
sail.shutDown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment