C.N*_*eal 2 java rdf sparql jena
我试图直接从Oracle数据库的SPARQL查询输出RDF/XML文件.由于我在ResultSet对象中验证了结果,因此查询工作正常.
但是,我不确定如何从那里开始.我想我想为每个QuerySolution创建一个Statement,然后将它添加到Model中.但是我无法想出办法,因为我无法找到获取谓词值的方法.
任何帮助将不胜感激,并暗示我是否走正确的道路.
QueryExecution qe = QueryExecutionFactory.create(query, oracleSemModel) ;
ResultSet results = qe.execSelect();
Model model = ModelFactory.createDefaultModel();
while (results.hasNext())
{
QuerySolution result = results.next();
Resource subject = result.getResource("s"); // get the subject
Property predicate = result.getProperty("p"); // <-- THIS FUNCTION DOESN'T EXIST
RDFNode object = result.get("o"); // get the object
Statement stmt = ResourceFactory.createStatement(subject, predicate, object);
model.add(stmt);
}
model.write(System.out, "RDF/XML-ABBREV");
Run Code Online (Sandbox Code Playgroud)
您尚未显示查询,但可能是以下形式:
SELECT ?s ?p ?o
WHERE {
...
}
Run Code Online (Sandbox Code Playgroud)
如果您替换SELECT为CONSTRUCT以下内容,您想要实现的目标很简单:
CONSTRUCT { ?s ?p ?o }
WHERE {
...
}
Run Code Online (Sandbox Code Playgroud)
CONSTRUCT通过获取三重模板并将每个解决方案绑定到它,然后将其添加到结果模型,使(构造)模型成为结果.在你的情况下,这个模板很简单{ ?s ?p ?o },即使用?s作为主题等.但是可以使用更复杂的模式,只需在查询结果中的值的位置写入您期望的RDF.
最终代码:
QueryExecution qe = QueryExecutionFactory.create(query, oracleSemModel) ;
Model model = qe.execConstruct();
// there is no step 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |