cbt CLI でインスタンスを作成してデータを書き込む

Bigtable について学習する場合は、クイックスタートに一通り目を通しておくと、本番環境で大規模に使用することになる基礎を実際に体験できます。

このクイックスタートでは、次のアクションを実行します。

  • Bigtable インスタンスに接続する
  • 基本的な管理タスクを実行する
  • テーブルにデータを書き込む
  • テーブルからデータを読み取る

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  13. Make sure that billing is enabled for your Google Cloud project.

  14. Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  16. 次のコマンドを実行して cbt CLI をインストールします。
    gcloud components install cbt

Bigtable インスタンスを作成する

  1. Google Cloud Console で [インスタンスの作成] ページを開きます。

    インスタンスの作成

  2. [インスタンス名] に「Quickstart instance」と入力します。

  3. [インスタンス ID] に「quickstart-instance」と入力します。

  4. [続行] をクリックします。

  5. [ストレージの種類] で [SSD] を選択します。

  6. [続行] をクリックします。

  7. [クラスタ ID] に「quickstart-instance-c1」と入力します。

  8. [リージョン] で、お近くのリージョンを選択します。

  9. [ゾーン] で [すべて] を選択します。

  10. [ノード スケーリング モード] で [手動で割り当てる] を選択します。

  11. [数量] で [1] を選択します。

  12. [作成] をクリックしてインスタンスを作成します。

インスタンスへの接続

  1. プロジェクトとインスタンスを使用するように cbt CLI を構成するため、.cbtrc ファイルを作成し、PROJECT_ID を Bigtable インスタンスの作成先であるプロジェクトの ID に置き換えます。

    echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc
    
  2. .cbtrc ファイルを正しく設定したことを確認します。

    cat ~/.cbtrc

    ターミナルに .cbtrc ファイルの内容が次のように表示されます。

    project = PROJECT_ID
    instance = quickstart-instance

    これで、インスタンスで cbt CLI を使用できるようになりました。

データの読み取りと書き込み

Bigtable では、いくつもの行で構成されたテーブルにデータが格納されます。各行は行キーによって識別されます。

各行内のデータは、列のグループである「列ファミリー」に整理されます。列ファミリー内の個々の列は列修飾子によって識別されます。

行と列が交差する部分に、タイムスタンプ付きのセルが複数存在することがあります。

  1. テーブルを my-table という名前で作成します。

    cbt createtable my-table
  2. テーブルの一覧を表示します。

    cbt ls

    コマンドによって次のような出力が表示されます。

        my-table

  3. 1 つの列ファミリーを cf1 という名前で追加します。

    cbt createfamily my-table cf1
  4. 列ファミリーを一覧表示します。

    cbt ls my-table

    コマンドによって次のような出力が表示されます。

        Family Name     GC Policy
        -----------     ---------
        cf1             <never>

  5. 列ファミリー cf1 と列修飾子 c1 を使用して、値 test-value1test-value2 を行 r1 に書き込みます。

    cbt set my-table r1 cf1:c1=test-value1
      cbt set my-table r1 cf1:c1=test-value2
    
  6. cbt read コマンドを使用して、テーブルに追加したデータを読み取ります。

    cbt read my-table

    Shell によって次のような出力が表示されます。

        ----------------------------------------
        r1
          cf1:c1                                   @ 2023/03/22-06:56:11.323000
            "test-value1"
          cf1:c1                                   @ 2023/03/22-06:56:04.361000
            "test-value2"

    タイムスタンプが同じ 2 つの値が、同じ列の r1 行に格納されます。

クリーンアップ

このクイックスタートで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、インスタンスを削除します。.cbtrc ファイルを削除すると、別のプロジェクトで作業できるようになります。

  1. テーブル my-table を削除します。

    cbt deletetable my-table
  2. インスタンスを削除します。

    cbt deleteinstance quickstart-instance
  3. ファイル .cbtrc を削除します。

    rm ~/.cbtrc

  4. (省略可)gcloud CLI から認証情報を取り消します。

    gcloud auth revoke

次のステップ