Reputation: 627
I have got two RDF documents:
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?
Upvotes: 4
Views: 4179
Reputation: 3301
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
Upvotes: 7
Reputation: 85863
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 );
}
}
}
Upvotes: 6
Reputation: 13305
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
Upvotes: 9