GridGain Developers Hub

GridGain With Spring Data

Overview

Spring Data Framework provides a unified and widely used API that allows abstracting an underlying data storage from the application layer. Spring Data helps you avoid locking to a specific database vendor, making it easy to switch from one database to another with minimal efforts. GridGain supports Spring Data JDBC by implementing IgniteDialect.

Maven Configuration

The easiest way to start working with GridGain’s Spring Data repository is by adding the following Maven dependencies to the application’s pom.xml file:

<dependency>
    <groupId>org.gridgain</groupId>
    <artifactId>spring-data-ignite</artifactId>
    <version>9.1.10</version>
</dependency>

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>spring-boot-starter-ignite-client</artifactId>
    <version>3.0.0</version>
</dependency>

Application Configuration

To start working with Spring Data, first provide the following properties to your application:

ignite.client.addresses=${gridgain.address}
spring.datasource.url=${gridgain.jdbc.connectionString}
spring.datasource.driver-class-name=org.apache.ignite.jdbc.IgniteJdbcDriver

In the example above:

  • Replace ${gridgain.address} with the host and port of your node. For example, localhost:10800.

  • Replace ${gridgain.jdbc.connectionString} with your JDBC connection string, for example jdbc:ignite:thin://localhost.

Here is an example of the application.properties file:

ignite.client.addresses=127.0.0.1:10800
spring.datasource.url=jdbc:ignite:thin://localhost
spring.datasource.driver-class-name=org.apache.ignite.jdbc.IgniteJdbcDriver

You should also provide the IgniteDialectProvider parameter by adding the following configuration property to the resources/META-INF/spring.factories (create the file if it does not exist):

org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider=org.apache.ignite.data.IgniteDialectProvider

Creating Repositories

After providing the above configuration, you can create spring repositories. For instance, let’s create the first custom repository named PersonRepository:

@Repository
public interface PersonRepository extends IgniteRepository<Person, Long> {
    /**
     * Gets all the persons with the given name.
     * @param name Person name.
     * @return A list of Persons with the given first name.
     */
    public List<Person> findByFirstName(String name);

    /**
     * Returns top Person with the specified surname.
     * @param name Person surname.
     * @return Person that satisfy the query.
     */
    public Person findTopByLastNameLike(String name);

    /**
     * Getting ids of all the Person satisfying the custom query from {@link Query} annotation.
     *
     * @param orgId Query parameter.
     * @param pageable Pageable interface.
     * @return A list of Persons' ids.
     */
    @Query("SELECT id FROM Person WHERE orgId > ?")
    public List<Long> selectId(long orgId, Pageable pageable);
}

Signatures of custom methods like findByFirstName(name) and findTopByLastNameLike(name) will be automatically processed and turned into SQL queries when methods get executed. In addition, @Query(queryString) annotation can be used if a specific​ SQL query needs to be executed as a result of a method call.

Using Repositories

Now, you can put data in GridGain by using Spring Data API:

TreeMap<Long, Person> persons = new TreeMap<>();

persons.put(1L, new Person(1L, 2000L, "John", "Smith", 15000, "Worked for Apple"));

persons.put(2L, new Person(2L, 2000L, "Brad", "Pitt", 16000, "Worked for Oracle"));

persons.put(3L, new Person(3L, 1000L, "Mark", "Tomson", 10000, "Worked for Sun"));

// Adding data into the repository.
repo.save(persons);

To query the data, you can use CRUD operations or methods that will be automatically turned into GridGain SQL queries:

List<Person> persons = repo.findByFirstName("John");

for (Person person: persons)
    System.out.println("   >>>   " + person);

Person topPerson = repo.findTopByLastNameLike("Smith");

System.out.println("\n>>> Top Person with surname 'Smith': " +
        topPerson.getValue());