1. 概要

このクイックガイドでは、 SpringDataAPIをApacheIgniteプラットフォームと統合する方法に焦点を当てます。

Apache Igniteの詳細については、以前のガイドをご覧ください。

2. Mavenのセットアップ

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

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

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

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 = "baeldungCache")
public interface EmployeeRepository 
  extends IgniteRepository<EmployeeDTO, Integer> {
    EmployeeDTO getEmployeeDTOById(Integer id);
}

Apache Igniteは、SpringDataのCrudRepositoryから拡張された独自のIgniteRepositoryを使用します。 また、SpringDataからSQLグリッドにアクセスできるようにします。

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

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

4. スプリング構成

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

@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);
    }
}

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

また、baeldungCache構成を定義および設定しました。 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)メソッドを使用しました。 この理由は標準のCrudRepositorysave(entity)、save(entities)、delete(entity)操作はまだサポートされていません。

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

代わりに、 保存 (キー、値)、save(マップ値)、deleteAll(Iterable ids) メソッド。

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

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

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

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

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

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

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

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

6. 概要

この短いチュートリアルでは、SpringDataFrameworkをApacheIgniteプロジェクトと統合する方法を示します。 実際の例の助けを借りて、 SpringDataAPIを使用してApacheIgniteキャッシュを操作する方法を学びました。

いつものように、この記事の完全なコードはGitHubプロジェクトで入手できます。