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 JSONReadHandle
s will need to use SPARQLMimeTypes.SPARQL_JSON
mimetype, and XMLReadHandle
s will need
to use SPARQLMimeTypes.SPARQL_XML
mimetype, other SPARQLResultsReadHandle
s accept any text and can therefore accept results
in any of the SPARQLMimeTypes. For executeDescribe and executeConstruct
JSONReadHandle
s will need to use RDFMimeTypes.RDFJSON
mimetype, and XMLReadHandle
s will need to use RDFMimeTypes.RDFXML
mimetype, other TriplesReadHandle
s 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.
Modifier and Type | Method and Description |
---|---|
void |
clearPageLength()
Reset this instance to have no page length set.
|
java.lang.Boolean |
executeAsk(SPARQLQueryDefinition qdef)
Execute a SPARQL "ASK" statement.
|
java.lang.Boolean |
executeAsk(SPARQLQueryDefinition qdef,
Transaction tx)
Execute a SPARQL "ASK" statement.
|
<T extends TriplesReadHandle> |
executeConstruct(SPARQLQueryDefinition qdef,
T handle)
Execute a SPARQL "CONSTRUCT" statement.
|
<T extends TriplesReadHandle> |
executeConstruct(SPARQLQueryDefinition qdef,
T handle,
Transaction tx)
Execute a SPARQL "CONSTRUCT" statement.
|
<T extends TriplesReadHandle> |
executeDescribe(SPARQLQueryDefinition qdef,
T handle)
Execute a SPARQL "DESCRIBE" query (which implements the Concise Bounded Description specification).
|
<T extends TriplesReadHandle> |
executeDescribe(SPARQLQueryDefinition qdef,
T handle,
Transaction tx)
Execute a SPARQL "DESCRIBE" query (which implements the Concise Bounded Description specification).
|
<T extends SPARQLResultsReadHandle> |
executeSelect(SPARQLQueryDefinition qdef,
T handle)
Execute a SPARQL "SELECT" query.
|
<T extends SPARQLResultsReadHandle> |
executeSelect(SPARQLQueryDefinition qdef,
T handle,
long start)
Execute a SPARQL "SELECT" query.
|
<T extends SPARQLResultsReadHandle> |
executeSelect(SPARQLQueryDefinition qdef,
T handle,
long start,
Transaction tx)
Execute a SPARQL "SELECT" query.
|
<T extends SPARQLResultsReadHandle> |
executeSelect(SPARQLQueryDefinition qdef,
T handle,
Transaction tx)
Execute a SPARQL "SELECT" query.
|
void |
executeUpdate(SPARQLQueryDefinition qdef)
Execute a SPARQL update statement.
|
void |
executeUpdate(SPARQLQueryDefinition qdef,
Transaction tx)
Execute a SPARQL update statement.
|
long |
getPageLength() |
SPARQLQueryDefinition |
newQueryDefinition()
Instantiate a new SPARQLQueryDefinition.
|
SPARQLQueryDefinition |
newQueryDefinition(java.lang.String sparql)
Instantiate a new SPARQLQueryDefinition with provided SPARQL.
|
SPARQLQueryDefinition |
newQueryDefinition(TextWriteHandle sparql)
Instantiate a new SPARQLQueryDefinition with the SPARQL from
the provided TextWriteHandle.
|
GraphPermissions |
permission(java.lang.String role,
Capability... capabilities)
For use with SPARQL update, where specified permissions will apply
to any records created by the update.
|
void |
setPageLength(long pageLength)
Set a page length for all SPARQL "SELECT" queries sent by this instance.
|
SPARQLQueryDefinition newQueryDefinition()
SPARQLQueryDefinition newQueryDefinition(java.lang.String sparql)
sparql
- a sparql query as textSPARQLQueryDefinition newQueryDefinition(TextWriteHandle sparql)
sparql
- the handle containing a sparql query as text<T extends SPARQLResultsReadHandle> T executeSelect(SPARQLQueryDefinition qdef, T handle)
T
- the type of SPARQLResultsReadHandle to returnqdef
- the queryhandle
- the handle capable of reading sparql results
<T extends SPARQLResultsReadHandle> T executeSelect(SPARQLQueryDefinition qdef, T handle, Transaction tx)
T
- the type of SPARQLResultsReadHandle to returnqdef
- the queryhandle
- the handle capable of reading sparql results
tx
- the transaction context for this operation<T extends SPARQLResultsReadHandle> T executeSelect(SPARQLQueryDefinition qdef, T handle, long start)
T
- the type of SPARQLResultsReadHandle to returnqdef
- the queryhandle
- the handle capable of reading sparql results
start
- when paging through results, the first result of this page--must be > 0.
Use together with setPageLength(long)
.<T extends SPARQLResultsReadHandle> T executeSelect(SPARQLQueryDefinition qdef, T handle, long start, Transaction tx)
T
- the type of SPARQLResultsReadHandle to returnqdef
- the queryhandle
- the handle capable of reading sparql results
start
- when paging through results, the first result of this page--must be > 0.
Use together with setPageLength(long)
.tx
- the transaction context for this operationlong getPageLength()
void setPageLength(long pageLength)
pageLength
- the non-negative number of results per pagevoid clearPageLength()
<T extends TriplesReadHandle> T executeConstruct(SPARQLQueryDefinition qdef, T handle)
T
- the type of TriplesReadHandle to returnqdef
- the SPARQL "CONSTRUCT" statementhandle
- the handle capable of reading triples or quads results
<T extends TriplesReadHandle> T executeConstruct(SPARQLQueryDefinition qdef, T handle, Transaction tx)
T
- the type of TriplesReadHandle to returnqdef
- the SPARQL "CONSTRUCT" statementhandle
- the handle capable of reading triples or quads results
tx
- the transaction context for this query<T extends TriplesReadHandle> T executeDescribe(SPARQLQueryDefinition qdef, T handle)
T
- the type of TriplesReadHandle to returnqdef
- the queryhandle
- the handle capable of reading triples or quads results
<T extends TriplesReadHandle> T executeDescribe(SPARQLQueryDefinition qdef, T handle, Transaction tx)
T
- the type of TriplesReadHandle to returnqdef
- the queryhandle
- the handle capable of reading triples or quads results
tx
- the transaction context for this queryjava.lang.Boolean executeAsk(SPARQLQueryDefinition qdef)
qdef
- the SPARQL "CONSTRUCT" statementjava.lang.Boolean executeAsk(SPARQLQueryDefinition qdef, Transaction tx)
qdef
- the SPARQL "CONSTRUCT" statementtx
- the transaction context for this queryvoid executeUpdate(SPARQLQueryDefinition qdef)
permission(java.lang.String, com.marklogic.client.semantics.Capability...)
.qdef
- the SPARQL update statementvoid executeUpdate(SPARQLQueryDefinition qdef, Transaction tx)
permission(java.lang.String, com.marklogic.client.semantics.Capability...)
.qdef
- the SPARQL update statementtx
- the transaction context for this operationGraphPermissions permission(java.lang.String role, Capability... capabilities)
For 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 © 2013-2020 MarkLogic Corporation.