Spring
This page discusses using the Spring Framework to interact with Stardog.
Overview
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:version
and 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.
At the lowest level, Spring for Stardog includes:
DataSource
andDataSourceFactoryBean
for managing Stardog connectionsSnarlTemplate
for transaction- and connection-pool safe Stardog programmingDataImporter
for 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 ItemReader
and ItemWriter
interfaces are implemented with a separate callback writing records using the SNARL Adder API.
Contributing
The Spring for Stardog source code is available on Github. This framework is open source and in continuous development. All are welcome to contribute.
Basic Spring
There are three Beans to add to a Spring application context:
DataSourceFactoryBean
:com.complexible.stardog.ext.spring.DataSourceFactoryBean
SnarlTemplate
:com.complexible.stardog.ext.spring.SnarlTemplate
DataImporter
:com.complexible.stardog.ext.spring.DataImporter
DataSourceFactoryBean
is a Spring FactoryBean
that configures and produces a DataSource
. All of the Stardog ConnectionConfiguration
and ConnectionPoolConfig
methods are also property names of the DataSourceFactoryBean
– for example, to
, url
, 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., JdbcTemplate
, 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(AdderCallback)
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(RemoverCallback)
doWithRemover()
As above, the remover method that is transaction and pool safe.
execute(ConnectionCallback)
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.
Spring Batch
In addition to the base DataSource
and SnarlTemplate
, Spring Batch support adds the following:
SnarlItemReader
:com.stardog.ext.spring.batch.SnarlItemReader
SnarlItemWriter
:com.stardog.ext.spring.batch.SnarlItemWriter
BatchAdderCallback
:com.stardog.ext.spring.batch.BatchAdderCallback
Adding Stardog Spring and Batch to your project
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
and stardog-spring-batch
releases are available from Maven Central:
com.stardog.ext:stardog-spring:1.0.0
com.stardog.ext:stardog-spring-batch:1.0.0
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:
com.complexible.stardog.ext:stardog-spring:7.2.0
com.complexible.stardog.ext:stardog-spring-batch:7.2.0
Getting Started
A quick start guide is provided in the Stardog Spring repository on Github.