1

我有一个问题,即使经过数小时的搜索,我也找不到合适的解决方案。我想用海龟语法(~8GB)解析一个巨大的 RDF 文档。这就是为什么我选择了 jena riot 提供的流方法,即RDFDataMgr。我正在处理的文件使用了包含空格的无效 URI。这引发了一个致命的异常并且解析停止。我想事先识别那些无效的 URI 并跳过整个语句,因为它无论如何都没用。我已经尝试了apache email archive中提出的解决方案,但是由于在发出三元组之前引发了异常,它不能按预期工作。有谁知道我在哪里弄错了,或者我必须找到其他解决方案吗?请参阅此处的示例代码:

import org.apache.jena.atlas.lib.Sink;
import org.apache.jena.graph.Triple;
import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.system.StreamRDFLib;
import org.apache.jena.riot.system.StreamRDFWrapper;
import org.apache.jena.riot.system.SyntaxLabels;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.out.SinkQuadOutput;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class JenaStreamWrapper extends StreamRDFWrapper {

    public static void main(String... argv) throws FileNotFoundException {
        String filename = "file.ttl";
        OutputStream outputStream = new FileOutputStream(new File("file.nq"));
        StreamRDF filtered = new JenaStreamWrapper(StreamRDFLib.writer(outputStream));
        RDFDataMgr.parse(filtered, filename);
    }

    public JenaStreamWrapper(StreamRDF other) {
        super(other);
    }

    @Override
    public void triple(Triple triple) {
        if ((triple.getSubject().isURI() && triple.getSubject().getURI().contains(" "))
                || (triple.getObject().isURI() && triple.getObject().getURI().contains(" ")))
            System.out.println(triple.getSubject().getURI());
        else
            other.triple(triple);
    }
}

这里有一些从数据集中提取的示例语句:

@prefix gndo: <http://d-nb.info/standards/elementset/gnd#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix dnbt: <http://d-nb.info/standards/elementset/dnb/> .

<http://d-nb.info/gnd/117177040> a gndo:DifferentiatedPerson ;
        owl:sameAs <http://viaf.org/viaf/64153583> ;
        gndo:gndIdentifier "117177040" ;
        gndo:oldAuthorityNumber "(DE-588a)117177040" , "(DE-588a)117177040670 BnF" ;
        owl:sameAs <http://d-nb.info/gnd/117177040670 BnF> ;
        dnbt:deprecatedUri "http://d-nb.info/gnd/117177040670 BnF" ;
        gndo:oldAuthorityNumber "(DE-588)117177040670 BnF" ;
        gndo:variantNameForThePerson "Jourdan, Camille" ;
        gndo:variantNameEntityForThePerson _:node1aj1cbug9x62759112 . 

执行代码时,我收到以下消息:

Exception in thread "main" org.apache.jena.riot.RiotException: [line: 9, col: 55] Bad character in IRI (space): <http://d-nb.info/gnd/117177040670[space]...>
    at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
    at org.apache.jena.riot.lang.LangEngine.raiseException(LangEngine.java:165)
    at org.apache.jena.riot.lang.LangEngine.nextToken(LangEngine.java:108)
    at org.apache.jena.riot.lang.LangTurtleBase.predicateObjectItem(LangTurtleBase.java:286)
    at org.apache.jena.riot.lang.LangTurtleBase.predicateObjectList(LangTurtleBase.java:280)
    at org.apache.jena.riot.lang.LangTurtleBase.triples(LangTurtleBase.java:249)
    at org.apache.jena.riot.lang.LangTurtleBase.triplesSameSubject(LangTurtleBase.java:190)
    at org.apache.jena.riot.lang.LangTurtle.oneTopLevelElement(LangTurtle.java:46)
    at org.apache.jena.riot.lang.LangTurtleBase.runParser(LangTurtleBase.java:89)
    at org.apache.jena.riot.lang.LangBase.parse(LangBase.java:42)
    at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTLang.read(RDFParserRegistry.java:179)
    at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:861)
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:667)
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:637)
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:626)
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:617)
    at zbw.tbl.schemex.additions.JenaStreamWrapper.main(JenaStreamWrapper.java:27)

我感谢任何有用的输入。

4

1 回答 1

0

我也有同样的问题,但只收到警告而不是致命异常。我的耶拿版本是 3.2.0。为了阻止这些警告,我不得不稍微调整一下 Jena 代码。我覆盖了org.apache.jena.riot.tokens.TokenizerText类并将私有静态变量的值AllowSpacesInIRIfalse更改为true

于 2017-05-17T13:09:47.520 回答