在 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. 如果您使用的是外部身份提供方 (IdP),则必须先 使用联合身份登录 gcloud CLI

  4. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  5. 初始化 gcloud CLI 后,对其进行更新并安装所需组件:

    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. Verify 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. 如果您使用的是外部身份提供方 (IdP),则必须先 使用联合身份登录 gcloud CLI

  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. 初始化 gcloud CLI 后,对其进行更新并安装所需组件:

    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. Verify 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
  17. 创建 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. cbt CLI 配置为使用您的项目和实例,方法是创建一个 .cbtrc 文件。

      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_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
      
    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
      

      该 shell 会显示类似如下所示的输出。这些值以整数格式表示,时间戳采用 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
      

    更新计数器

    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 实例页面。

        前往实例列表

      2. 从列表中选择 counters-quickstart-instance

      3. 在导航菜单中,点击 Bigtable Studio

      4. 点击编辑器标签页。

      5. 将以下查询粘贴到编辑器中:

        SELECT * FROM `counters_quickstart_table`
        
      6. 点击运行。查询结果会显示在结果表中,类似于以下内容:

      _key max_family min_family sum_family
      row-key1 { "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
      

    后续步骤