4

I have got two RDF documents:

  1. http://dublincore.org/2012/06/14/dcterms.rdf
  2. http://xmlns.com/foaf/spec/index.rdf

I would like to merge them into one file, e.g., purl_foaf.rdf. I am working in Java; how can I do this with Jena?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Petr Dušek
  • 627
  • 4
  • 12
  • 26
  • 1
    One possible approach would be to do it with a simple(?) XSLT stylesheet, exploiting the fact that RDF is XML. – Stephen C Sep 23 '13 at 11:48
  • 2
    @StephenC That might work if your RDF already happens to be serialized in _the XML based serialization of RDF_, RDF/XML, but it doesn't help at all if it's one of the other formats, such as N3 or (the much more human readable) Turtle. For some more information about why you shouldn't try to work with RDF as XML, see [one of my past answers that mentions the topic](http://stackoverflow.com/a/17052385/1281433). – Joshua Taylor Sep 23 '13 at 13:22

3 Answers3

9

Jena has a built-in command line utility to do this: rdfcat. So, to join those two RDF files together and write the results as Turtle in the file purl_foaf.rdf, do the following from your command line. It should be on one line, but I've split it up for readability:

rdfcat -out Turtle "http://dublincore.org/2012/06/14/dcterms.rdf" \
   "http://xmlns.com/foaf/spec/index.rdf" > purl_foaf.rdf
Ian Dickinson
  • 12,875
  • 11
  • 40
  • 67
6

I like Ian Dickinson's answer, and if I only needed to do this once, I'd use Jena's rdfcat. You mention that you need to do this in Java though, so perhaps a command line tool isn't appropriate. This is still pretty easy to do with the Jena API. If you have just two models, you can create a UnionModel from the two, or if you have more (perhaps the two in the question are just a reduced case, and you actually need to handle more), you can just create a new model to hold all the triples, and add the triples from the two models to the new one. Here's code that shows each approach:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ManualRDFCat {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        final Model dcterms = ModelFactory.createDefaultModel().read( "http://dublincore.org/2012/06/14/dcterms.rdf" );
        final Model foafIndex = ModelFactory.createDefaultModel().read( "http://xmlns.com/foaf/spec/index.rdf" );

        // If you only have two models, you can use Union model.
        final Model union = ModelFactory.createUnion( dcterms, foafIndex );
        try ( final OutputStream out1 = new FileOutputStream( new File( "/tmp/purl_foaf1.rdf" )) ) {
            union.write( out1, "Turtle", null );
        }

        // In general, though, it's probably better to just create a new model to 
        // hold all the triples, and to add the triples to that model.
        final Model blob = ModelFactory.createDefaultModel();
        for ( final Model part : new Model[] { dcterms, foafIndex } ) {
            blob.add( part );
        }
        try ( final OutputStream out2 = new FileOutputStream( new File( "/tmp/purl_foaf2.rdf" )) ) {
            blob.write( out2, "RDF/XML-ABBREV", null );
        }
    }
}
Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
6

Just in case you were looking for rdfcat like me, rdfcat has been deprecated. If you have Jena command line tools insalled, you just need to use riot. The options have also changed, full list of options are here. Basic merging from the command line:

riot --time --output=RDF/JSON city.rdf company.ttl country.rdf > output.js
Artemis
  • 3,271
  • 2
  • 20
  • 33