Merging multiple RDF documents into one

北城以北 提交于 2019-12-22 11:04:58

问题


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?


回答1:


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



回答2:


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 );
        }
    }
}



回答3:


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


来源:https://stackoverflow.com/questions/18958255/merging-multiple-rdf-documents-into-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!