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.
voidexecuteAsk (SPARQLQueryDefinition qdef,
Transaction tx)<T extends TriplesReadHandle>
TexecuteConstruct (SPARQLQueryDefinition qdef,
T handle)<T extends TriplesReadHandle>
TexecuteConstruct (SPARQLQueryDefinition qdef,
T handle, Transaction tx)<T extends TriplesReadHandle>
TexecuteDescribe (SPARQLQueryDefinition qdef,
T handle)<T extends TriplesReadHandle>
TexecuteDescribe (SPARQLQueryDefinition qdef,
T handle, Transaction tx)<T extends SPARQLResultsReadHandle>
TexecuteSelect (SPARQLQueryDefinition qdef,
T handle)<T extends SPARQLResultsReadHandle>
TexecuteSelect (SPARQLQueryDefinition qdef,
T handle, long start)<T extends SPARQLResultsReadHandle>
TexecuteSelect (SPARQLQueryDefinition qdef,
T handle, long start, Transaction tx)<T extends SPARQLResultsReadHandle>
TexecuteSelect (SPARQLQueryDefinition qdef,
T handle, Transaction tx)voidvoidexecuteUpdate (SPARQLQueryDefinition qdef,
Transaction tx)longnewQueryDefinition (TextWriteHandle sparql)newQueryDefinition (String sparql)permission (String role, Capability... capabilities)voidsetPageLength (long pageLength)sparql - a sparql query as textsparql - the handle containing a sparql query as
textT - the type of SPARQLResultsReadHandle to
returnqdef - the queryhandle - the handle capable of reading sparql
resultsT - the type of SPARQLResultsReadHandle to
returnqdef - the queryhandle - the handle capable of reading sparql
resultstx - the transaction context for this
operationT - the type of SPARQLResultsReadHandle to
returnqdef - the queryhandle - the handle capable of reading sparql
resultsstart - when paging through results, the first
result of this page--must be > 0. Use together with setPageLength(long).T - the type of SPARQLResultsReadHandle to
returnqdef - the queryhandle - the handle capable of reading sparql
resultsstart - when paging through results, the first
result of this page--must be > 0. Use together with setPageLength(long).tx - the transaction context for this
operationpageLength - the non-negative number of results
per pageT - the type of TriplesReadHandle to returnqdef - the SPARQL "CONSTRUCT" statementhandle - the handle capable of reading triples or quads
resultsT - the type of TriplesReadHandle to returnqdef - the SPARQL "CONSTRUCT" statementhandle - the handle capable of reading triples or quads
resultstx - the transaction context for this queryT - the type of TriplesReadHandle to returnqdef - the queryhandle - the handle capable of reading triples or quads
resultsT - the type of TriplesReadHandle to returnqdef - the queryhandle - the handle capable of reading triples or quads
resultstx - the transaction context for this queryqdef - the SPARQL "CONSTRUCT" statementqdef - the SPARQL "CONSTRUCT" statementtx - the transaction context for this querypermission(java.lang.String,
com.marklogic.client.semantics.Capability...).qdef - the SPARQL update statementpermission(java.lang.String,
com.marklogic.client.semantics.Capability...).qdef - the SPARQL update statementtx - the transaction context for this
operationFor use with SPARQL update, where specified permissions will apply to any records created by the update. Create a GraphPermissions builder object with the specified role and capabilities.
For example:
String sparqlUpdate = "INSERT DATA { <a> <b> <c> }";
SPARQLQueryDefinition qdef = sparqlMgr.newQueryDefinition(sparqlUpdate);
qdef.setUpdatePermissions(sparqlMgr.permission("rest-reader", Capability.UPDATE));
sparqlMgr.executeUpdate(qdef);
role - the name of the role receiving these
capabilitiescapabilities - the capabilities (READ, UPDATE, or
EXECUTE) granted to this roleCopyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.