Apache ignite with springboot jdbcTemplate example

1. The purpose of this post

Apache ignite is a memory-centric distributed database. It aims to provide fast and scalable data access to users. In this post , I would demo how to use springboot jdbcTemplate with apache ignite.

2. Environments

2.1 Develop enrironments

  • JDK 1.8
  • Apache ignite 2.6
  • Springboot 1.5.9.RELEASE

2.2 Apache ignite environments

  • The table create sql:
CREATE TABLE City (id LONG PRIMARY KEY, name VARCHAR);
  • The insert data sql: INSERT INTO PUBLIC.CITY (ID, NAME) VALUES(1, ‘Paris’);

  • Then we got this table: demo ignite table

3. Setup the maven pom.xml

Add ignite core dependency to maven pom like this:

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.ignite/ignite-core -->
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-core</artifactId>
            <version>2.6.0</version>
        </dependency>
    </dependencies>

4. Setup database configurations.

@Configuration
public class DatasourceConfig {
    @Bean(name = "dsIgnite")
    @Primary
    @ConfigurationProperties(prefix="spring.datasourceIgnite")
    public DataSource igniteDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcIgnite")
    @Autowired
    public JdbcTemplate igniteJdbcTemplate(@Qualifier("dsIgnite") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

5. The application.properties

spring.datasourceIgnite.url=jdbc:ignite:thin://10.21.16.1:10800,10.21.16.2:10800
spring.datasourceIgnite.driver=org.apache.ignite.IgniteJdbcDriver

6. The Domain class

public class City {
    private int id;
    private String name;

    //getter and setters of id and name
}

7. The DAO class

@Component
public class CityDao {
    @Autowired
    @Qualifier("jdbcIgnite") #1
    private JdbcTemplate jdbcTemplateObject;

    public City getCity(Integer id) {
        String SQL = "select * from CITY where ID = ?";
        City city = jdbcTemplateObject.queryForObject(SQL,
                new Object[]{id}, new CityMapper());

        return city;
    }
    public List<City> getCities() {
        String SQL = "select * from CITY";
        List<City> students = jdbcTemplateObject.query(SQL, new CityMapper());
        return students;
    }

    class CityMapper implements RowMapper<City> {
        @Override
        public City mapRow(ResultSet rs, int rowNum) throws SQLException {
            City student = new City();
            student.setId(rs.getInt("ID"));
            student.setName(rs.getString("NAME"));

            return student;
        }
    }
}
  • The line #1 means, I would like to inject the jdbcIgite jdbcTemplate object which defined in the DatasourceConfig class

8. The unit test class

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestIgniteDataSource {

    @Autowired
    private CityDao cityDao;

    @Test
    public void testSimpleSelect() {
        City city = cityDao.getCity(1);
        assertNotNull(city);
        assertEquals(city.getName(),"Paris");
    }

    @Test
    public void testSimpleListSelect() {
        List<City> cities = cityDao.getCities();
        assertNotNull(cities);
        assertEquals(cities.size(),1);
    }
}

And run the unit test , we got this result: ignite test result

To summarize, the apache ignite is a very comprehensive memory database, it provides JDBC driver and it’s very easy for java developers to use it.

You can find the full source code on github bswen project