Skip to content

元ドキュメント: データ読み取り

データ読み取り

概要

本ドキュメントでは、TDSQL Boundless の HBase 互換モードにおけるデータ読み取り操作について説明します。Get、Scan、BatchGet 等の API の使い方、フィルタリング、バージョン指定読み取りについて解説します。

Get — 単一行の取得

指定した行キーに対応するデータを取得します。

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

public class GetExample {

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

            // 特定のカラムファミリーを指定
            get.addFamily(Bytes.toBytes("cf"));

            // 特定のカラムを指定
            get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"));

            Result result = table.get(get);
            byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"));
            System.out.println("値: " + Bytes.toString(value));
        }
    }
}

Scan — 範囲スキャン

行キーの範囲を指定してデータをスキャンします。

java
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

public class ScanExample {

    public static void scanRows(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            Scan scan = new Scan();

            // 開始行キーと終了行キーの指定
            scan.withStartRow(Bytes.toBytes("row_001"));
            scan.withStopRow(Bytes.toBytes("row_100"));

            // スキャン対象のカラムファミリーを指定
            scan.addFamily(Bytes.toBytes("cf"));

            // 返却行数の上限を設定
            scan.setLimit(50);

            try (ResultScanner scanner = table.getScanner(scan)) {
                for (Result result : scanner) {
                    String rowKey = Bytes.toString(result.getRow());
                    String value = Bytes.toString(
                            result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name")));
                    System.out.printf("行キー: %s, 値: %s%n", rowKey, value);
                }
            }
        }
    }
}

BatchGet — バッチ取得

複数の行キーを一括で取得します。

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

public class BatchGetExample {

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

            Result[] results = table.get(gets);
            for (Result result : results) {
                if (!result.isEmpty()) {
                    String rowKey = Bytes.toString(result.getRow());
                    String value = Bytes.toString(
                            result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name")));
                    System.out.printf("行キー: %s, 値: %s%n", rowKey, value);
                }
            }
        }
    }
}

フィルタリング

SingleColumnValueFilter

特定のカラムの値に基づいてフィルタリングします。

java
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.BinaryComparator;

public class FilterExample {

    public static void scanWithFilter(Connection connection) throws Exception {
        try (Table table = connection.getTable(TableName.valueOf("my_table"))) {
            Scan scan = new Scan();

            SingleColumnValueFilter filter = new SingleColumnValueFilter(
                    Bytes.toBytes("cf"),
                    Bytes.toBytes("status"),
                    CompareFilter.CompareOp.EQUAL,
                    new BinaryComparator(Bytes.toBytes("active"))
            );
            filter.setFilterIfMissing(true);

            scan.setFilter(filter);

            try (ResultScanner scanner = table.getScanner(scan)) {
                for (Result result : scanner) {
                    System.out.println("行キー: " + Bytes.toString(result.getRow()));
                }
            }
        }
    }
}

PrefixFilter

行キーのプレフィックスに基づいてフィルタリングします。

java
import org.apache.hadoop.hbase.filter.PrefixFilter;

Scan scan = new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes("user_")));

バージョン指定読み取り

特定のバージョン数を指定してデータを読み取ります。

java
Get get = new Get(Bytes.toBytes("row_key_001"));
get.readVersions(3);  // 最新 3 バージョンを取得

Result result = table.get(get);

タイムスタンプ範囲を指定して読み取ることも可能です。

java
Get get = new Get(Bytes.toBytes("row_key_001"));
get.setTimeRange(startTimestamp, endTimestamp);

Result result = table.get(get);

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