如何在java中构建SPARQL查询?

发布于 2024-12-02 08:01:52 字数 238 浏览 0 评论 0原文

是否有一个库能够像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

煞人兵器 2024-12-09 08:01:52

您可以使用两种方法在 Jena 中以编程方式构建查询:语法或代数。 jena wiki 中有一个介绍

使用代数,您可以执行以下操作:(

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

取自 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:

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

(taken from the wiki page)

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.

流殇 2024-12-09 08:01:52

Jena 的最新版本添加了 StringBuilder 样式 API,用于构建查询/更新字符串并在需要时对其进行参数化。

此类称为 ParameterizedSparqlString,这是使用它创建查询的示例:

ParameterizedSparqlString queryStr = new ParameterizedSparqlString();
queryStr.setNSPrefix("sw", "http://skunkworks.example.com/redacted#");
queryStr.append("SELECT ?a ?b ?c ?d");
queryStr.append("{");
queryStr.append("   ?rawHit sw:key");
queryStr.appendNode(someKey);
queryStr.append(".");
queryStr.append("  ?rawHit sw:a ?a .");
queryStr.append("  ?rawHit sw:b ?b .");
queryStr.append("  ?rawHit sw:c ?c . ");
queryStr.append("  ?rawHit sw:d ?d .");
queryStr.append("} ORDER BY DESC(d)");

Query q = queryStr.asQuery();

免责声明 - 我是向 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:

ParameterizedSparqlString queryStr = new ParameterizedSparqlString();
queryStr.setNSPrefix("sw", "http://skunkworks.example.com/redacted#");
queryStr.append("SELECT ?a ?b ?c ?d");
queryStr.append("{");
queryStr.append("   ?rawHit sw:key");
queryStr.appendNode(someKey);
queryStr.append(".");
queryStr.append("  ?rawHit sw:a ?a .");
queryStr.append("  ?rawHit sw:b ?b .");
queryStr.append("  ?rawHit sw:c ?c . ");
queryStr.append("  ?rawHit sw:d ?d .");
queryStr.append("} ORDER BY DESC(d)");

Query q = queryStr.asQuery();

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.

梨涡 2024-12-09 08:01:52

我实现了 SPARQL Java - 一种用 Java 编写 SPARQL 查询的 DSL。

它解决了 IDE 自动格式化连接的 SPARQL 查询字符串等问题。

举例来说:

String shortQuery = Q.prefix("books", "http://example.org/books#")
            .select("?book ?authorName", new where() {
                {
                    $("?book books:author ?author");
                    $("?author books:authorName ?authorName");
                }
            }).get();

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:

String shortQuery = Q.prefix("books", "http://example.org/books#")
            .select("?book ?authorName", new where() {
                {
                    $("?book books:author ?author");
                    $("?author books:authorName ?authorName");
                }
            }).get();
以可爱出名 2024-12-09 08:01:52

我最近开始使用 Sesame 查询生成器。它看起来很有希望,只是它没有提供太多文档,而且我很难找到示例。这是一个可以帮助您入门的简单示例:

ParsedTupleQuery query = QueryBuilderFactory
            .select("pubProperty", "pubPropertyValue")
                .group()
                    .atom(cmResource(resourceId), LinkPublicationsTransformation.REFERENCE_URI, "pubUri")
                    .atom("pubUri", "pubProperty", "pubPropertyValue")
                    .filter(isLiteral("pubPropertyValue"))
                .closeGroup()
            .query();

请注意,isLiteralcmResource 是我自己的小型静态帮助器类。 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:

ParsedTupleQuery query = QueryBuilderFactory
            .select("pubProperty", "pubPropertyValue")
                .group()
                    .atom(cmResource(resourceId), LinkPublicationsTransformation.REFERENCE_URI, "pubUri")
                    .atom("pubUri", "pubProperty", "pubPropertyValue")
                    .filter(isLiteral("pubPropertyValue"))
                .closeGroup()
            .query();

Just note that isLiteral and cmResource are my own little static helper classes. isLiteral stands for new 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 turn ParsedQuery into String 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 on RenderUtils from sesame-queryrendered which has all the convenient methods to add < > around URIs, escape special characters etc.

土豪 2024-12-09 08:01:52

Eclipse RDF4J 框架(Sesame 的后继者)提供了一个类似于 JDBC 的 Repository API - 它允许您创建一个准备好的查询对象并在执行之前注入变量绑定:

String query = "SELECT * WHERE {?X ?P ?Y }";
TupleQuery preparedQuery = conn.prepareQuery(QuerLanguage.SPARQL, query);
preparedQuery.setBinding("X", someValue);
...
TupleQueryResult result = preparedQuery.evaluate();

此外,RDF4J 有一个 SparqlBuilder(最初称为 spanqit) - 用于 SPARQL 的 Java DSL,允许您在代码中创建 SPARQL 查询,如下所示:

query.prefix(foaf).select(name)
    .where(x.has(foaf.iri("name"), name))
    .orderBy(name)
    .limit(5)
    .offset(10);

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:

String query = "SELECT * WHERE {?X ?P ?Y }";
TupleQuery preparedQuery = conn.prepareQuery(QuerLanguage.SPARQL, query);
preparedQuery.setBinding("X", someValue);
...
TupleQueryResult result = preparedQuery.evaluate();

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:

query.prefix(foaf).select(name)
    .where(x.has(foaf.iri("name"), name))
    .orderBy(name)
    .limit(5)
    .offset(10);
尛丟丟 2024-12-09 08:01:52

我刚刚发布了一个测试版项目来做到这一点,名为 Spanqit

我力求可读性和直观的界面,例如,这里是一些用于创建查询的 Spanqit 语法示例:

query.prefix(foaf).select(name)
    .where(x.has(foaf.iri("name"), name))
    .orderBy(name)
    .limit(5)
    .offset(10);

查看它,并随时发表评论并提出改进建议!

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:

query.prefix(foaf).select(name)
    .where(x.has(foaf.iri("name"), name))
    .orderBy(name)
    .limit(5)
    .offset(10);

Check it out, and feel free to comment and suggest improvements!

末骤雨初歇 2024-12-09 08:01:52

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.

︶葆Ⅱㄣ 2024-12-09 08:01:52

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文