Archive for December 13th, 2009

4store and Dan Hanley’s client libgs

Sunday, December 13th, 2009

Find below step by step instructions on how to talk to a 4store KB via the SPARQL-Procotol in Java, using Dan Hanley’s 4store client libs.

First of all you need to have the following installed :
4store
A java compiler (i have Sun’s Java SDK 1.6.x installed on my laptop)
Maven (to compile Dan Hanley’s 4store client libs).

I have all of the code needed for this example placed on my site here : http://mmt.me.uk/examples/4store-java-query-example/ feel free to grab and use it as you wish.

First of all, I should say that I have had to patch the 4store-client-library, as it seems to chomp all of the carriage returns at the end of the lines (“\n”). This is fine you are using the SPARQL-XML-RESULT format, by I am a tad lazy and much prefer working with the tsv format: tsv requires line breaks.

The git diff of my patch looks like so (and again yes it is a tad dirty) :

diff --git a/src/main/java/uk/co/magus/fourstore/client/Store.java b/src/main/java/uk/co/magus/fourstore/clie
index 6652874..097be43 100644
--- a/src/main/java/uk/co/magus/fourstore/client/Store.java
+++ b/src/main/java/uk/co/magus/fourstore/client/Store.java
@@ -349,7 +349,7 @@ public class Store {
String response = "";
String str;
while (null != ((str = in.readLine()))) {
- response += str;
+ response += str+"\n";
}
in.close();
return response;
~
~

This above patch allows me to make use of the TSV output as needed. The patch can be grabbed from here : http://mmt.me.uk/examples/4store-java-query-example/patch.txt.

Ok, now that I have built the 4store-client.jar file with my patch using the instructions in the git repo. I create an example RDF file, and some example java code which will query the 4store KB. All the files can be found on my site http://mmt.me.uk/examples/4store-java-query-example/.

These are the steps which I have taken to get this little demo up and running :
1. Create 4store KB called lame

4s-backend-setup --node 0 --cluster 1 --segments 2 lame

2. Start lame‘s backend (i.e. start the KB)

4s-backend lame

3. Import the example.rdf file into lame‘s backend (i.e. start the KB)

4s-import lame example.rdf

4. Start HTTPD for the KB named lame

4s-httpd -s -1 lame

And then I need to compile the exampleQuery code I knocked together

javac -cp 4store-client-1.0.jar exampleQuery.java

Finally, I run the code like so :

java -cp 4store-client-1.0.jar:. exampleQuery

Resulting in the following output:

Am about to query a sparql endpoint
A user name is:"Bob"
A user name is:"Alice"

I hope this helps!