user976083
user976083

Reputation: 51

How to insert/update an RDF file using Jena?

I want to insert a node in my RDF file. I know that there is SPARQL insert/update syntax, but how to do this using Jena. An example will be of great use.

Upvotes: 3

Views: 3510

Answers (3)

RobV
RobV

Reputation: 28675

Use ARQs (Jena's SPARQL processor library) UpdateFactory to create an update and then the UpdateExecutionFactory to create an UpdateProcessor which can evaluate the updates.

It's slightly more involved than this as you also need a dataset to evaluate the updates on. I honestly don't know how easy it is to create a dataset from a single model but can't imagine it being that difficult

Upvotes: 0

heshjse
heshjse

Reputation: 910

try this

   Model m = ModelFactory.createDefaultModel();
        m.read("/Users/heshanjayasinghe/Documents/A-enigmaProject/jena_Enigma/src/jena_enigma/Enigma.RDF", "RDF/XML");
        String NS="http://www.heshjayasinghe.webatu.com/Enigma.RDF#";

        Resource r = m.createResource(NS+"user8");//like subject
        Property p1 =m.createProperty(NS+"lname");
        Property p2 =m.createProperty(NS+"email");
        Property p3 =m.createProperty(NS+"fname");
        Property p4 =m.createProperty(NS+"password");


        r.addProperty(p1, "thathasara", XSDDatatype.XSDstring);
        r.addProperty(p2, "[email protected]", XSDDatatype.XSDstring);
        r.addProperty(p3, "nipun", XSDDatatype.XSDstring);
        r.addProperty(p4, "t123", XSDDatatype.XSDstring);
      //   m.write(System.out,"thurtle");
          m.write(new FileOutputStream("/Users/heshanjayasinghe/Documents/A-enigmaProject/jena_Enigma/src/jena_enigma/Enigma.RDF"), "RDF/XML");

Upvotes: 1

Ian Dickinson
Ian Dickinson

Reputation: 13315

Are you sure that you need to use SPARQL update? In Jena, nodes (resources or literals) aren't in the Model by themselves, but only by virtue of being part of a triple (i.e. Statement) in the model. If you have a Model object in your code, use one of the many variants of addStatement to add a given resource or literal into your graph.

Addendum

After your comment clarifying that you want to add to a file on disk, you could do as RobV says and amend the Model in memory, then write it out again. And this is really the correct way to do it. However, there is a quick-and-dirty workaround you might find helpful. If your file is in Turtle or N-Triples format, you can just append to the end of the file (which, obviously, you can't do in XML). So something along the lines of:

File f = new File( "where.your.file.is" );
FileOutputStream out = new FileOutputStream( f, true );
out.write( ":john :loves :jane.\n" );
out.close();

would work. It's not really recommended, because you run the risk of (a) not having the correct namespace prefixes in place, (b) introducing syntax errors (since you're not using a Jena writer), and (c) creating duplicate triples, but it's sometimes a useful trick in a tight spot. Obviously you can only add information using this technique, not update or delete existing triples.

Directly appending to the end of an N-triples file is a valid and useful technique when collecting large volumes of data from ongoing logging or monitoring applications.

Upvotes: 3

Related Questions