Bigtable でカウンタを作成して更新する
集計を使用して Bigtable でカウンタを作成して更新する方法について説明します。集計とは、書き込み時に値を集計するテーブル セルです。このクイックスタートでは、Google Cloud CLI と cbt CLI を使用して 3 つのカウンタを作成します。
- 実行中の合計を保持するカウンタ
- 追加されたすべての値の最小値を追跡するカウンタ
- 追加されたすべての値の最大値を追跡するカウンタ
始める前に
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init -
gcloud CLI を初期化した後に更新して、必要なコンポーネントをインストールします。
gcloud components update gcloud components install cbt
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable API and Cloud Bigtable Admin API APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com -
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init -
gcloud CLI を初期化した後に更新して、必要なコンポーネントをインストールします。
gcloud components update gcloud components install cbt
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable API and Cloud Bigtable Admin API APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com - 次のコマンドを実行して、gcloud CLI が最新の状態であり、
cbtCLI が含まれていることを確認します。gcloud components updategcloud components install cbt bigtable instances createコマンドを使用して、インスタンスを作成します。gcloud bigtable instances create counters-quickstart-instance \ --display-name="Counters quickstart instance" \ --cluster-config=id="counters-quickstart-cluster",zone="us-east1-c"プロジェクトとインスタンスを使用するように
cbtCLI を構成するため、.cbtrcファイルを作成します。echo project = PROJECT_ID >> ~/.cbtrc && echo instance = counters-quickstart-instance >> ~/.cbtrcPROJECT_ID は、使用しているプロジェクトの ID に置き換えます。
.cbtrcファイルを正しく設定したことを確認します。cat ~/.cbtrcターミナルに
.cbtrcファイルの内容が次のように表示されます。project = PROJECT_ID instance = counters-quickstart-instance
これで、インスタンスで
cbtCLI を使用できるようになりました。cbt createtableコマンドを使用して、3 つの集計列ファミリーを持つcounters_quickstart_tableという名前のテーブルを作成します。各列ファミリーを異なる集計タイプで構成します。- 列ファミリー
max_familyはMax型で、入力型はIntegerです。 - 列ファミリー
min_familyはMin型で、入力型はIntegerです。 - 列ファミリー
sum_familyはSum型で、入力型はIntegerです。
cbt createtable counters_quickstart_table families=sum_family:never:intsum,min_family:never:intmin,max_family:never:intmax- 列ファミリー
cbt lsコマンドを実行して、列ファミリーを一覧表示します。cbt ls counters_quickstart_tableシェルによって次のような出力が表示されます。
Family Name GC Policy ----------- --------- max_family <never> min_family <never> sum_family <never>cbt addtocellコマンドを使用して、行キーrow-key1とタイムスタンプ0を使用して、3 つの列ファミリーのそれぞれにある新しい列に初期値5を書き込みます。このオペレーションでは、カウンタとして使用する集計セルが作成されます。cbt addtocell counters_quickstart_table row-key1 sum_family:sum_column=5@0 cbt addtocell counters_quickstart_table row-key1 min_family:min_column=5@0 cbt addtocell counters_quickstart_table row-key1 max_family:max_column=5@0カウンタ値をバイトではなく整数として表示するには、
cbtCLI が出力のフォーマットに使用できるyamlファイルを定義します。以下のコマンドを実行します。echo "families:" > cbtformat.yaml echo " max_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yaml echo " min_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yaml echo " sum_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yamlcbtformat.yamlファイルを正しく設定したことを確認します。cat ~/cbtformat.yamlターミナルに
cbtformat.yamlファイルの内容が次のように表示されます。families: max_family: default_encoding: BigEndian default_type: INT64 min_family: default_encoding: BigEndian default_type: INT64 sum_family: default_encoding: BigEndian default_type: INT64cbt readコマンドを使用してyamlファイルを渡し、テーブルに追加したデータを読み取ります。テーブルには 3 つの列があり、それぞれ異なる集計タイプが設定されています。cbt read counters_quickstart_table format-file=$HOME/cbtformat.yamlシェルによって次のような出力が表示されます。値は整数としてフォーマットされ、タイムスタンプは UTC 形式です。
row-key1 max_family:max_column @ 1970/01/01-00:00:00.000000 5 min_family:min_column @ 1970/01/01-00:00:00.000000 5 sum_family:sum_column @ 1970/01/01-00:00:00.000000 5セルを作成したときに使用した同じタイムスタンプを使用して、テーブルの各列に値 3 を追加します。各列で、セルの値はセルの集計タイプに基づいて既存の値と統合されます。
cbt addtocell counters_quickstart_table row-key1 sum_family:sum_column=3@0 cbt addtocell counters_quickstart_table row-key1 min_family:min_column=3@0 cbt addtocell counters_quickstart_table row-key1 max_family:max_column=3@0cbt readコマンドをもう一度使用して、テーブル内のデータを読み取ります。各セルに集計値が表示されます。cbt read counters_quickstart_table format-file=$HOME/cbtformat.yamlsum_columnには 5 と 3 の合計(8)が格納され、min_columnには書き込まれた 2 つの値の最小値(3)が格納され、max_columnには書き込まれた 2 つの値の最大値(5)が格納されます。row-key1 max_family:max_column @ 1970/01/01-00:00:00.000000 5 min_family:min_column @ 1970/01/01-00:00:00.000000 3 sum_family:sum_column @ 1970/01/01-00:00:00.000000 8省略可: Google Cloud コンソールで SQL を使用してテーブルに対してクエリを実行します。
Google Cloud コンソールで、[Bigtable インスタンス] ページを開きます。
リストから
counters-quickstart-instanceを選択します。ナビゲーション メニューで [Bigtable Studio] をクリックします。
[エディタ] タブをクリックします。
次のクエリをエディタに貼り付けます。
SELECT * FROM `counters_quickstart_table`[実行] をクリックします。クエリの結果は [結果] テーブルに表示され、次のようになります。
_key max_family min_family sum_family row-key1 { "max_column": 5 } { "min_column": 5 } { "sum_column": 8 } ターミナルで、テーブル
counters_quickstart_tableを削除します。cbt deletetable counters_quickstart_tableインスタンスを削除します。
cbt deleteinstance counters-quickstart-instance.cbtrcファイルを削除します。rm ~/.cbtrcフォーマット ファイルを削除します。
rm ~/cbtformat.yaml(省略可)gcloud CLI から認証情報を取り消します。
gcloud auth revoke
Bigtable インスタンスを作成する
インスタンスに接続する
集計列ファミリーを含むテーブルを作成する
テーブルにカウンタを作成する
データを読み取る
カウンタを更新する
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、リソースを含む Google Cloud プロジェクトを削除します。