[A-00220] Cloud Bigtableの使い方
Google Cloud Bigtableの使い方メモ
GCP上で使用すると非常に高額な課金が課される為、主にローカルエミュレータによる動作検証です。
・Bigtable Emulatorを起動する
まずgcloudコンポーネントをアップデートします。
gcloud components update beta
環境変数にエミュレータのホストとportを記載します。
export BIGTABLE_EMULATOR_HOST=localhost:8086
次にbigtableをコンソールで操作するためのcbtツールをインストールします。
gcloud components update
gcloud components install cbt
次にcbtの設定ファイルを作成します。
project = fake
instance = fake
creds = fake
admin-endpoint = localhost:8086
data-endpoint = localhost:8086
auth-token = fake
timeout = 30s
次にエミュレータを起動します。
gcloud beta emulators bigtable start
bigtableエミュレータを初期化します。
$(gcloud beta emulators bigtable env-init)
テスト用のテーブルを作成します。
cbt createtable student_class_school_master
列ファミリーを作成します。
cbt createfamily student_class_school_master student
cbt createfamily student_class_school_master class
cbt createfamily student_class_school_master school
次に適当なデータを入力します。
cbt set student_class_school_master 123456#elementary student:student_id=123456 student:student_name=yoshio-kubota class:class_grade=5 class:class_number=1 school:school_type=elementary
データを読み取りします。
cbt read student_class_school_master
user@usernoMBP ~ % cbt read student_class_school_master
----------------------------------------
123456#elementary
class:class_grade @ 2024/11/13-01:37:51.189000
"5"
class:class_number @ 2024/11/13-01:37:51.189000
"1"
school:school_type @ 2024/11/13-01:37:51.189000
"elementary"
student:student_id @ 2024/11/13-01:37:51.189000
"123456"
student:student_name @ 2024/11/13-01:37:51.189000
"yoshio-kubota"
Javaからデータを読み取ってみます。pom.xmlに下記の依存性を追加する。
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigtable</artifactId>
<version>2.45.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-client-core</artifactId>
<version>1.29.2</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-hbase</artifactId>
<version>2.14.5</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-hbase-1.x</artifactId>
<version>1.9.0</version>
</dependency>
package com.example.etl.plane;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
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;
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
public class BigtableConnectionPractice {
public static void main(String[] args) throws Exception {
String projectId = "fake";
String instanceId = "fake";
String tableId = "student_class_school_master";
Configuration config = BigtableConfiguration.configure(projectId, instanceId);
config.set(BigtableOptionsFactory.BIGTABLE_ADMIN_HOST_KEY, "localhost");
config.set(BigtableOptionsFactory.BIGTABLE_HOST_KEY, "localhost");
config.set(BigtableOptionsFactory.BIGTABLE_PORT_KEY, "8086");
config.set(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, "true");
config.set(BigtableOptionsFactory.BIGTABLE_USE_PLAINTEXT_NEGOTIATION, "true");
try (Connection con = BigtableConfiguration.connect(config)) {
try(Table table = con.getTable(TableName.valueOf(tableId))) {
String rowKey = "123456#elementary";
Result result = table.get(new Get(Bytes.toBytes(rowKey)));
//String rowValue = Bytes.toString(result.value());
String rowValue = Bytes.toString(result.getValue("student".getBytes(), "student_name".getBytes()));
System.out.println(rowValue);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
}
}
}
実行結果は省略しますが上記を実行してコンソールにstudent_nameが表示されればOKです。
・Appendix
参考サイトはこちら
コメントを残す