..

4store and Dan Hanley’s client libgs

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 : https://mmt.me.uk/blog/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 : https://mmt.me.uk/blog/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 https://mmt.me.uk/blog/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
<br /> 4s-backend-setup --node 0 --cluster 1 --segments 2 lame<br />

2. Start lame‘s backend (i.e. start the KB)
<br /> 4s-backend lame<br />

3. Import the example.rdf file into lame‘s backend (i.e. start the KB)
<br /> 4s-import lame example.rdf<br />

4. Start HTTPD for the KB named lame
<br /> 4s-httpd -s -1 lame<br />

And then I need to compile the exampleQuery code I knocked together
<br /> javac -cp 4store-client-1.0.jar exampleQuery.java<br />

Finally, I run the code like so :
<br /> java -cp 4store-client-1.0.jar:. exampleQuery<br />

Resulting in the following output:
<br /> Am about to query a sparql endpoint<br /> A user name is:"Bob"<br /> A user name is:"Alice"<br />

I hope this helps!