简体   繁体   English

耶拿:如何从Java中的SPARQL(Jena)ResultSet获取整数结果?

[英]Jena: How to get Integer result from SPARQL (Jena) ResultSet in Java?

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(). 问题是get()函数将仅使用toString()返回字符串输出。 I need an int output. 我需要一个int输出。 How do I go about doing that? 我该怎么做?

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等。

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

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