How to build SPARQL queries in java?

后端 未结 8 1610
囚心锁ツ
囚心锁ツ 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 10:57

    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.

提交回复
热议问题