Hi David,
What version of Jena do you talk about ?
I use Jena 2.7.3 to do this:
UpdateRequest update = UpdateFactory.create("insert data.....");
UpdateProcessRemote riStore = (UpdateProcessRemote)
UpdateExecutionFactory.createRemote(update, "http://myendpointuri");
riStore.execute();
To insert data from a servlet in a remote fuseki entpoint, and it works
just fine.
Met Vriendelijke Groet / With Kind Regards
Bart van Leeuwen
@semanticfire
##############################################################
# netage.nl
# http://netage.nl
# Enschedepad 76
# 1324 GJ Almere
# The Netherlands
# tel. +31(0)36-5347479
##############################################################
From: David Moss <[email protected]>
To: [email protected],
Date: 20-09-2012 00:17
Subject: remote sparql insert from jena
I've been banging my head against the wall until the early hours trying to
make Jena post an insert to a sparql endpoint.
I notice there is no code on the net to use as a guide on this topic.
Initially I thought my server wasn't responding to update requests, but I
installed fuseki and set it it accept updates from the command line. Still
no joy. I could send updates with fuseki's web form, but not from Java
using Jena.
The code I tried to use from Jena was:
String update = "http://localhost:3030/ds/update";
String query = "INSERT {<a> <b> <c>} WHERE {?s ?p ?o}";
UpdateRequest u = UpdateFactory.create();
u.add(query);
UpdateProcessRemote p = new UpdateProcessRemote(u, update);
p.execute();
No matter what I tried the code would fail deep in the processing of
p.execute()
I even tried writing my own html page with a form to send the query, just
to prove it would be accepted and processed by fuseki.
It was.
The error dump from the above code is as follows:
******************
0 [AWT-EventQueue-0] INFO jenatest.jenathing - creating jenathing
Jenathing created
3382 [AWT-EventQueue-0] DEBUG com.hp.hpl.jena.sparql.mgt.
ARQMgt - Register MBean: com.hp.hpl.jena.sparql.system:type=Context
3382 [AWT-EventQueue-0] DEBUG com.hp.hpl.jena.sparql.mgt.ARQMgt -
Register
MBean: com.hp.hpl.jena.sparql.system:type=Engine
3499 [AWT-EventQueue-0] DEBUG com.hp.hpl.jena.sparql.mgt.ARQMgt -
Register
MBean: com.hp.hpl.jena.sparql.system:type=SystemInfo
3501 [AWT-EventQueue-0] DEBUG com.hp.hpl.jena.sparql.mgt.ARQMgt -
Register
MBean: com.hp.hpl.jena.sparql.system:type=SystemInfo
3502 [AWT-EventQueue-0] DEBUG com.hp.hpl.jena.sparql.mgt.ARQMgt -
Register
MBean: org.openjena.riot.system:type=SystemInfo
4018 [AWT-EventQueue-0] DEBUG org.openjena.riot.web.HttpOp - [1] POST
http://localhost:3030/ds/update
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory
at
org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:182)
at
org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:150)
at org.openjena.riot.web.HttpOp.execHttpPost(HttpOp.java:206)
at org.openjena.riot.web.HttpOp.execHttpPost(HttpOp.java:154)
at org.openjena.riot.web.HttpOp.execHttpPost(HttpOp.java:128)
at
com.hp.hpl.jena.sparql.modify.UpdateProcessRemote.execute(UpdateProcessRemote.java:60)
at jenatest.jenathing.doUpdate(jenathing.java:65)
*************************
and this seems to be the culprit:
<init>(AbstractHttpClient.java:182)
when it gets to line 206 in HttpOp.java
// Execute
HttpClient httpclient = new DefaultHttpClient();
it hangs for a bit and then never actually executes the subsequent lines:
httppost.setEntity(provider) ;
HttpResponse response = httpclient.execute(httppost) ;
httpResponse(id, response, baseIRI, handlers) ;
httpclient.getConnectionManager().shutdown();
it skips over these and
} catch (IOException ex)
{
ex.printStackTrace(System.err) ;
}
straight to the finally clause:
finally { closeEntity(provider) ; }
}
So it doesn't work and the problem seems to be deeply hidden within the
Apache stuff.
Eventually after staying up till past 3am and missing a days work today, I
sidestepped the issue entirely and wrote (or rather cut-and-pasted) an
update routine that ignored the Jena stuff altogether. Not my preferred
option, but enough is enough.
I replaced the code above with:
String update = "http://localhost:3030/ds/update";
String query = "INSERT {<a> <b> <c>} WHERE {?s ?p ?o}";
postthing p = new postthing();
p.setEndpoint(update);
p.setUpdateString(query);
try {
p.execute();
}
catch (Exception e) {
logger.error(e.toString());
}
where the postthing is defined as:
******************************
package jenatest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
/**
*
* @author admos0
*/
public class postthing {
private String updateString = "";
private String endpoint = "";
postthing() {
// empty constructor
}
postthing(String input) {
this.updateString = input;
}
/**
* @param set the endpoint
*/
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
/**
* @return the endpoint
*/
public String getEndpoint() {
return this.endpoint;
}
/**
* @method execute the update
*/
void execute() throws Exception {
try {
if (this.endpoint.equals("")) throw new Exception("No endpoint
specified");
if (this.updateString.equals("")) throw new Exception("no
update string specified");
// Construct data
String data = URLEncoder.encode("request", "UTF-8") + "=" +
URLEncoder.encode(this.getUpdateString(), "UTF-8");
// Send data
URL url = new URL(endpoint);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new
OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) { throw e;
}
}
/**
* @return the updateString
*/
public String getUpdateString() {
return updateString;
}
/**
* @param updateString the updateString to set
*/
public void setUpdateString(String updateString) {
this.updateString = updateString;
}
}
***********************
Now it works. I can insert triples into my sparql update endpoint.
There has to be a deep bug in the Apache routines that come with Jena that
thwarted my previous efforts and I hope someone notices this and fixes it.
In the meantime I'm happy that I have something that works, and I hope
that
by posting here the recipe for updating a remote sparql endpoint will
become available and more people will take up Jena and Fuseki.
David Moss