使用 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. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    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. Verify 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 the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  11. 如要初始化 gcloud CLI,請執行下列指令:

    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. Verify 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 the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  16. 執行下列指令,安裝 cbt CLI :
    gcloud components install cbt
  17. 建立 Bigtable 執行個體

    1. 在 Google Cloud 控制台中開啟「建立執行個體」頁面。

      建立執行個體

    2. 在「Instance name」(執行個體名稱) 中輸入 Quickstart instance

    3. 在「Instance ID」(執行個體 ID) 中輸入 quickstart-instance

    4. 按一下「繼續」

    5. 在「儲存類型」部分,請選擇「SSD」

    6. 按一下「繼續」

    7. 在「Cluster ID」(叢集 ID) 中輸入 quickstart-instance-c1

    8. 在「區域」部分,選取您附近的區域。

    9. 在「可用區」部分,選取「任何」

    10. 針對「節點資源調度模式」,選擇「手動分配」

    11. 在「數量」部分,選取「1」

    12. 按一下「建立」,建立執行個體。

    連線至執行個體

    1. 建立 .cbtrc 檔案,將 PROJECT_ID 替換為您建立 Bigtable 執行個體的專案 ID,藉此設定 cbt CLI 使用您的專案和執行個體:

      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. 新增名為 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"

      同一欄在第 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

    後續步驟