0

将以下格式的 N3 RDF 文件加载到 Sesame Repository 时,是否可以告诉 Sesame '|' 作为分隔符?

下面的三元组由 | 分隔 :

http://article.com/1-3|http://relationship.com/wasGeneratedBy|http://edit.com/comment1-2
4

1 回答 1

3

正如评论中所指出的:您拥有的格式根本不是 N3 语法,因此您无法使用 Sesame 的 N3 解析器上传它。它也不是任何其他标准化格式,因此没有可用于处理它的解析器。

但是,“手动”处理这种格式的文件并自己将其添加到 Sesame 中会相当简单。这可能会奏效:

   try (RepositoryConnection conn = rep.getConnection()) {
      ValueFactory vf = conn.getValueFactory(); 

      File file = new File("/path/to/weirdlyformattedrdffile.txt");

      // open the file for reading
      try (BufferedReader br = new BufferedReader(new FileReader(file))) {
         String line;

         // start a transaction
         conn.begin();

         // read the file line-by-line
         while ((line = br.readLine()) != null) {
            // each line is an RDF triple with a subject, predicate, and object
            String[] triple = line.split("|");

            IRI subject = vf.createIRI(triple[0]);
            IRI predicate = vf.createIRI(triple[1]);
            IRI object = vf.createIRI(triple[2]);

            // add the triple to the database
            conn.add(subject, predicate, object);
         }                   
         // commit the txn when all lines are read
         conn.commit();
      }
   }

当然,如果您的文件还包含 IRI 以外的其他内容(例如文字或空白节点),您将必须包含一些逻辑来区分这些内容,而不是盲目地为所有内容创建 IRI。但这就是您不使用标准化语法格式所付出的代价。

于 2016-05-08T22:38:04.643 回答