简体   繁体   English

从DBPedia解析数据时,OpenRdf异常

[英]OpenRdf Exception when parsing data from DBPedia

I use OpenRdf with Sparql to gather data from DBPedia but I encounter some errors on the following query ran against the DBPedia Sparql endpoint: 我将OpenRdf与Sparql一起使用以从DBPedia收集数据,但是在针对DBPedia Sparql端点运行的以下查询中遇到一些错误:

CONSTRUCT{ 
    ?battle ?relation ?data . 
} 
WHERE{
  ?battle   rdf:type    yago:Battle100953559 ;  
            ?relation   ?data   .  
  FILTER(?relation != owl:sameAs)
}
LIMIT 1 
OFFSET 18177

I modified the LIMIT and OFFSET to point out the specific result that provokes the problem. 我修改了LIMIT和OFFSET以指出引发问题的特定结果。

The response is this one : 响应是这样的:

@prefix foaf:   <http://xmlns.com/foaf/0.1/> .
@prefix ns1:    <http://en.wikipedia.org/wiki/> .
<http://dbpedia.org/resource/Mongol%E2%80%93Jin_Dynasty_War>    foaf:isPrimaryTopicOf   ns1:Mongol–Jin_Dynasty_War .

The problem is that the ns1:Mongol–Jin_Dynasty_War entity contains a minus sign, therefore I get the following exception when running this query inside a Java application using OpenRdf : 问题是ns1:Mongol–Jin_Dynasty_War实体包含减号,因此在使用OpenRdf的Java应用程序中运行此查询时,出现以下异常:

org.openrdf.query.QueryEvaluationException: org.openrdf.rio.RDFParseException: Expected '.', found '–' [line 3] org.openrdf.query.QueryEvaluationException:org.openrdf.rio.RDFParseException:预期为“。”,找到为“ –” [第3行]

Is there any way to circumvent this problem ? 有什么办法可以解决这个问题?

Thanks ! 谢谢 !

To help other users who might encounter the same problem, I'll post here the way to set the preferred output format for Graph Queries using OpenRDF v2.7.x. 为了帮助可能遇到相同问题的其他用户,我将在此处发布使用OpenRDF v2.7.x为图形查询设置首选输出格式的方法。

You need to creat a subclass of SPARQLRepository to access the HTTPClient (for some reason, the field is protected . 您需要创建SPARQLRepository的子类来访问HTTPClient(由于某些原因,该字段为protected

public class NtripleSPARQLRepository extends SPARQLRepository {
    public NtripleSPARQLRepository(String endpointUrl) {
        super(endpointUrl);
        this.getHTTPClient().setPreferredRDFFormat(RDFFormat.NTRIPLES);
    }
}

The you just need to create a new Instance of this class : 您只需要创建此类的新实例:

NtripleSPARQLRepository repository = new NtripleSPARQLRepository(service);
RepositoryConnection connection = new SPARQLConnection(repository);
Query query = connection.prepareQuery(QueryLanguage.SPARQL, "YOUR_QUERY");

If you are querying a Virtuoso server, then you are probably encountering sloppiness in the implementation of Virtuoso. 如果要查询Virtuoso服务器,则在Virtuoso的实现中可能会遇到草率的情况。 I have seen this when getting XML results (vertical tab in output but only XML 1.0) and most recently in JSON results (\\U escape for characters not in Basic Multilingual Plane). 我在获取XML结果(输出中的垂直选项卡,但仅XML 1.0)以及最新的JSON结果(\\ U转义表示“基本多语言平面”中的字符)时看到了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM