我正在使用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();
}
问题是get()函数将仅使用toString()返回字符串输出。我需要一个int输出。我该怎么做呢?
最佳答案
我找到了解决方案。使用如下文字:
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();
}
Literal允许您获取许多数据类型,例如int,float等。