This page discusses using the Spring Framework to interact with Stardog.
Spring for Stardog makes it possible to rapidly build Stardog-backed applications with the Spring Framework. As with many other parts of Spring, Stardog’s Spring integration uses the template design pattern for abstracting standard boilerplate away from application developers.
Stardog Spring can be included via Maven with
com.stardog.ext:stardog-spring-batch:version for Spring Batch support. Both of these dependencies require the public Stardog repository to be included in your build script, and the Stardog Spring packages installed in Maven.
The embedded server is still supported, but via providing an implementation of the
Provider interface. This enables users of the embedded server to have full control over how to use the embedded server.
At the lowest level, Spring for Stardog includes:
DataSourceFactoryBeanfor managing Stardog connections
SnarlTemplatefor transaction- and connection-pool safe Stardog programming
DataImporterfor easy bootstrapping of input data into Stardog
In addition to the core capabilities, Spring for Stardog also integrates with the Spring Batch framework. Spring Batch enables complex batch processing jobs to be created to accomplish tasks such as ETL or legacy data migration. The standard
ItemWriter interfaces are implemented with a separate callback writing records using the SNARL Adder API.
The Spring for Stardog source code is available on Github. This framework is open source and in continuous development. All are welcome to contribute.
There are three Beans to add to a Spring application context:
DataSourceFactoryBean is a Spring
FactoryBean that configures and produces a
DataSource. All of the Stardog
ConnectionPoolConfig methods are also property names of the
DataSourceFactoryBean– for example,
createIfNotPresent. If you are interested in running an embedded server, use the
Provider interface and inject it into the
DataSourceFactoryBean. Note: all of the server jars must be added to your classpath for using the embedded server.
javax.sql.DataSource, that can be used to retrieve a
Connection from the
ConnectionPool. This additional abstraction serves as place to add Spring-specific capabilities (e.g.
spring-tx support in the future) without directly requiring Spring in Stardog.
SnarlTemplate provides a template abstraction over much of Stardog’s native API, and follows the same approach of other Spring template, i.e.,
JmsTemplate, and so on.
Spring for Stardog also comes with convenience mappers, for automatically mapping result set bindings into common data types. The
SimpleRowMapper projects the
BindingSet as a
List> and a
SingleMapper that accepts a constructor parameter for binding a single parameter for a single result set.
The key methods on
SnarlTemplate include the following:
query(String sparqlQuery, Map args, RowMapper)
query() executes the SELECT query with provided argument list, and invokes the mapper for result rows.
doWithAdder() is a transaction- and connection-pool safe adder call.
doWithGetter(String subject, String predicate, GetterCallback)
doWithGetter() is the connection pool boilerplate method for the
Getter interface, including the programmatic filters.
doWithRemover() As above, the remover method that is transaction and pool safe.
execute() lets you work with a connection directly; again, transaction and pool safe.
construct(String constructSparql, Map args, GraphMapper)
construct() executes a SPARQL CONSTRUCT query with provided argument list, and invokes the
GraphMapper for the result set.
DataImporter is a new class that automates the loading of RDF files into Stardog at initialization time.
It uses the Spring Resource API, so files can be loaded anywhere that is resolvable by the Resource API: classpath, file, url, etc. It has a single load method for further run-time loading and can load a list of files at initialization time. The list assumes a uniform set of file formats, so if there are many different types of files to load with different RDF formats, there would be different
DataImporter beans configured in Spring.
In addition to the base
SnarlTemplate, Spring Batch support adds the following:
Stardog-spring adheres to SemVer conventions for releases. The current version is 1.0.0. and has been tested against Stardog 7.3.2.
stardog-spring-batch releases are available from Maven Central:
You will also need to include the Stardog Maven repository into your build script:
<repositories> <repository> <id>stardog-public</id> <url>http://maven.stardog.com</url> </repository> </repositories>
Prior to Stardog 7.2.1, the corresponding Stardog Spring version matched the Stardog release, e.g.
stardog-spring-6.0.1 for Stardog 6.0.1. Those releases are also available from Maven Central under a different group name:
A quick start guide is provided in the Stardog Spring repository on Github.