2

I am querying for integer result using a SPARQL Query:

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?

besmirched
  • 81
  • 5
  • The answer to [get latitude and longitude of a place dbpedia](http://stackoverflow.com/q/16737653/1281433) has an example of extracting both the lexical forms and the Java floats from some xsd:float literals in a result set. – Joshua Taylor Dec 17 '14 at 15:56

1 Answers1

6

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.

besmirched
  • 81
  • 5