Skip to content

元ドキュメント: データ書き込み

データ書き込み

概要

本ドキュメントでは、TDSQL Boundless の HBase 互換モードにおけるデータ書き込み操作について説明します。Put、Delete、BatchPut 等の API の使い方を解説します。

Put — 単一行の書き込み

指定した行キーにデータを書き込みます。

java
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class PutExample {

    public static void putRow(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            Put put = new Put(Bytes.toBytes("row_key_001"));

            // カラムファミリーとカラムに値を設定
            put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("田中太郎"));
            put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("email"), Bytes.toBytes("tanaka@example.com"));
            put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"), Bytes.toBytes("30"));

            table.put(put);
            System.out.println("データの書き込みが完了しました。");
        }
    }
}

タイムスタンプの指定

特定のタイムスタンプを指定して書き込むことが可能です。

java
Put put = new Put(Bytes.toBytes("row_key_001"));
long timestamp = System.currentTimeMillis();
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), timestamp, Bytes.toBytes("田中太郎"));

Delete — データの削除

単一行の削除

java
import org.apache.hadoop.hbase.client.Delete;

public class DeleteExample {

    public static void deleteRow(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            Delete delete = new Delete(Bytes.toBytes("row_key_001"));
            table.delete(delete);
            System.out.println("行を削除しました。");
        }
    }
}

特定カラムの削除

java
Delete delete = new Delete(Bytes.toBytes("row_key_001"));

// 特定のカラムの最新バージョンを削除
delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("email"));

// 特定のカラムの全バージョンを削除
delete.addColumns(Bytes.toBytes("cf"), Bytes.toBytes("email"));

table.delete(delete);

カラムファミリーの削除

java
Delete delete = new Delete(Bytes.toBytes("row_key_001"));
delete.addFamily(Bytes.toBytes("cf"));
table.delete(delete);

BatchPut — バッチ書き込み

複数の行を一括で書き込みます。

java
import java.util.ArrayList;
import java.util.List;

public class BatchPutExample {

    public static void batchPut(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            List<Put> puts = new ArrayList<>();

            for (int i = 1; i <= 100; i++) {
                Put put = new Put(Bytes.toBytes(String.format("row_%03d", i)));
                put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"),
                        Bytes.toBytes("ユーザー" + i));
                put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("status"),
                        Bytes.toBytes("active"));
                puts.add(put);
            }

            table.put(puts);
            System.out.println(puts.size() + " 件のデータを一括書き込みしました。");
        }
    }
}

BatchDelete — バッチ削除

複数の行を一括で削除します。

java
public class BatchDeleteExample {

    public static void batchDelete(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            List<Delete> deletes = new ArrayList<>();
            deletes.add(new Delete(Bytes.toBytes("row_001")));
            deletes.add(new Delete(Bytes.toBytes("row_002")));
            deletes.add(new Delete(Bytes.toBytes("row_003")));

            table.delete(deletes);
            System.out.println("バッチ削除が完了しました。");
        }
    }
}

CheckAndPut — 条件付き書き込み

指定した条件を満たす場合にのみ書き込みを行います。

java
public class CheckAndPutExample {

    public static void checkAndPut(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            Put put = new Put(Bytes.toBytes("row_key_001"));
            put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("status"), Bytes.toBytes("inactive"));

            // status が "active" の場合にのみ更新
            boolean success = table.checkAndMutate(Bytes.toBytes("row_key_001"), Bytes.toBytes("cf"))
                    .qualifier(Bytes.toBytes("status"))
                    .ifEquals(Bytes.toBytes("active"))
                    .thenPut(put);

            System.out.println("条件付き書き込み結果: " + (success ? "成功" : "条件不一致のためスキップ"));
        }
    }
}

注意事項

  • 大量データの書き込みにはバッチ書き込み(BatchPut)を使用し、1 件ずつの Put を繰り返す方法は避けてください。
  • 書き込み後にリソースを適切に解放してください。
  • 本番環境では、書き込みエラー発生時のリトライ処理を実装することを推奨します。

Tencent Cloud プロダクトドキュメント