Interface SPARQLQueryManager


public interface SPARQLQueryManager

A manager for executing SPARQL queries in MarkLogic Server and retrieving the results.

For example perform a SPARQL SELECT:

    SPARQLQueryManager sparqlMgr = databaseClient.newSPARQLQueryManager();
    String sparql = "SELECT * WHERE { ?s ?p ?o } LIMIT 10";
    SPARQLQueryDefinition query = sparqlMgr.newQueryDefinition(sparql)
        .withBinding("o", "http://example.org/object1");
    JacksonHandle handle = new JacksonHandle();
    handle.setMimetype(SPARQLMimeTypes.SPARQL_JSON);
    JacksonHandle results = sparqlMgr.executeSelect(query, handle);
    JsonNode tuples = results.get().path("results").path("bindings");
    for ( JsonNode row : tuples ) {
        String s = row.path("s").path("value").asText();
        String p = row.path("p").path("value").asText();
        ...
    }

Or perform a SPARQL CONSTRUCT:

    String sparql = "CONSTRUCT { <a> <b> <c> } WHERE { ?s ?p ?o } LIMIT 10";
    SPARQLQueryDefinition query = sparqlMgr.newQueryDefinition(sparql);
    SPARQLBindings bindings = query.getBindings();
    query.setBindings(bindings.bind("o", "http://example.org/object1"));
    JacksonHandle handle = new JacksonHandle();
    handle.setMimetype(RDFMimeTypes.RDFJSON);
    JacksonHandle triples = sparqlMgr.executeConstruct(query, handle);
    JsonNode a = triples.get().path("a");
    JsonNode b = a.path("b");
    JsonNode c = b.get(0).path("value");

Each new instance of SPARQLQueryManager is created by DatabaseClient.newSPARQLQueryManager(). While these examples use JacksonHandle, any SPARQLResultsReadHandle may be used--including custom handles. For executeSelect JSONReadHandles will need to use SPARQLMimeTypes.SPARQL_JSON mimetype, and XMLReadHandles will need to use SPARQLMimeTypes.SPARQL_XML mimetype, other SPARQLResultsReadHandles accept any text and can therefore accept results in any of the SPARQLMimeTypes. For executeDescribe and executeConstruct JSONReadHandles will need to use RDFMimeTypes.RDFJSON mimetype, and XMLReadHandles will need to use RDFMimeTypes.RDFXML mimetype, other TriplesReadHandles accept any text and can therefore accept results in any of the RDFMimeTypes.

SPARQLQueryManager is thread-safe other than setPageLength. In other words the only state maintained by an instance is the page length. Common usage is to call setPageLength only once then use the instance across many threads. If you intend to call setPageLength from multiple threads, create a new SPARQLQueryManager for each thread.

For details about RDF, SPARQL, and semantics in MarkLogic see the Semantics Developer's Guide.