Using the Stardog RDF datastore from JRuby
I was playing with the latest Stardog release during lunch - the way to quickly get going with the included Java examples is to create a project (I use IntelliJ, but use your favorite Java IDE) and include all JAR files in lib/ (included all nested directories) and the source under examples/src.
I took the first Java example class ConnectionAPIExample and converted the RDF loading and query part to JRuby (strange formatting to get it to fit the page width):
Output will look like:
6/21/2012 note: I just tried these code snippets with the released version 1.0 of Stardog and the APIs have changed.
I took the first Java example class ConnectionAPIExample and converted the RDF loading and query part to JRuby (strange formatting to get it to fit the page width):
require 'java' Dir.glob("lib/**.jar").each do |fname| require fname end com.clarkparsia.stardog.security.SecurityUtil. setupSingletonSecurityManager() com.clarkparsia.stardog.StardogDBMS.get(). createMemory("test") CONN = com.clarkparsia.stardog.api. ConnectionConfiguration.to("test").connect() CONN.begin() CONN.add().io().format(org.openrdf.rio.RDFFormat::N3). stream(java.io.FileInputStream.new( "examples/data/sp2b_10k.n3")) QUERY = CONN.query("select * where {?s ?p ?o}") QUERY.limit(10) RESULTS = QUERY.executeSelect() while RESULTS.hasNext() do result = RESULTS.next() result.getBindingNames().toArray().each do |obj| puts "#{obj}: #{result.getBinding(obj).getValue().stringValue()}" end puts endThis is mostly just a straight conversion from Java to Ruby. The first few lines enumerate all JAR files and require them. The last part, of interpreting the results, took a few minutes to figure out. I used IntelliJ to explore the result values of class MapBindingSet, looking at available methods to call to get the binding names of the variables in my SPARQL query and the values (as strings) for these three variables for each returned result.
Output will look like:
s: http://localhost/vocabulary/bench/Journal p: http://www.w3.org/2000/01/rdf-schema#subClassOf o: http://xmlns.com/foaf/0.1/Document s: http://localhost/vocabulary/bench/Proceedings p: http://www.w3.org/2000/01/rdf-schema#subClassOf o: http://xmlns.com/foaf/0.1/Document ...If you want to run this bit of code, put it in a file test.rb in the top level Stardog distribution directroy and just run
jruby test.rbI wanted to be able to use Stardog from both JRuby and Clojure. My lunch time hacking today is just a first step.
Comments
Post a Comment