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 useSpring 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:

  1. Maven Configuration
  2. Spring Configuration
  3. Domain Model
  4. Apache Ignite Repository
  5. Sample Rest API
  6. 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:

@Configuration
	@EnableIgniteRepositories(value = "com.cache.*")
	public class IgniteCacheConfiguration {

	    @Bean
	    public Ignite igniteInstance() {
	        return Ignition.start(igniteConfiguration());
	    }

	    @Bean(name = "igniteConfiguration")
	    public IgniteConfiguration igniteConfiguration() {
	        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
	        igniteConfiguration.setIgniteInstanceName("testIgniteInstance");
	        //igniteConfiguration.setClientMode(true);
	        igniteConfiguration.setPeerClassLoadingEnabled(true);
	        igniteConfiguration.setLocalHost("127.0.0.1");

	        TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi();
	        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
	        ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
	        tcpDiscoverySpi.setIpFinder(ipFinder);
	        tcpDiscoverySpi.setLocalPort(47500);
	        // Changing local port range. This is an optional action.
	        tcpDiscoverySpi.setLocalPortRange(9);
	        //tcpDiscoverySpi.setLocalAddress("localhost");
	        igniteConfiguration.setDiscoverySpi(tcpDiscoverySpi);

	        TcpCommunicationSpi communicationSpi = new TcpCommunicationSpi();
	        communicationSpi.setLocalAddress("localhost");
	        communicationSpi.setLocalPort(48100);
	        communicationSpi.setSlowClientQueueLimit(1000);
	        igniteConfiguration.setCommunicationSpi(communicationSpi);

	        igniteConfiguration.setCacheConfiguration(cacheConfiguration());

	        return igniteConfiguration;

	    }

	    @Bean(name = "cacheConfiguration")
	    public CacheConfiguration[] cacheConfiguration() {
	        List<CacheConfiguration> cacheConfigurations = new ArrayList<>();           
	            // Defining and creating a new cache to be used by Ignite Spring Data
	            // repository.
	            CacheConfiguration ccfg = new CacheConfiguration("PersonCache");
	            // Setting SQL schema for the cache.
	            ccfg.setIndexedTypes(Long.class, Person.class);

	            cacheConfigurations.add(ccfg);

	        return cacheConfigurations.toArray(new CacheConfiguration[cacheConfigurations.size()]);
	    }
	}
view raw
spring-data hosted with ❤ by GitHub

#spring-boot #spring-data #apache-ignite #data analysis

Apache Ignite With Spring Data
11.20 GEEK