创建实例并使用 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
  17. 创建 Bigtable 实例

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

      创建实例

    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. 添加一个名为 cf1 的列族:

      cbt createfamily my-table cf1
    4. 列出您的列族:

      cbt ls my-table

      该命令会显示类似如下所示的输出:

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

    5. test-value1test-value2 值写入到 r1 行中,且所属列族为 cf1,列限定符为 c1

      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

    后续步骤