Spring is a platform to build and run enterprise applications in Java. Stardog's Spring support makes life easier for enterprise developers who need to work with Semantic Web technology—including RDF, SPARQL, and OWL—by way of Stardog.
To build Spring for Stardog, you need a release of Stardog; we use Gradle to build Stardog for Spring. Then,
- edit build.gradle to point stardogLocation at a Stardog release directory;
- run gradlew, which will download and bootstrap a gradle build environment;
- then run gradlew build, which eventually results in a stardog-spring.jar in build/libs; finally,
- gradlew install does a build, generates a POM, and installs the POM in local Maven repo; alternately,
- mvn install will work, too:
mvn install:install-file -DgroupId=com.clarkparsia.stardog -DartifactId=stardog-spring -Dversion=1.1.1 -Dfile=stardog-spring.jar -Dpackaging=jar -DpomFile=pom.xml
Spring for Stardog 1.1.1 provides a set of capabilities for rapidly building 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.
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 ItemReader and ItemWriter interfaces are implemented with a separate callback writing records using the SNARL Adder API.
Future releases of Spring for Stardog will address other common enterprise capabilities: Spring Integration, Spring Data, etc.
There are three Beans to add to a Spring application context:
DataSourceFactoryBean is a Spring
that configures and produces a
of the Stardog
ConnectionPoolConfig methods are also property names of
DataSourceFactoryBean—for example, "to", "url",
DataSource is a Spring for Stardog class, similar
to javax.sql.DataSource, that can be used to retrieve a
Connection from the
additional abstraction serves as place to add Spring-specific capabilities
(e.g. spring-tx support in the future) without directly requiring Spring in
SnarlTemplate provides a template abstraction over much
of Stardog's native API, SNARL, 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
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
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
Here's a sample
In addition to the base
SnarlTemplate, Spring Batch support adds the following:
These beans can then be used within Spring Batch job definition, for example: