【问题标题】:Merging multiple RDF documents into one将多个 RDF 文档合并为一个
【发布时间】:2013-09-23 11:36:22
【问题描述】:

我有两个 RDF 文档:

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

我想将它们合并到一个文件中,例如purl_foaf.rdf。我在 Java 中工作;我怎样才能用 Jena 做到这一点?

【问题讨论】:

  • 一种可能的方法是使用简单的(?)XSLT 样式表,利用 RDF 是 XML 的事实。
  • @StephenC 如果您的 RDF 恰好在 基于 XML 的 RDF 序列化、RDF/XML 中序列化,这可能会起作用,但如果它是其他格式之一,例如 N3 或(更易于阅读的)Turtle。有关为什么不应该尝试将 RDF 作为 XML 使用的更多信息,请参阅one of my past answers that mentions the topic

标签: java rdf jena


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我喜欢 Ian Dickinson's answer,如果我只需要这样做一次,我会使用 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 );
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      以防万一您像我一样寻找rdfcatrdfcat 已被弃用。如果您安装了Jena command line 工具,您只需要使用riot。选项也发生了变化,完整的选项列表是here。从命令行进行基本合并:

      riot --time --output=RDF/JSON city.rdf company.ttl country.rdf > output.js
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多