Apache Ignite with Spring Data – Apache Ignite与Spring Data

最后修改: 2018年 4月 23日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick guide, we’re going to focus on how to integrate the Spring Data API with the Apache Ignite platform.

在这份快速指南中,我们将重点介绍如何将Spring Data API与Apache Ignite平台集成。

To learn about Apache Ignite check out our previous guide.

要了解Apache Ignite,请查看我们之前的指南

2. Maven Setup

2.Maven的设置

In addition to the existing dependencies, we have to enable Spring Data support:

除了现有的依赖关系外,我们还必须启用Spring Data支持。

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring-data</artifactId>
    <version>${ignite.version}</version>
</dependency>

The ignite-spring-data artifact can be downloaded from Maven Central.

ignite-spring-data工件可以从Maven中心下载。

3. Model and Repository

3.模型和存储库

To demonstrate the integration, we’ll build an application which stores employees into Ignite’s cache by using a Spring Data API.

为了演示集成,我们将建立一个应用程序,通过使用Spring Data API将employees存储到Ignite的缓存。

The POJO of the EmployeeDTO will look like this:

EmployeeDTO的POJO将看起来像这样。

public class EmployeeDTO implements Serializable {
 
    @QuerySqlField(index = true)
    private Integer id;
    
    @QuerySqlField(index = true)
    private String name;
    
    @QuerySqlField(index = true)
    private boolean isEmployed;

    // getters, setters
}

Here, the @QuerySqlField annotation enables querying the fields using SQL.

在这里,@QuerySqlField注解能够使用SQL查询字段。

Next, we’ll create the repository to persist the Employee objects:

接下来,我们将创建存储库来保存Employee对象。

@RepositoryConfig(cacheName = "baeldungCache")
public interface EmployeeRepository 
  extends IgniteRepository<EmployeeDTO, Integer> {
    EmployeeDTO getEmployeeDTOById(Integer id);
}

Apache Ignite uses its own IgniteRepository which extends from Spring Data’s CrudRepository. It also enables access to the SQL grid from Spring Data. 

Apache Ignite使用自己的IgniteRepository,它从Spring Data的CrudRepository扩展而来。它还能够从Spring Data访问SQL网格。

This supports standard CRUD methods, except a few that don’t require an id. We’ll take a look at why in more detail in our testing section.

这支持标准的CRUD方法,除了一些不需要ID的方法。我们将在测试部分更详细地看看原因。

The @RepositoryConfig annotation maps the EmployeeRepository to Ignite’s baeldungCache.

@RepositoryConfig注解将EmployeeRepository映射到Ignite的baeldungCache

4. Spring Configuration

4.Spring配置

Let’s now create our Spring configuration class.

现在让我们来创建我们的Spring配置类。

We’ll use the @EnableIgniteRepositories annotation to add support for Ignite repositories:

我们将使用@EnableIgniteRepositories注解来增加对Ignite存储库的支持:

@Configuration
@EnableIgniteRepositories
public class SpringDataConfig {

    @Bean
    public Ignite igniteInstance() {
        IgniteConfiguration config = new IgniteConfiguration();

        CacheConfiguration cache = new CacheConfiguration("baeldungCache");
        cache.setIndexedTypes(Integer.class, EmployeeDTO.class);

        config.setCacheConfiguration(cache);
        return Ignition.start(config);
    }
}

Here, the igniteInstance() method creates and passes the Ignite instance to IgniteRepositoryFactoryBean in order to get access to the Apache Ignite cluster.

这里,igniteInstance()方法创建并将Ignite实例传递给IgniteRepositoryFactoryBean,以获得对Apache Ignite集群的访问。

We’ve also defined and set the baeldungCache configuration. The setIndexedTypes() method sets the SQL schema for the cache.

我们还定义并设置了baeldungCache配置。setIndexedTypes()方法为缓存设置了SQL模式。

5. Testing the Repository

5.测试存储库

To test the application, let’s register the SpringDataConfiguration in the application context and get the EmployeeRepository from it:

为了测试该应用程序,让我们在应用程序上下文中注册SpringDataConfiguration,并从中获取EmployeeRepository

AnnotationConfigApplicationContext context
 = new AnnotationConfigApplicationContext();
context.register(SpringDataConfig.class);
context.refresh();

EmployeeRepository repository = context.getBean(EmployeeRepository.class);

Then, we want to create the EmployeeDTO instance and save it in the cache:

然后,我们要创建EmployeeDTO实例并将其保存在缓存中。

EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setId(1);
employeeDTO.setName("John");
employeeDTO.setEmployed(true);

repository.save(employeeDTO.getId(), employeeDTO);

Here we used the save(key, value) method of IgniteRepository. The reason for this is that the standard CrudRepository save(entity), save(entities), delete(entity) operations aren’t supported yet.

这里我们使用了save(key, value)方法的IgniteRepository。原因是标准的CrudRepository save(entity), save(entities), delete(entity) 操作还不被支持

The issue behind this is that the IDs generated by the CrudRepository.save() method are not unique in the cluster.

这背后的问题是,由CrudRepository.save()方法生成的ID在集群中并不唯一。

Instead, we have to use the save(key, value), save(Map<ID, Entity> values), deleteAll(Iterable<ID> ids) methods.

相反,我们必须使用save(key, value), save(Map<ID, Entity> values), deleteAll(Iterable< ID> ids) 方法。

After, we can get the employee object from the cache by using Spring Data’s getEmployeeDTOById() method:

之后,我们可以通过Spring Data的getEmployeeDTOById()方法从缓存中获取雇员对象。

EmployeeDTO employee = repository.getEmployeeDTOById(employeeDTO.getId());
System.out.println(employee);

The output shows that we successfully fetched the initial object:

输出显示,我们成功地获取了初始对象。

EmployeeDTO{id=1, name='John', isEmployed=true}

Alternatively, we can retrieve the same object using the IgniteCache API:

另外,我们可以使用IgniteCache API来检索同一对象。

IgniteCache<Integer, EmployeeDTO> cache = ignite.cache("baeldungCache");
EmployeeDTO employeeDTO = cache.get(employeeId);

Or by using the standard SQL:

或者通过使用标准SQL。

SqlFieldsQuery sql = new SqlFieldsQuery(
  "select * from EmployeeDTO where isEmployed = 'true'");

6. Summary

6.总结

This short tutorial shows how to integrate the Spring Data Framework with the Apache Ignite project. With the help of the practical example, we learned to work with the Apache Ignite cache by using the Spring Data API.

这个简短的教程展示了如何将Spring数据框架与Apache Ignite项目集成。在实际例子的帮助下,我们学会了通过使用Spring Data API与Apache Ignite缓存进行协作。

As usual, the complete code for this article is available in the GitHub project.

像往常一样,本文的完整代码可在GitHub项目中找到。