DBpedia查询在使用jena时给出了错误

时间:2018-05-21 01:28:43

标签: sparql jena dbpedia

当我在dbpedia.org上运行此查询时

SELECT DISTINCT ?resource ?label ?location 
WHERE 
  { 
    <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . 
    ?resource geo:geometry ?location ; 
                                 rdfs:label ?label . 
    FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) . 
    FILTER( lang( ?label ) = "en" ) 
  }

它产生的结果很好但是同样的查询在使用java jena api时给出了错误。

 String s2 = "SELECT DISTINCT ?resource ?label ?location \r\n" + 
        "WHERE \r\n" + 
        "  { \r\n" + 
        "    <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . \r\n" + 
        "    ?resource geo:geometry ?location ; \r\n" + 
        "                                 rdfs:label ?label . \r\n" + 
        "    FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) . \r\n" + 
        "    FILTER( lang( ?label ) = \"en\" ) \r\n" + 
        "  }\r\n" + 
        "";

    Query query = QueryFactory.create(s2); //s2 = the query above
    QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
    ResultSet results = qExe.execSelect();
    ResultSetFormatter.out(System.out, results, query) ;

它给了我以下错误

Exception in thread "main" org.apache.jena.query.QueryParseException: Line 4, column 49: Unresolved prefixed name: geo:geometry
    at org.apache.jena.sparql.lang.ParserBase.throwParseException(ParserBase.java:521)
    at org.apache.jena.sparql.lang.ParserBase.resolvePName(ParserBase.java:286)
    at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.PrefixedName(SPARQLParser11.java:4857)
    at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.iri(SPARQLParser11.java:4841) and it goes on

1 个答案:

答案 0 :(得分:2)

DBPedia有一些Predefined Namespace Prefixes,其中包括

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

耶拿没有的。因此,您必须在SELECT之前指定使用Jena。即

String s2 = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\r\n" +
            "SELECT DISTINCT ?resource ?label ?location \r\n" + 
            ... etc