如何在java中构建SPARQL查询? [英] How to build SPARQL queries in java?
问题描述
是否有一个库,它能够以编程方式构建SPARQL查询,如JPA中的 CriteriaBuilder
,或构建类似 PreparedStatement <的查询/ code> for SQL?
Is there a library, which is able to build SPARQL queries programmatically like the CriteriaBuilder
in JPA or to build the queries like with a PreparedStatement
for SQL?
类似(对于SQL):用Java构建SQL字符串的最简洁方法
Similar (for SQL): Cleanest way to build an SQL string in Java
推荐答案
您可以使用两种方法在Jena中以编程方式构建查询:语法或代数。在jena wiki中有一个介绍。
You can build queries programmatically in Jena using two methods: syntax or algebra. There's an introduction in the jena wiki.
使用代数你会做类似的事情:
Using the algebra you'd do something like:
Op op;
BasicPattern pat = new BasicPattern(); // Make a pattern
pat.add(pattern); // Add our pattern match
op = new OpBGP(pat); // Make a BGP from this pattern
op = OpFilter.filter(e, op); // Filter that pattern with our expression
op = new OpProject(op, Arrays.asList(Var.alloc("s"))); // Reduce to just ?s
Query q = OpAsQuery.asQuery(op); // Convert to a query
q.setQuerySelectType(); // Make is a select query
(取自维基页面)
它不是 CriteriaBuilder
(也不打算如此),但是有一些方法。你 OpJoin
而不是AND, OpUnion
当你想要OR等时。痛点是我的经验表达:你可能想从字符串中解析它们。
It's not CriteriaBuilder
(nor was it intended to be), but is some of the way there. You OpJoin
rather than AND, OpUnion
when you want to OR, etc. The pain points are expressions in my experience: you probably want to parse them from a string.
这篇关于如何在java中构建SPARQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!