我正在尝试获取在端点 linkedlifedata.com/sparql 上进行的查询的结果集,并且我想要获取的对象是“指示”,它是一个段落。我如何在 netbeans 中打印它?代码是:-
String qs1 = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"+
"PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>"+
"select ?indication"+
"where {"+
"?drug drugbank:indication ?indication."+
"?drug drugbank:genericName ?drugname."+
"filter(regex(?drugname, 'Omalizumab','i'))"+
"}LIMIT 100 ";
com.hp.hpl.jena.query.Query query = QueryFactory.create(qs1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://linkedlifedata.com/sparql", query);
String o;
try{
ResultSet results = qexec.execSelect();
for ( ; results.hasNext() ; )
{
QuerySolution soln = results.nextSolution() ;
o = soln.get("indication").toString();
System.out.println(o);
}
}
finally {
qexec.close();
}
}
我应用了上面的方法。但它显示空指针异常。查询运行良好(在端点上检查)。我之前使用 isLiteral() 函数获得了药物名称(一个小字符串),但是对于作为段落的“指示”,它没有发生。请帮忙,这很紧急。谢谢
最佳答案
在运行它之前我根本没有发现这一点。你的问题是这样的:
"select ?indication"+
"where {"+
选择行末尾没有空格,因此查询实际上是:
... select ?indicationwhere { ... }
因此没有 ?inspiration
的绑定(bind),并且出现空指针异常。在其中插入换行符或空格。
关于java - Sparql 查询,获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552184/