jena.query.ResultSet and jena.query.QuerySolution: hasNext() returns alway false after SPARQL request

最后都变了- 提交于 2019-12-23 06:12:22

问题


I have a problem with receiving SPARQL response. The problem is .... when I use the following source code, rs.hasNext() always return false even though the response shouldn't be empty.

SPARQL Query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?s ?sl <http://dbpedia.org/property/name> ?o ?ol
FROM <http://en.dbpedia.org/20120601/>
WHERE {
    ?s rdfs:label ?sl .
    ?s <http://dbpedia.org/property/name> ?o .
    ?o rdfs:label ?ol .
FILTER (   lang(?sl)= "en"  &&  lang(?ol)= "en"  ) }
LIMIT 100

This query works correctly in DBPedia Endpoint.

This query was executed using Jena as below.

Soucre Code:

...

            QueryEngineHTTP qexec = new QueryEngineHTTP("http://dbpedia.org/sparql", query));
            // "query" is as above
            qexec.addDefaultGraph(http://en.dbpedia.org/20120601/);
            List<QuerySolution> resultSetList = new ArrayList<QuerySolution>();

            ResultSet rs = qexec.execSelect();

            while (rs.hasNext())
                resultSetList.add(rs.next());

            if (!resultSetList.isEmpty()) {
                if (query.contains("?o rdfs:label ?ol")) {
                    func...1
                    func...2
                }
                else {
                    func...3
                    func...4
                }
            }
            else {
                qexec.close();
                break;
            }

...

There is no problem in SPARQL Endpoint because I could get the result from SPARQL Query. However, I couldn't get any result using Jena. As I mentioned, rs.hasNext() always return false even though the response shouldn't be empty. What would be the resolution for that rs.hasNext() doesn't work, and things cannot be added to resultSetList?

I think the way of using Jena is fine... Is there something wrong?


回答1:


You didn't provide a complete and minimal example, so it's hard to see exactly what's going wrong in your code. You query doesn't even parse with Jena, so I'm not sure why you're not simply getting an error, as opposed to an empty result set. Here's a minimal example that uses your query:

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;

public class DBpediaExample {
    public static void main(String[] args) {
        String query = "" +
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "SELECT ?s ?sl <http://dbpedia.org/property/name> ?o ?ol\n" +
                "FROM <http://en.dbpedia.org/20120601/>\n" +
                "WHERE {\n" +
                "  ?s rdfs:label ?sl .\n" +
                "  ?s <http://dbpedia.org/property/name> ?o .\n" +
                "  ?o rdfs:label ?ol .\n" +
                "  FILTER (   lang(?sl)= \"en\"  &&  lang(?ol)= \"en\"  )\n" +
                "}\n" +
                "LIMIT 100";
        ResultSet rs = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query ).execSelect();

        while ( rs.hasNext() ) {
            System.out.println( rs.next() );
        }
    }
}
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " <IRIref> "<http://dbpedia.org/property/name> "" at line 3, column 15.
Was expecting one of:
    <VAR1> ...
    <VAR2> ...
    "from" ...
    "where" ...
    "(" ...
    "{" ...

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:139)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
    at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:358)
    at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:345)
    at DBpediaExample.main(DBpediaExample.java:19)

You can't use a URI reference in the projection variables like you have, even if Virtuoso accepts it. It's not legal SPARQL, and you can check using sparql.org's query validator.

I'm not sure why you're using the FROM in your query, but if you actually put that IRI into to the public endpoint's "Default Data Set Name (Graph IRI)" field, you don't get any results. Perhaps you want to query the standard data set instead?

As an additional issue, you should really be comparing language tags using langMatches.

If you fix those problems, you'd end up with the following query and code, which shows plenty of results.

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;

public class DBpediaExample {
    public static void main(String[] args) {
        String query = "" +
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "SELECT ?s ?sl (<http://dbpedia.org/property/name> as ?p) ?o ?ol\n" +
                "WHERE {\n" +
                "  ?s rdfs:label ?sl .\n" +
                "  ?s <http://dbpedia.org/property/name> ?o .\n" +
                "  ?o rdfs:label ?ol .\n" +
                "  FILTER ( langMatches(lang(?sl),'en') && langMatches(lang(?ol),'en') )\n" +
                "}\n" +
                "LIMIT 100";
        ResultSet rs = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query ).execSelect();

        while ( rs.hasNext() ) {
            System.out.println( rs.next() );
        }
    }
}
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1990–91 NOFV-Oberliga"@en ) ( ?s = <http://dbpedia.org/resource/1990%E2%80%9391_NOFV-Oberliga> ) ( ?o = <http://dbpedia.org/resource/Jens_Dowe> ) ( ?ol = "Jens Dowe"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1. FC Heidenheim"@en ) ( ?s = <http://dbpedia.org/resource/1._FC_Heidenheim> ) ( ?o = <http://dbpedia.org/resource/Kevin_Kraus> ) ( ?ol = "Kevin Kraus"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1905–06 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1905%E2%80%9306_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Carles_Comamala> ) ( ?ol = "Carles Comamala"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1910–11 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1910%E2%80%9311_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Carles_Comamala> ) ( ?ol = "Carles Comamala"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1910–11 FC Barcelona season"@en ) ( ?s = <http://dbpedia.org/resource/1910%E2%80%9311_FC_Barcelona_season> ) ( ?o = <http://dbpedia.org/resource/Francisco_Bru> ) ( ?ol = "Francisco Bru"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1. FC Heidenheim"@en ) ( ?s = <http://dbpedia.org/resource/1._FC_Heidenheim> ) ( ?o = <http://dbpedia.org/resource/Michael_Thurk> ) ( ?ol = "Michael Thurk"@en )
( ?p = <http://dbpedia.org/property/name> ) ( ?sl = "1990–91 NOFV-Oberliga"@en ) ( ?s = <http://dbpedia.org/resource/1990%E2%80%9391_NOFV-Oberliga> ) ( ?o = <http://dbpedia.org/resource/Hilmar_Weilandt> ) ( ?ol = "Hilmar Weilandt"@en )
…


来源:https://stackoverflow.com/questions/23867499/jena-query-resultset-and-jena-query-querysolution-hasnext-returns-alway-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!