Jena:如何从Java中的SPARQL(Jena)ResultSet获取Integer结果?

时间:2020-12-21 16:47:33

I am querying for integer result using a SPARQL Query:

我使用SPARQL查询查询整数结果:

String qString = 
            "SELECT (COUNT(?S) AS ?C) "+
            "WHERE "+
            "{ "+
            "?S <"+p1+"> ?O ."+
            "?S <"+p2+"> ?O ."+
            "} ";
    Query q = QueryFactory.create(PREFIX+qString);
    QueryExecution qExecution = QueryExecutionFactory.sparqlService(ENDPOINT, q);
    ResultSet qResults = qExecution.execSelect();
    if(qResults.hasNext()){
        QuerySolution thisRow = qResults.next();
        int C = thisRow.get("C").toString();
    }

Problem is that the get() function would return only string outputs using toString(). I need an int output. How do I go about doing that?

问题是get()函数只使用toString()返回字符串输出。我需要一个int输出。我该怎么做呢?

1 个解决方案

#1


5  

I found the solution. Use literals as follows:

我找到了解决方案。使用文字如下:

String qString = 
            "SELECT (COUNT(?S) AS ?C) "+
            "WHERE "+
            "{ "+
            "?S <"+p1+"> ?O ."+
            "?S <"+p2+"> ?O ."+
            "} ";
    Query q = QueryFactory.create(PREFIX+qString);
    QueryExecution qExecution = QueryExecutionFactory.sparqlService(ENDPOINT, q);
    ResultSet qResults = qExecution.execSelect();
    if(qResults.hasNext()){
        QuerySolution thisRow = qResults.next();
        Literal C_12_literal = ((Literal) thisRow.get("C"));
        C_12 = C_12_literal.getInt();
    }

A Literal allows you to get many data types like int, float, etc.

Literal允许您获得许多数据类型,如int,float等。

#1


5  

I found the solution. Use literals as follows:

我找到了解决方案。使用文字如下:

String qString = 
            "SELECT (COUNT(?S) AS ?C) "+
            "WHERE "+
            "{ "+
            "?S <"+p1+"> ?O ."+
            "?S <"+p2+"> ?O ."+
            "} ";
    Query q = QueryFactory.create(PREFIX+qString);
    QueryExecution qExecution = QueryExecutionFactory.sparqlService(ENDPOINT, q);
    ResultSet qResults = qExecution.execSelect();
    if(qResults.hasNext()){
        QuerySolution thisRow = qResults.next();
        Literal C_12_literal = ((Literal) thisRow.get("C"));
        C_12 = C_12_literal.getInt();
    }

A Literal allows you to get many data types like int, float, etc.

Literal允许您获得许多数据类型,如int,float等。