Jena有一个内置的命令行实用程序来执行此操作:rdfcat.因此,要将这两个RDF文件连接在一起并将结果作为Turtle写入文件purl_foaf.rdf,请从命令行执行以下操作.它应该在一行上,但为了便于阅读,我将其拆分:
rdfcat -out Turtle "http://dublincore.org/2012/06/14/dcterms.rdf" \
"http://xmlns.com/foaf/spec/index.rdf" > purl_foaf.rdf
Run Code Online (Sandbox Code Playgroud)
我喜欢Ian Dickinson的答案,如果我只需要这样做一次,我就会使用Jena的rdfcat.您提到需要在Java中执行此操作,因此可能不适合使用命令行工具.使用Jena API仍然很容易.如果你只有两个模型,你可以从这两个模型创建一个UnionModel,或者如果你有更多(可能这两个问题只是一个简化的案例,你实际上需要处理更多),你可以创建一个新的模型保持所有三元组,并将两个模型中的三元组添加到新模型中.这是显示每种方法的代码:
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 );
}
}
}
Run Code Online (Sandbox Code Playgroud)
以防万一你正在寻找rdfcat像我一样的人,rdfcat已被弃用。如果您安装了Jena 命令行工具,则只需使用riot. 选项也发生了变化,完整的选项列表在这里。从命令行进行基本合并:
riot --time --output=RDF/JSON city.rdf company.ttl country.rdf > output.js
Run Code Online (Sandbox Code Playgroud)