创建一个测试表

本页面介绍了创建小型 Bigtable 表的步骤, 测试代码段的方法。该表包含智能手机和平板电脑的时间序列数据。

以下说明使用 Google Cloud 控制台和 cbt CLI(一种专为 Bigtable 构建的命令行界面)。

准备工作

在创建测试表之前,请先完成以下前提条件。

设置身份验证

In the Google Cloud console, activate Cloud Shell.

Activate Cloud Shell

At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

如需了解如何为生产环境设置身份验证,请参阅 Set up Application Default Credentials for code running on Google Cloud

授予 IAM 角色

如需获得创建测试表所需的权限,请让您的管理员向您授予项目的 Bigtable Administrator (roles/bigtable.admin) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

安装 cbt CLI

运行以下命令以安装 cbt CLI :

  gcloud components install cbt

创建实例

在您选择的项目中,创建一个测试实例来存储您的测试表。该表非常小,因此您只需使用单节点实例。

  1. 在 Google Cloud 控制台中打开创建实例页面。

    创建实例

  2. 实例名称部分,输入 Test instance

  3. 实例 ID 部分,输入 test-instance

  4. 存储类型部分,选择 SSD

  5. 集群 ID 部分,输入 test-instance-c1

  6. 区域部分,选择您附近的一个区域。

  7. 地区部分,选择所选区域内的一个地区。

  8. 节点下,输入 1

  9. 点击创建以创建实例。

连接到实例

  1. 配置 cbt CLI 使用项目和实例 .cbtrc 文件,将 PROJECT_ID 替换为 ID (您在其中创建了 Bigtable 实例的项目):

    echo project = PROJECT_ID >> ~/.cbtrc && echo instance = test-instance >> ~/.cbtrc
    
  2. 验证 .cbtrc 文件的设置是否正确:

    cat ~/.cbtrc

    终端会显示 .cbtrc 文件的内容,如下所示:

    project = PROJECT_ID
    instance = test-instance

    现在,您可以使用 cbt CLI 实例

创建一个测试表

  1. 创建一个名为 test_table 且包含两个列族的表。针对每个列族,设置垃圾回收政策,以使每个列最多附加 2 个单元:

       cbt createtable test_table "families=stats_summary:maxversions=2,stats_detail:maxversions=2,cell_plan:maxversions=2"

  2. 列出列族:

       cbt ls test_table

    该终端会显示类似如下所示的输出:

        Family Name       GC Policy
        -----------       ---------
        stats_detail        versions() > 2
        stats_summary       versions() > 2
        cell_plan           versions() > 2

