ApacheがSpring Dataに点火する

Spring Dataを使用したApache Ignite

1. 概要

このクイックガイドでは、how to integrate the Spring Data API with the Apache Ignite platform.に焦点を当てます。

Apache Igniteについては、our previous guideを確認してください。

2. Mavenセットアップ

既存の依存関係に加えて、Spring Dataサポートを有効にする必要があります。


    org.apache.ignite
    ignite-spring-data
    ${ignite.version}

ignite-spring-dataアーティファクトはMaven Centralからダウンロードできます。

3. モデルとリポジトリ

統合を示すために、Spring Data APIを使用してemployeesを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 = "exampleCache")
public interface EmployeeRepository
  extends IgniteRepository {
    EmployeeDTO getEmployeeDTOById(Integer id);
}

Apache Ignite uses its own IgniteRepository which extends from Spring Data’s CrudRepository. Spring DataからSQLグリッドへのアクセスも可能にします。 

これは、IDを必要としないいくつかを除いて、標準のCRUDメソッドをサポートします。 その理由については、テストセクションで詳しく説明します。

The @RepositoryConfigアノテーションは、EmployeeRepositoryをIgniteのexampleCacheにマップします。

4. スプリング構成

それでは、Spring構成クラスを作成しましょう。

@EnableIgniteRepositoriesアノテーションを使用して、Igniteリポジトリのサポートを追加します。

@Configuration
@EnableIgniteRepositories
public class SpringDataConfig {

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

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

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

ここで、igniteInstance()メソッドは、Apache Igniteクラスターにアクセスするために、Igniteインスタンスを作成してIgniteRepositoryFactoryBeanに渡します。

また、exampleCache構成を定義および設定しました。 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);

ここでは、IgniteRepositorysave(key, value)メソッドを使用しました。 この理由は、the standard CrudRepository save(entity), save(entities), delete(entity) operations aren’t supported yet.

この背後にある問題は、CrudRepository.save()メソッドによって生成されたIDがクラスター内で一意ではないことです。

代わりに、save(key, value), save(Map<ID, Entity> values), deleteAll(Iterable<ID> ids)メソッドを使用する必要があります。

その後、Spring DataのgetEmployeeDTOById()メソッドを使用して、キャッシュから従業員オブジェクトを取得できます。

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

出力は、初期オブジェクトを正常に取得したことを示しています。

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

または、IgniteCacheAPIを使用して同じオブジェクトを取得することもできます。

IgniteCache cache = ignite.cache("exampleCache");
EmployeeDTO employeeDTO = cache.get(employeeId);

または、標準SQLを使用して:

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

6. 概要

この短いチュートリアルでは、Spring Data FrameworkとApache Igniteプロジェクトを統合する方法を示します。 実用的な例の助けを借りて、we learned to work with the Apache Ignite cache by using the Spring Data API.

いつものように、この記事の完全なコードはGitHub projectで入手できます。