gpt4 book ai didi

java - Jena:如何从 Java 中的 SPARQL (Jena) 结果集中获取整数结果?

转载 作者:行者123 更新时间:2023-12-03 07:28:35 26 4
gpt4 key购买 nike

我正在使用 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 等。

关于java - Jena:如何从 Java 中的 SPARQL (Jena) 结果集中获取整数结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com