填充表

  1. 将以下命令复制到终端窗口中,然后按 Enter

      cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=true
      cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=true
      cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=0 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190406.000
      cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=true
      cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=true
  2. 使用 cbt read 命令读取您已添加到表中的数据:

       cbt read test_table

    该终端会显示类似如下所示的输出。请注意,因为您的写入请求不包含时间戳,所以系统会自动将时间戳分配给单元格。

        ----------------------------------------
        phone#4c410523#20190501
          cell_plan:data_plan_01gb                 @ 2023/09/23-11:44:10.535000
            "true"
          cell_plan:data_plan_05gb                 @ 2023/09/23-11:44:10.535000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:10.535000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:10.535000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:10.535000
            "PQ2A.190405.003"
        ----------------------------------------
        phone#4c410523#20190502
          cell_plan:data_plan_05gb                 @ 2023/09/23-11:44:11.545000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:11.545000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:11.545000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:11.545000
            "PQ2A.190405.004"
        ----------------------------------------
        phone#4c410523#20190505\
          stats_summary:connected_cell             @ 2023/09/23-11:44:12.503000
            "0"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:12.503000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:12.503000
            "PQ2A.190406.000"
        ----------------------------------------
        phone#5c10102#20190501
          cell_plan:data_plan_10gb                 @ 2023/09/23-11:44:13.553000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:13.553000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:13.553000
            "1"
          stats_summary:os_build                   @ 2023/09/23-11:44:13.553000
            "PQ2A.190401.002"
        ----------------------------------------
        tablet#5c10102#20190502
          cell_plan:data_plan_10gb                 @ 2023/09/23-11:44:14.480000
            "true"
          stats_summary:connected_cell             @ 2023/09/23-11:44:14.480000
            "1"
          stats_summary:connected_wifi             @ 2023/09/23-11:44:14.480000
            "0"
          stats_summary:os_build                   @ 2023/09/23-11:44:14.480000
                 "PQ2A.190406.000"
    
  3. 可选:向相同的行和列写入新值。 Bigtable 会创建带有新时间戳的新单元格来存储数据的新版本。将以下命令复制到您的终端 窗口,然后按 Enter

      cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=false
      cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=false
      cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=1 stats_summary:connected_wifi=4 stats_summary:os_build=PQ2A.190406.000
      cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=3 stats_summary:connected_wifi=3 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=false
      cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=false
  4. 使用 cbt read 命令读取表中的所有数据:

       cbt read test_table

    该终端会显示类似如下所示的输出。现在,每个列都包含两个具有唯一时间戳的单元。

      ----------------------------------------
      phone#4c410523#20190501
        cell_plan:data_plan_01gb                 @ 2024/07/09-17:48:21.191000
        "true"
        cell_plan:data_plan_01gb                 @ 2024/07/09-17:46:09.369000
        "true"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:48:21.191000
        "false"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:46:09.369000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:21.191000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:09.369000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:21.191000
        "5"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:09.369000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:21.191000
        "PQ2A.190405.003"
        stats_summary:os_build                   @ 2024/07/09-17:46:09.369000
        "PQ2A.190405.003"
    
      ----------------------------------------
      phone#4c410523#20190502
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:48:22.205000
        "false"
        cell_plan:data_plan_05gb                 @ 2024/07/09-17:46:10.455000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:22.205000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:10.455000
        "1"
         stats_summary:connected_wifi             @ 2024/07/09-17:48:22.205000
        "5"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:10.455000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:22.205000
        "PQ2A.190405.004"
        stats_summary:os_build                   @ 2024/07/09-17:46:10.455000
        "PQ2A.190405.004"
    
      ----------------------------------------
      phone#4c410523#20190505
        stats_summary:connected_cell             @ 2024/07/09-17:48:23.206000
        "1"
        stats_summary:connected_cell             @ 2024/07/09-17:46:11.402000
        "0"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:23.206000
        "4"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:11.402000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:23.206000
        "PQ2A.190406.000"
        stats_summary:os_build                   @ 2024/07/09-17:46:11.402000
        "PQ2A.190406.000"
    
      ----------------------------------------
      phone#5c10102#20190501
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:48:24.172000
        "false"
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:46:12.388000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:24.172000
        "3"
        stats_summary:connected_cell             @ 2024/07/09-17:46:12.388000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:24.172000
        "3"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:12.388000
        "1"
        stats_summary:os_build                   @ 2024/07/09-17:48:24.172000
        "PQ2A.190401.002"
        stats_summary:os_build                   @ 2024/07/09-17:46:12.388000
        "PQ2A.190401.002"
    
      ----------------------------------------
      tablet#5c10102#20190502
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:48:25.194000
        "false"
        cell_plan:data_plan_10gb                 @ 2024/07/09-17:46:13.391000
        "true"
        stats_summary:connected_cell             @ 2024/07/09-17:48:25.194000
        "2"
        stats_summary:connected_cell             @ 2024/07/09-17:46:13.391000
        "1"
        stats_summary:connected_wifi             @ 2024/07/09-17:48:25.194000
        "0"
        stats_summary:connected_wifi             @ 2024/07/09-17:46:13.391000
        "0"
        stats_summary:os_build                   @ 2024/07/09-17:48:25.194000
        "PQ2A.190406.000"
        stats_summary:os_build                   @ 2024/07/09-17:46:13.391000
        "PQ2A.190406.000"
    

使用测试数据

您现在可以在 test-instance 上使用 test_table 来测试代码示例或查询。

清理

为避免系统因这些步骤中创建的资源向您的 Google Cloud 账号收取费用,请在完成测试后立即删除实例。删除 .cbtrc 文件后,您就可以开始处理另一个项目了。

  1. 如果您要保留实例但删除表,可以使用 deletetable 命令:

       cbt deletetable test_table

  2. 如果您不打算使用该实例进行进一步测试,请删除该实例。此操作也会删除该表格。

       cbt deleteinstance test-instance

  3. 删除 .cbtrc 文件:

       rm ~/.cbtrc

后续步骤