Apache Ignite JavaThinClient 入门ガイド - キー値(SQL)ストレージ

Apache Ignite 概要

Apache Ignite は、水平スケーラブルでフェールセーフな分布式 SQL データベースです。データは、リプリケーションまたはパーティション形式でクラスタ内で管理されます。

Ignite は、SQL データベースとして、SELECT、UPDATE、INSERT、DELETE などの DML 操作をサポートします。また、distributed system 特有の一部の DDL 操作も実装されています。

Ignite と接続する方法はさまざまで、JDBC または ODBC ドライバーを介して外部ツールやアプリケーションと連携できます。さらに、Java、.NET、C++ 開発者は、Ignite のネイティブ SQL API を利用することができます。

Maven 依存関係

使用するバージョンは、クラスタのバージョンと一致させる必要があります。

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-core</artifactId>
    <version>2.8.0</version>
</dependency>

キー値ストレージ

キー値ストレージの基本的な使用方法を以下に示します。

public void basicKVOperations() {
    // 方法1: 簡単な設定
    ClientConfiguration simpleCfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
    try (IgniteClient client = Ignition.startClient(simpleCfg)) {
        ClientCache<String, String> cache = client.cache("myCache"); // 現在存在するキャッシュを取得
        cache.put("キー", "値"); // データを格納
        String 取得値 = cache.get("キー"); // データを取得
        cache.remove("キー"); // データを削除
    } catch (Exception e) {
        e.printStackTrace();
    }

    // 方法2: カスタム設定
    ClientCacheConfiguration customCacheCfg = new ClientCacheConfiguration();
    customCacheCfg.setName("myCache");
    customCacheCfg.setOnheapCacheEnabled(true);
    customCacheCfg.setBackups(1); // 1つのバックアップを設定
    customCacheCfg.setCacheMode(CacheMode.PARTITIONED); // パーティションモードを使用
    customCacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    customCacheCfg.setPartitionLossPolicy(PartitionLossPolicy.READ_WRITE_SAFE);
    customCacheCfg.setDataRegionName("Default_Region"); // デフォルトリージョンを使用

    ClientConfiguration advancedCfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
    try (IgniteClient client = Ignition.startClient(advancedCfg)) {
        ClientCache<String, String> customCache = client.getOrCreateCache(customCacheCfg);
        customCache.put("キー", "値");
        String 取得値 = customCache.get("キー");
        customCache.remove("キー");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

SQL ストレージ

SQL ストレージの使用例を以下に示します。

public void basicSQLOperations() {
    ClientCacheConfiguration sqlCacheCfg = new ClientCacheConfiguration();
    sqlCacheCfg.setName("マイSQLキャッシュ名"); // キャッシュ名
    sqlCacheCfg.setOnheapCacheEnabled(true);
    sqlCacheCfg.setBackups(1);
    sqlCacheCfg.setCacheMode(CacheMode.PARTITIONED);
    sqlCacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    sqlCacheCfg.setPartitionLossPolicy(PartitionLossPolicy.READ_ONLY_SAFE);
    sqlCacheCfg.setDataRegionName("Default_Region");
    sqlCacheCfg.setSqlSchema("マイSQLスキーマ名"); // データベース名として使用

    ClientConfiguration sqlClientCfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
    try (IgniteClient client = Ignition.startClient(sqlClientCfg)) {
        ClientCache<String, String> sqlCache = client.getOrCreateCache(sqlCacheCfg);
        
        // テーブル作成
        List<List<?>> createResult = sqlCache.query(new SqlFieldsQuery(
            "CREATE TABLE IF NOT EXISTS テストテーブル (" +
            "  `id` varchar(40)," +
            "  `name` varchar(64)," +
            "  `age` varchar(32)," +
            "  `creator` varchar(15)," +
            "  `create_time` timestamp," +
            "  PRIMARY KEY (`id`)" +
            ") WITH \"TEMPLATE=cacheTemplate\"")).getAll();

        // レコード挿入
        String insertQuery = "INSERT INTO テストテーブル (id, name, age, creator, create_time) " +
                            "VALUES(?, ?, ?, ?, ?)";
        List<List<?>> insertResult = sqlCache.query(new SqlFieldsQuery(insertQuery)
            .setArgs("id", "name", "17", "levi", new Date())).getAll();

        // レコード検索
        List<List<?>> selectResult = sqlCache.query(new SqlFieldsQuery(
            "SELECT COUNT(1) FROM テストテーブル")).getAll();
        System.out.println(selectResult.get(0).get(0));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

タグ: Apache Ignite JavaThinClient キー値ストレージ SQLストレージ DML

7月4日 20:01 投稿