简体   繁体   中英

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:

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.

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 :

org.openrdf.query.QueryEvaluationException: org.openrdf.rio.RDFParseException: Expected '.', found '–' [line 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.

You need to creat a subclass of SPARQLRepository to access the HTTPClient (for some reason, the field is 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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