The Guide for Time Series Data Projects is out.

Download now
Skip to content

Using Spring Data Crate With Your Java REST Application

This article is more than 4 years old

Crate as an Open Source project lives from contributions to its ecosystem from the community. Therefore we're always happy to see projects coming up that facilitate the use of Crate.

Hasnain Javed (KP Technology Lab) and Rizwan Idrees (Springer) developed a Spring Data adapter for Crate.


"Spring Data makes it easy to use new data access technologies, such as non-relational databases, map-reduce frameworks, and cloud based data services. Spring Data also provides improved support for relational database technologies."


In this tutorial post will demonstrate how to integrate Spring Data Crate with your Java application based on a very simple Spring Boot REST application that accepts GET requests at http://localhost:8080 and returns a list of users that are stored in Crate.

You can find the complete example of this tutorial also on Github!

Let's go!

To get started please clone the code of the spring-data-crate project to your local machine and compile the jar file. This will require JDK 1.8 to be installed, but the compile target should be 1.7, because Crate does not officially support Java 8 yet.

*Update March 2016: Due to a bugfix in Crate, some spring-data-crate tests will now fail. Use mvn -Dmaven.test.skip=true clean install instead. *

$ git clone
$ mvn clean install

Then let's create a new Gradle project using IntelliJ IDEA with the following folder structure ...

├── libs
└── src
    ├── main
    │   ├── java
    │   │   └── io
    │   │       └── crate
    │   │           └── hellospring
    │   └── resources
    └── test
        ├── java
        └── resources

... and put the previously compiled spring-data-crate-1.0.0-BUILD-SNAPSHOT.jar (and the -sources.jar if you want) into the ./libs folder.

In the file build.gradle we add the Spring Boot Gradle plugin and additional repositories where Gradle should look for dependencies.
Note the flatDirs {} directive, this will tell Gradle to look in the ./libs folder for jar files.

