![](/img/trans.png)
[英]Sparql Select Query works on Sesame-Workbench but not on Java
[英]Export SPARQL query results into json file in sesame
我需要使用芝麻将SPARQL查询结果导出到JSON。 我应该使用SPARQLResultsJSONWriter
类吗? 如何实现(用Java)?
Sesame Repository API用户文档中对此进行了实际解释,并提供了示例代码。
但是,重申一下:使用RepositoryConnection.prepareTupleQuery准备查询后,可以通过两种方式评估返回的TupleQuery对象:一种是通过调用validate evaluate()
,在这种情况下,评估方法将返回TupleQueryResult对象。 另一种是通过调用evaluate(TupleQueryResultHandler)
并将其传递给TupleQueryResultHandler实例,该实例的SPARQLResultJSONWriter是其子类。 因此,您需要做的就是将各个部分放在一起,就像这样:
RepositoryConnection conn = rep.getConnection();
try {
// prepare the query
String queryString = "SELECT * WHERE {?s ?p ?o . }";
TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// open a file to write the result to it in JSON format
OutputStream out = new FileOutputStream("/path/to/output.json");
TupleQueryResultHandler writer = new SPARQLResultJSONWriter(out);
// execute the query and write the result directly to file
query.evaluate(writer);
}
finally {
conn.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.