如何在java中构建SPARQL查询?
是否有一个库能够像 JPA 中的 CriteriaBuilder 那样以编程方式构建 SPARQL 查询,或者像 SQL 中的 PreparedStatement 那样构建查询?
类似(对于 SQL): 构建 SQL 字符串的最简洁方法Java
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?
Similar (for SQL): Cleanest way to build an SQL string in Java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用两种方法在 Jena 中以编程方式构建查询:语法或代数。 jena wiki 中有一个介绍。
使用代数,您可以执行以下操作:(
取自 wiki 页面)
它不是
CriteriaBuilder
(也不是有意的),但只是其中的一种方式。当您想要进行 OR 等操作时,您需要使用 OpJoin 而不是 AND、OpUnion。根据我的经验,痛点是表达式:您可能希望从字符串中解析它们。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:
(taken from the wiki page)
It's not
CriteriaBuilder
(nor was it intended to be), but is some of the way there. YouOpJoin
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.Jena 的最新版本添加了
StringBuilder
样式 API,用于构建查询/更新字符串并在需要时对其进行参数化。此类称为
ParameterizedSparqlString
,这是使用它创建查询的示例:
免责声明 - 我是向 Jena 贡献此功能的开发人员,
请参阅参数化 SPARQL 查询的最佳方式是什么? 有关执行此操作的更多讨论跨各种 API。
The recent versions of Jena have added a
StringBuilder
style API for building query/update strings and parameterizing them if desired.This class is called
ParameterizedSparqlString
, here's an example of using it to create a query:Disclaimer - I'm the developer who contributed this functionality to Jena
See What's the best way to parametize SPARQL queries? for more discussion on doing this across various APIs.
我实现了 SPARQL Java - 一种用 Java 编写 SPARQL 查询的 DSL。
它解决了 IDE 自动格式化连接的 SPARQL 查询字符串等问题。
举例来说:
I implemented SPARQL Java - a kind of DSL for writing SPARQL queries in Java.
It solves the problem with IDE's auto formatting of concatenated SPARQL query strings and things like that.
As for example:
我最近开始使用 Sesame 查询生成器。它看起来很有希望,只是它没有提供太多文档,而且我很难找到示例。这是一个可以帮助您入门的简单示例:
请注意,
isLiteral
和cmResource
是我自己的小型静态帮助器类。isLiteral
代表new IsLiteral(new Var("..."))
例如,后者使用我频繁使用的前缀创建 URI。您可能还会对
SPARQLQueryRenderer
感兴趣,它可以将ParsedQuery
转换为String
,这可能会方便进一步使用。如果您最终使用
String(Builder)
方法,我不鼓励您至少查看一下sesame-queryrendered
中的RenderUtils
,它有所有在 URI 周围添加<
>
、转义特殊字符等的便捷方法。I recently started to use Sesame query builder. It looks promising except it doesn't provide much documentation and I struggled to find examples. Here is simple sample which may help you to get started:
Just note that
isLiteral
andcmResource
are my own little static helper classes.isLiteral
stands fornew IsLiteral(new Var("..."))
for example where the latter one create URI with my heavily used prefix.You might be then also interested in
SPARQLQueryRenderer
which can turnParsedQuery
intoString
which may be convenient for further usage.If you end up using
String(Builder)
approach what I discourage you to do have at least a look onRenderUtils
fromsesame-queryrendered
which has all the convenient methods to add<
>
around URIs, escape special characters etc.Eclipse RDF4J 框架(Sesame 的后继者)提供了一个类似于 JDBC 的 Repository API - 它允许您创建一个准备好的查询对象并在执行之前注入变量绑定:
此外,RDF4J 有一个 SparqlBuilder(最初称为 spanqit) - 用于 SPARQL 的 Java DSL,允许您在代码中创建 SPARQL 查询,如下所示:
The Eclipse RDF4J framework (the successor of Sesame) offers a Repository API which is somewhat similar to JDBC - it allows you to create a prepared Query object and inject variable bindings before executing it:
In addition, RDF4J has a SparqlBuilder (originally known as spanqit) - a Java DSL for SPARQL which allows you to create SPARQL queries in code like this:
我刚刚发布了一个测试版项目来做到这一点,名为 Spanqit。
我力求可读性和直观的界面,例如,这里是一些用于创建查询的 Spanqit 语法示例:
查看它,并随时发表评论并提出改进建议!
I have just released a beta project to do just this, called Spanqit.
I strove for readability and an intuitive interface, for example, here is some example Spanqit syntax for creating a query:
Check it out, and feel free to comment and suggest improvements!
Jena 在 Extras 包中提供了 QueryBuilder。
https://jena.apache.org/documentation/extras/querybuilder/index.html
它会做你想做的事。
Jena provides a QueryBuilder in the Extras package.
https://jena.apache.org/documentation/extras/querybuilder/index.html
It does what you want.
您可以使用 Jena 语义框架 (SPARQL 文档)。另请查看此相关问题。遗憾的是,它的语法更接近于 SQLPreparedStatement,而不是 JPA。
You can use the Jena Semantic Framework (SPARQL documentation). Also take a look at this related question. Sadly, its syntax is closer to a SQL PreparedStatement than to the JPA.