Skip to content

StoredQuery

Brad Bebee edited this page Feb 13, 2020 · 1 revision

A stored query is SPARQL SERVICE that exposes custom applications for execution. The stored query may be a parameterized SPARQL query or arbitrary procedural application logic, but it must evaluate to a solution multi-set. The service interface is written to the openrdf interfaces in order to remove the burden of dealing with bigdata IVs from the application.

The main advantage of stored queries is the ability to execute complex application logic on the server without requiring the marshaling of RDF data and SPARQL solutions sets across the HTTP interface.

Support for stored queries in available in r1.3.2.

Registering a Stored Query

In order to use a stored query, a concrete instance of the StoredQueryService class must be registered against the ServiceRegistry. The choice of the SERVICE URI is up to the application.

 final URI serviceURI = new URIImpl("http://www.bigdata.com/rdf/stored-query#my-stored-query");
 
 ServiceRegistry.getInstance().add(serviceURI, new MyStoredQueryService());

Invoking a Stored Query

Once registered, the stored query may be referenced from SPARQL using its assigned service URI:

  SELECT * {
     SERVICE <http://www.bigdata.com/rdf/stored-query#my-stored-query> { }
 }

Specifying Arguments

The SERVICE invocation may include a group graph pattern that will be parsed and made accessible to the stored query service as ServiceParams object. For example:

 SELECT * {
    SERVICE <http://www.bigdata.com/rdf/stored-query#my-stored-query> {
       bd:serviceParam :color :"blue" .
       bd:serviceParam :color :"green" .
       bd:serviceParam :size  :"large" .
    }
 }

will provide the stored query with two bindings for the :color = {"blue", "green"} and one binding for :size = {"large"}. The value key names, the allowed value types for each key name, and the interpretation of those values are all specific to a given stored query service implementation class. They will be provided to that class as a ServiceParams object.

SimpleStoredQueryService

The SimpleStoredQueryService class allows you to execute a parameterized SPARQL query on the database. You need to implement a single method, getQuery(), which returns the SPARQL query to be evaluated.

    class MyStoredQueryService extends SimpleStoredQueryService {

        @Override
        public String getQuery(final ServiceCallCreateParams createParams,
                final ServiceParams serviceParams) {
            final StringBuilder sb = new StringBuilder();
            sb.append("PREFIX dc:   <http://purl.org/dc/elements/1.1/> \n");
            sb.append("PREFIX :     <http://example.org/book/> \n");
            sb.append("PREFIX ns:   <http://example.org/ns#> \n");
            sb.append("SELECT ?book ?title ?price { \n");
            sb.append("  ?book dc:title ?title ; \n");
            sb.append("  ns:price ?price . \n");
            sb.append("} \n");
            return sb.toString();
        }
        
    }

The effective value of the baseURI during query evaluation will be the SERVICE URI.

StoredQueryService

The StoredQueryService allows general purpose application logic to execute against a read-only connection on a specific triple or quad store instance in the database.

To implement a general purpose stored query, you need to provide a concrete implementation of the StoredQueryService class. For example:

    class MyStoredQueryService extends StoredQueryService {

        @Override
        protected TupleQueryResult doQuery(
                BigdataSailRepositoryConnection cxn,
                ServiceCallCreateParams createParams,
                ServiceParams serviceParams) throws Exception {

            final TupleQueryResult result = doWork();

            return result;
        }
        
    }
Clone this wiki locally