How to build SPARQL queries in java?

后端 未结 8 1586
囚心锁ツ
囚心锁ツ 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:55

    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);
    

提交回复
热议问题