How to build SPARQL queries in java?

后端 未结 8 1582
囚心锁ツ
囚心锁ツ 2021-02-01 10:19

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 PreparedStateme

8条回答
  •  盖世英雄少女心
    2021-02-01 11:09

    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.

提交回复
热议问题