Contents

Apache Ignite 与 Spring Data 集成

1.概述

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

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

2. Maven 设置

除了现有的依赖项,我们还必须启用 Spring Data 支持:

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

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

3. 模型和存储库

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

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
}

在这里,@QuerySqlField注释允许使用 SQL 查询字段。

接下来,我们将创建存储库来持久化 Employee对象:

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

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

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

@RepositoryConfig注释将EmployeeRepository映射到 Ignite 的blogdemoCache。**

4. Spring配置

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

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

@Configuration
@EnableIgniteRepositories
public class SpringDataConfig {
    @Bean
    public Ignite igniteInstance() {
        IgniteConfiguration config = new IgniteConfiguration();
        CacheConfiguration cache = new CacheConfiguration("blogdemoCache");
        cache.setIndexedTypes(Integer.class, EmployeeDTO.class);
        config.setCacheConfiguration(cache);
        return Ignition.start(config);
    }
}

在这里,igniteInstance() 方法创建Ignite实例并将其传递给IgniteRepositoryFactoryBean以便访问 Apache Ignite 集群。

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

5. 测试存储库

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

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

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

EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setId(1);
employeeDTO.setName("John");
employeeDTO.setEmployed(true);
repository.save(employeeDTO.getId(), employeeDTO);

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

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

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

之后,我们可以使用 Spring Data 的*getEmployeeDTOById()*方法从缓存中获取员工对象:

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

输出显示我们成功获取了初始对象:

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

或者,我们可以使用IgniteCache API 检索相同的对象:

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

或者使用标准 SQL:

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