在 Bigtable 中建立及更新計數器

瞭解如何使用匯總在 Bigtable 中建立及更新計數器,也就是在寫入時匯總值的資料表儲存格。本快速入門導覽課程會使用 Google Cloud CLI 和 cbt CLI 建立三個計數器:

  • 持續計算總和的計數器
  • 追蹤所有新增值的最小值
  • 追蹤所有新增值最大值的計數器

事前準備

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  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. After initializing the gcloud CLI, update it and install the required components:

    gcloud components update
    gcloud components install cbt
  6. 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.

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

  8. Enable the Cloud Bigtable API and Cloud Bigtable Admin API APIs:

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  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. After initializing the gcloud CLI, update it and install the required components:

    gcloud components update
    gcloud components install cbt
  13. 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.

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

  15. Enable the Cloud Bigtable API and Cloud Bigtable Admin API APIs:

    gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com
  16. 執行下列指令,確保 gcloud CLI 為最新版本,且包含 cbt CLI:
    gcloud components update
    gcloud components install cbt

建立 Bigtable 執行個體

  1. 使用 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"
    

連線至執行個體

  1. 建立 .cbtrc 檔案,設定 cbt CLI 以使用您的專案和執行個體。

    echo project = PROJECT_ID >> ~/.cbtrc && echo instance = counters-quickstart-instance >> ~/.cbtrc
    

    PROJECT_ID 替換為您使用的專案 ID。

  2. 確認您已正確設定 .cbtrc 檔案。

    cat ~/.cbtrc
    

    終端機會顯示 .cbtrc 檔案的內容,類似於下列內容:

    project = PROJECT_ID
    instance = counters-quickstart-instance

    現在您可以使用 cbt CLI 執行個體。

建立具有匯總資料欄系列的資料表

  1. 使用 cbt createtable 指令建立名為 counters_quickstart_table 的資料表,其中包含三個匯總資料欄系列。為每個資料欄系列設定不同的匯總類型:

    • 資料欄系列 max_familyMax 類型,輸入類型為 Integer
    • 資料欄系列 min_familyMin 類型,輸入類型為 Integer
    • 資料欄系列 sum_familySum 類型,輸入類型為 Integer
    cbt createtable counters_quickstart_table families=sum_family:never:intsum,min_family:never:intmin,max_family:never:intmax
    
  2. 執行 cbt ls 指令,列出資料欄系列。

    cbt ls counters_quickstart_table
    

    shell 會顯示類似以下內容的輸出︰

    Family Name     GC Policy
    -----------     ---------
    max_family      <never>
    min_family      <never>
    sum_family      <never>
    

在表格中建立計數器

  1. 使用 cbt addtocell 指令,在三個資料欄系列中各寫入一個新資料欄,並使用 row-key1 的資料列鍵和 0 的時間戳記,將初始值設為 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
    

讀取資料

  1. 如要以整數而非位元組的形式查看計數器值,請定義 yaml 檔案,供 cbt CLI 用於格式化輸出內容。執行以下指令:

    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.yaml
    
  2. 確認您已正確設定 cbtformat.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: INT64
    
  3. 使用 cbt read 指令傳遞 yaml 檔案,並讀取您新增至資料表的資料。現在表格有三欄,每欄的匯總類型都不同。

    cbt read counters_quickstart_table format-file=$HOME/cbtformat.yaml
    

    殼層會顯示類似下列內容的輸出。這些值會格式化為整數,時間戳記則採用世界標準時間格式。

    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
    

更新計數器

  1. 使用您建立儲存格時相同的時間戳記,在表格的每個資料欄中新增值 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@0
    
  2. 再次使用 cbt read 指令讀取資料表中的資料。每個儲存格現在都包含匯總值。

    cbt read counters_quickstart_table format-file=$HOME/cbtformat.yaml
    

    sum_column 包含 5 和 3 的總和 (8),min_column 包含寫入其中的兩個值的最小值 (3),而 max_column 包含寫入其中的兩個值的最大值 (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
    
  3. 選用:在 Google Cloud 控制台中以 SQL 查詢資料表。

    1. 在 Google Cloud 控制台中,開啟「Bigtable instances」(Bigtable 執行個體) 頁面。

      前往執行個體清單

    2. 從清單中選取 counters-quickstart-instance

    3. 在導覽選單中,按一下「Bigtable Studio」

    4. 按一下「編輯器」分頁標籤。

    5. 將這個查詢貼到編輯器中:

      SELECT * FROM `counters_quickstart_table`
      
    6. 按一下「執行」。查詢結果會顯示在「Results」(結果) 資料表中,如下所示:

    _key max_family min_family sum_family
    資料列索引鍵 1 { "max_column": 5 } { "min_column": 5 } { "sum_column": 8 }

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。

  1. 在終端機中刪除資料表 counters_quickstart_table

    cbt deletetable counters_quickstart_table
    
  2. 刪除執行個體:

    cbt deleteinstance counters-quickstart-instance
    
  3. 刪除 .cbtrc 檔案:

    rm ~/.cbtrc
    
  4. 刪除格式設定檔案:

    rm ~/cbtformat.yaml
    
  5. 選用:從 gcloud CLI 撤銷憑證:

    gcloud auth revoke
    

後續步驟