buildscript {
    ext {
        springBootVersion = '1.2.2.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = '1.7'
targetCompatibility = '1.7'

repositories {
    flatDir {
        dirs 'libs'
    maven {
        url ''

We'll also need to add a couple of dependencies, such as the Crate client and some Spring framework libraries.

The validators are optional, but we use them in our example to define column constraints.

dependencies {
    ext {
    springVersion = '4.0.9.RELEASE'
    springDataVersion = '1.10.0.BUILD-SNAPSHOT'
    crateVersion = '0.47.4'

    // Crate client
    // our newly created Spring Data Crate jar
    compile name: 'spring-data-crate-1.0.0.BUILD-SNAPSHOT'

    // spring framework

    // other tools

    // optional

    // testing
    testCompile group: 'junit', name: 'junit', version: '4.11'

That's it for the setup!

Installing Crate

However, before you start coding the application,
you will need to download and install Crate first.
You can either download and extract the tarball or pull the latest Docker image.
Instructions for both ways can be found in the documentation section on our website.

Run Crate after extracting the tar.gz

$ cd crate-0.47.4
$ ./bin/crate

Or run Crate using Docker after pulling the image crate:latest

$ docker run -ti -p 4200:4200 crate:latest --env CRATE_HEAP_SIZE=1g crate

Building The Application

For our REST application we need a User model which will be mapped to a users table in Crate, an HTTP endpoint which is provided by a Spring RestController, and an application entry point that will make the app executable.

The Model

Our User model has an email address as its unique id and a few other attributes.
Important to mention here is that Crate has built in support for ARRAY and OBJECT types (see docs).
In your Java application you can simply use <Type>[] and HashMap<String, Object>, and Spring Data Crate will take care of the correct conversion to the database types.

The @Table annotation wires up the User model to the correct database table. The decorator allows various arguments:

  • name: the name of the table (required)
  • refreshInterval: specifies the refresh interval of a shard in milliseconds (optional, defaults to 1000) - see docs
  • numberOfReplicas: number of configured replicas (optional, defaults to "1") - see docs
  • columnPolicy: defines whether a table enforces its defined schema (optional, defaults to DYNAMIC) - see docs

If you provide one or more constructors for a model - such as we do in our example,
you need to annotate one of them with @PersistenceConstructor or an exception will be thrown at runtime.

@Table(name="users", numberOfReplicas="0-all")
public class User {

    private String email;
    private String firstName;
    private String lastName;
    private Long dateJoined;
    private String[] tags;
    private HashMap<String, Object> attributes;

    public User(String firstName, String lastName, String email, Long dateJoined) {
        this(firstName, lastName, email, dateJoined, new String[]{});

    public User(String firstName, String lastName, String email, Long dateJoined, String[] tags) {
        this(firstName, lastName, email, dateJoined, tags, new HashMap<String, Object>());

    public User(String firstName, String lastName, String email, Long dateJoined, String[] tags, HashMap<String, Object> attributes) {
        this.firstName = firstName;
        this.lastName = lastName; = email;
        this.dateJoined = dateJoined;
        this.tags = tags;
        this.attributes = attributes;

    // getter and setters here

The Database Configuration

User Repository

Spring Data Crate provides a generic repository programming model to simplify the creation of data repositories.
For our User class with Id property of type String, a UserRepository interface can be defined as:

public interface UserRepository extends CrateRepository<User, String> {

Note: Currently documents can be queried only by Id. Support will be added to derive queries from method names.

App Configuration

To provide beans for the Crate client, the Crate template and the Crate PESM we also need to create an application configuration
that enables all Crate data repositories using the @EnableCrateRepositories annotation.

class ApplicationConfig extends AbstractCrateConfiguration {

    public CratePersistentEntitySchemaManager cratePersistentEntitySchemaManager() throws Exception {
        return new CratePersistentEntitySchemaManager(crateTemplate(), SchemaExportOption.CREATE);

    // optional
    // only required because we are using @Email and @NotBlank annotations (JSR-303) for validation
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();

    // optional
    // only required because we are using @Email and @NotBlank annotations (JSR-303) for validation
    public ValidatingCrateEventListener validatingCrateEventListener() {
        return new ValidatingCrateEventListener(validator());


For further information on how you can configure the PESM, please refer to the Spring Data Crate documentation

The Service

The UserService is an additional abstraction between the controller that we will create afterwards and the Crate data respository.
Herer we define methods for creating, updating and retrieving users from the database. Since we've already defined beans for
the Crate template and the User data repository earlier in the application configuration, we can inject them here using the @Autowired annotation on the properties.

The @Service annotation defines the class as a Spring Service so we can also inject it later in our controller.

public class UserService {

    private UserRepository userRepository;

    private Long now() {
        return System.currentTimeMillis();

    public void deleteUsers() {

    public User createUser(String email, String firstName, String lastName) {
        User user = new User(firstName, lastName, email, now());;
        return user;

    public void updateUser(User user) {
        // is clever enough to distinguish between insert and update;

    public Collection<User> allUsers() {
        return userRepository.findAll();

    public void refresh() {


The Controller

The controller is the heart of our application and defines the routing. Annotating the controller with Spring's @RestController automatically turns the returned value into JSON using the Jackson library. This makes it really simple to create RESTful webservices using Spring.

The userService bean is injected using the @Autowired annotation again.
Our application only has one single HTTP endpoint at root (/), where we first delete all users from the table and then create three new which are then updated with attributes and tags.

In the end we retrieve all user objects from the database and return them as a collection.

public class CrateController {

    private UserService userService;

    public Collection<User> index() {
        // reset

        // create users
        User christian = userService.createUser("",
                                                "Christian", "Haudum");
        User rizwan = userService.createUser("",
                                             "Rizwan", "Idrees");
        User hasnain = userService.createUser("",
                                              "Hasnain", "Javed");

        // refresh table to make sure we get latest version of the documents

        // update users
        christian.setTags(new String[]{"python"});
        christian.setAttributes(new HashMap<String, Object>(){
            { put("company","Crate.IO"); }

        rizwan.setTags(new String[]{"java","spring framework"});
        rizwan.setAttributes(new HashMap<String, Object>(){
            { put("company", "KP Technology Lab"); }

        hasnain.setTags(new String[]{"java","spring data"});
        hasnain.setAttributes(new HashMap<String, Object>(){
            { put("company", "KP Technology Lab"); }

        // refresh table to make sure we get latest version of the documents

        // return a collection of users
        // Spring will automatically encode it to JSON using Jackson
        return userService.allUsers();

The Application

All we need to run our Spring Boot application now is the entry point. This is achieved by annotating the
application class with @SpringBootApplication and defining the good old main method that invokes

This will embed and run the application inside the Tomcat HTTP runtime when executed.

package io.crate.hellospring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class CrateApplication {

  public static void main(String[] args) {, args);


Now it's time to run the application! You can either start it from IntelliJ directly by right clicking on the CrateApplication class and selecting "Run CrateApplication.main()" or using the CLI:

$ ./gradlew clean bootRun

Once the application is started up, you can navigate to http://localhost:8080/ in your browser.
And voilà ... there you should get a JSON formatted response looking like this:

    "email": "",
    "firstName": "Christian",
    "lastName": "Haudum",
    "dateJoined": 1425631721178,
    "tags": [
    "attributes": {
      "company": "Crate.IO"
    "id": "",
    "fullName": "Christian Haudum"
    "email": "",
    "firstName": "Rizwan",
    "lastName": "Idrees",
    "dateJoined": 1425631721183,
    "tags": [
      "spring framework"
    "attributes": {
      "company": "KP Technology Lab"
    "id": "",
    "fullName": "Rizwan Idrees"
    "email": "",
    "firstName": "Hasnain",
    "lastName": "Javed",
    "dateJoined": 1425631721188,
    "tags": [
      "spring data"
    "attributes": {
      "company": "KP Technology Lab"
    "id": "",
    "fullName": "Hasnain Javed"


The Spring Data Crate adapter allows you to create RESTful web applications with the scalable Crate backend super easily.
Although the project is only in its early stage and misses some features (e.g. find entities by method) it can be used for various use cases.
The project is Open Source and available on Github, so contributions are very welcome!

You can find the complete example of this tutorial also on Github!