Apache Ignite With Spring Data
In this tutorial, we will learn how to integrate Spring Data API with Apache Ignite. To learn about Apache Ignite do check What is Apache Ignite? In one of my previous tutorials, we have already seen how to use Spring Cache with Apache Ignite.
Spring Data Framework provides a unified and widely used API that abstract the underlying data storage used by any application. It provides CRUD capabilities to any domain object without the need for any boilerplate code. We can minimize the amount of source code needed to write custom queries. The API offers simple abstractions for performing common tasks.
Spring Data helps us to avoid locking to specific database storage. Hence, it is easier to switch from one storage to another with minimal efforts.
Apache Ignite implements Spring Data CrudRepository
interface that not only supports basic CRUD operations but also provides access to the Apache Ignite SQL Grid via the unified Spring Data API.
We will use the following steps to integrate Apache Ignite with Spring Data Framework:
- Maven Configuration
- Spring Configuration
- Domain Model
- Apache Ignite Repository
- Sample Rest API
- Testing the Repository
Prerequisites
For this tutorial, we are using JDK 1.8 and Spring Boot 2.2.3.RELEASE project with the following dependencies:
spring-boot-starter-actuator
spring-boot-starter-validation
spring-boot-starter-web
springfox-swagger2
springfox-swagger-ui
1. Maven Configuration
We will begin by adding the following Maven dependencies for our application that we are going to build. We will use Apache Ignite version 2.8.1 for our application.
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data_2.2</artifactId>
<version>2.8.1</version>
</dependency>
Here,
- ignite-core is required to create an Ignite Cluster
- ignite-spring-data_2.2 is required to integrate Spring Data with Apache Ignite
2. Spring Configuration
We will need the following configurations related to Spring Data integration:
We are achieving three main steps through the code shown above:
a) Setting up Ignite programmatically: To create an Ignite Instance programmatically, we just need to create a method that returns IgniteConfiguration
bean. This will be used to initialize an Ignite node with all the configuration that we have specified. Here, we are starting an Ignite node in embedded mode, which is in the same JVM where the application is running.
Note: Apache Ignite also provides two extensions that automate Ignite configuration in Spring Boot application:
ignite-spring-boot-autoconfigure-ext
- autoconfigures ignite server and client nodes within Spring Boot.ignite-spring-boot-thin-client-autoconfigure-ext
- autoconfigures Ignite Thin Client with Spring Boot.
b) Defining the Cache Configuration: We’ve also defined our PersonCache configuration. The setIndexedTypes() method in CacheConfiguration sets the SQL schema for the cache.
c) Adding Support for Apache Ignite Repositories: To enable Apache Ignite backed repositories in Spring Data, we have used @EnableIgniteRepositories(value="com.cache.*)
annotation, as shown in the code above. Here, we have specified the base package name in the value. The igniteInstance() method creates and passes the Ignite instance to IgniteRepositoryFactoryBean in order to get access to the Apache Ignite cluster.
3. Domain Model
To demonstrate the integration, we’ll build a simple application that stores Person objects into Ignite’s cache by using a Spring Data API.
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
@QuerySqlField(index = true)
private Long id;
@QuerySqlField
private String firstName;
/** Will NOT be visible for SQL engine. */
private String lastName;
private int age;
}
Here, we have used the annotation @QuerySqlField
to configure the Indexes, as well as queryable fields. id
is defined as an indexed field and firstName
is defined as a queryable field. @QuerySqlField
annotation enables querying the fields using SQL.
4. Apache Ignite Repository
Apache Ignite uses its own IgniteRepository which extends from Spring Data’s CrudRepository. This interface should be extended by all custom Spring Data repositories that need to store and query data located in an Apache Ignite cluster.
IgniteRepository supports standard CRUD methods, except a few that don’t require an id like below.
save(S entity)
save(Iterable<S> entities)
delete(T entity)
delete(Iterable<? extends T> entities)
We can also use custom SQL queries in the Repository. In the PersonRepository
we have defined some find methods using the Spring Data naming convention and Ignite’s queries.
5. Sample Rest API
Let’s create a sample Rest API to test our Spring Data Integration Application.
5.1 RestController
We will create some sample Rest endpoint to test our application.
5.2 Service
The Service is also very straight forward. It uses the repository methods to interact with the underlying data storage, which in our case is Ignite Cache.
We have used the @PostConstruct method to populate some sample data into the Cache during application startup.
Here, we have used save(key, value) method of IgniteRepository instead of save(entity) of CrudRepository. The reason for this is that the standard CrudRepository save(entity), save(entities), delete(entity) methods are not supported yet. Unique ID generations at the Cluster level is one of the reasons for not supporting those methods.
6. Testing the Rest API
We will use Swagger to test our application.
Since we have loaded some sample data during start-up, we can search Person using the GET API method.
We can also use the search API to search persons by age.
If we want to add a new Person then we can use the Post API.
If you would like to refer to the full code, do check:
https://github.com/sumanentc/spring-boot-ignite