1

I am trying to update local rdf files with SPARQL Update queries using jena arq. Following is the code

GraphStore graphstore = GraphStoreFactory.create();
UpdateRequest req = UpdateFactory.create();
req.add("  PREFIX : myprefixuri  INSERT DATA { GRAPH file:///path-to-file/file.n3 { :person :name 'xyz'. } } ");
UpdateAction.execute(req, graphstore); 

After executing the code it states that query is executed successfully but local file is not updated.

2 Answers 2

3

I think your confusion arises from using the file name as the graph name:

INSERT DATA { GRAPH file:///path-to-file/file.n3

This doesn't insert data into a file named file.n3, it inserts it into a graph named file:///path-to-file/file.n3. That graph will be wherever the graphstore creates it, which, as you have it set up, will be an in-memory object. In order to have your updates persist, you either need to use a graphstore which manages persistent graphs, for example using TDB, or you need to write your graph out to disk after the update. I'm guessing that you want the latter.

Writing out RDF currently uses API methods on a Model, which is a facade for an underlying Jena Graph object but with additional convenience functions, including I/O. So you need to get a reference to your graph from the graph store, wrap it as a model, then write it out. Warning - untested code:

Node graphName = Node.createURI( "file:///path-to-file/file.n3" );
Graph g = graphstore.getGraph( graphName );
Model m = ModelFactory.createModelForGraph( g );
g.write( new FileWriter("/path-to-file/file.n3"), "Turtle" );
2
  • Thanks a million. You are right, I was missing the part of writing the updated graph back to the file. Dec 6, 2012 at 18:33
  • @user1876683 If this resolved your problem don't forget to accept the answer (click the tick mark under the vote count)
    – RobV
    Dec 6, 2012 at 20:21
2

You've created a store and uploaded your file into it, then executed the update over your store. So your store is updated succesfully, but you haven't yet exported the store contents back to file.

2
  • Thanks for the reply but can you please guide me or any pointers to the complete code which executes the sparql updates queries and also writes back to file? Dec 4, 2012 at 22:12
  • 1
    I'm no Jena expert but I assume the answer to that can be found in the Jena documentation. See jena.apache.org/documentation . Dec 4, 2012 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.