创建实例并使用 cbt CLI 写入数据
如果您想要了解 Bigtable,可以通过逐步完成快速入门中的操作来了解在生产环境中广泛使用的基础知识。
在本快速入门中,您将执行以下操作:
- 连接到 Bigtable 实例。
- 执行基本管理任务。
- 将数据写入表中。
- 从表中读取数据。
准备工作
- 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.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith 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_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Bigtable and Cloud Bigtable Admin APIs: Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com 
- 
    
        Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admingcloud 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.
 
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith 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_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Bigtable and Cloud Bigtable Admin APIs: Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable bigtable.googleapis.com bigtableadmin.googleapis.com 
- 
    
        Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/bigtable.admingcloud 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.
 
- 运行以下命令以安装 cbtCLI:gcloud components install cbt
- 在 Google Cloud 控制台中打开创建实例页面。 
- 在实例名称部分,输入 - Quickstart instance。
- 在实例 ID 部分,输入 - quickstart-instance。
- 点击继续。 
- 在存储类型部分,选择 SSD。 
- 点击继续。 
- 在集群 ID 部分,输入 - quickstart-instance-c1。
- 在区域部分,选择您附近的一个区域。 
- 在可用区部分,选择任何。 
- 在节点扩缩模式部分,选择手动分配。 
- 在数量部分,选择 1。 
- 点击创建以创建实例。 
- 将 - cbtCLI 配置为使用您的项目和实例,方法是创建一个- .cbtrc文件,并将- PROJECT_ID替换为在其中创建了 Bigtable 实例的项目的 ID:- echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc 
- 验证 - .cbtrc文件的设置是否正确:- cat ~/.cbtrc - 终端会显示 - .cbtrc文件的内容,如下所示:- project = PROJECT_ID instance = quickstart-instance - 现在,您可以使用 - cbtCLI 来处理您的实例了。
- 创建一个名为 - my-table的表。- cbt createtable my-table 
- 列出您的表: - cbt ls - 该命令会显示类似如下所示的输出: - my-table
- 添加一个名为 - cf1的列族:- cbt createfamily my-table cf1 
- 列出您的列族: - cbt ls my-table - 该命令会显示类似如下所示的输出: - Family Name GC Policy ----------- --------- cf1 <never>
- 将 - test-value1和- test-value2值写入到- r1行中,且所属列族为- cf1,列限定符为- c1:- cbt set my-table r1 cf1:c1=test-value1 cbt set my-table r1 cf1:c1=test-value2 
- 使用 - 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行中的同一列存储两个带有时间戳的值。
- 删除 - my-table表:- cbt deletetable my-table 
- 删除实例: - cbt deleteinstance quickstart-instance 
- 删除 - .cbtrc文件:- rm ~/.cbtrc 
- (可选)使用 gcloud CLI 撤消凭据: - gcloud auth revoke 
- 浏览 Bigtable Codelab。
- 查看 cbtCLI 参考文档。
- 请参阅欺诈侦查用例的示例源代码。
- 使用 C#、C++、Go、Java、Node.js、PHP、Python、Ruby 编写 Hello World 应用,或使用Java 版 HBase 客户端。
创建 Bigtable 实例
连接到您的实例
读取和写入数据
Bigtable 将数据存储在表中。这些表包含行,且每行由一个行键标识。
行中的数据以“列族”(即一组列)的形式整理。 “列限定符”用于标识列族中的单个列。
行和列的交集处可能会有多个包含时间戳的单元。
清理
为避免系统因本快速入门中使用的资源向您的 Google Cloud 账号收取费用,请删除您的实例。删除 .cbtrc 文件后,您就可以开始处理其他项目了。