创建启用了变更数据流的表并捕获更改
了解如何设置启用了变更数据流的 Bigtable 表、如何运行变更数据流流水线、如何对表进行更改,以及如何查看已流式传输的更改。
准备工作
- 
    
    
      
        In the Google Cloud console, on the project selector page, select or create 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.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Dataflow, Cloud Bigtable API, and Cloud Bigtable Admin API 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.
- 
  
    In the Google Cloud console, activate Cloud Shell. 创建启用了变更数据流的表- 在 Google Cloud 控制台中,前往 Bigtable 实例页面。 
- 点击您在本快速入门中使用的实例的 ID。 - 如果您没有可用实例,请在您附近的区域中使用默认配置创建实例。 
- 在左侧导航窗格中,点击表。 
- 点击创建表。 
- 将表格命名为 - change-streams-quickstart。
- 添加一个名为 - cf的列族。
- 选择启用变更数据流。 
- 点击创建。 
 初始化数据流水线以捕获变更数据流- 在 Cloud Shell 中,运行以下命令以下载并运行代码。 - git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/bigtable/beam/change-streams mvn compile exec:java -Dexec.mainClass=ChangeStreamsHelloWorld \ "-Dexec.args=--project=PROJECT_ID --bigtableProjectId=PROJECT_ID \ --bigtableInstanceId=BIGTABLE_INSTANCE_ID --bigtableTableId=change-streams-quickstart \ --runner=dataflow --region=BIGTABLE_REGION --experiments=use_runner_v2"- 请替换以下内容: - PROJECT_ID:您正在使用的项目的 ID
- BIGTABLE_INSTANCE_ID:将包含新表的实例的 ID
- BIGTABLE_REGION:Bigtable 实例所在的区域,例如 us-east5
 
- 在 Google Cloud 控制台中,前往 Dataflow 页面。 
- 点击名称以 changestreamquickstart 开头的作业。 
- 点击屏幕底部的显示以打开日志面板。 
- 点击工作器日志以监控变更数据流的输出。 
- 在 Cloud Shell 中,向 Bigtable 写入一些数据,以查看变更数据流处理过程。 - cbt -instance=BIGTABLE_INSTANCE_ID -project=PROJECT_ID \ import change-streams-quickstart quickstart-data.csv column-family=cf
- 在 Google Cloud 控制台中,确保将严重程度至少设置为 - Info。
- 工作器日志输出日志包含以下内容: - Change captured: user123#2023,USER,SetCell,cf,col1,abc Change captured: user546#2023,USER,SetCell,cf,col1,def Change captured: user789#2023,USER,SetCell,cf,col1,ghi
 清理为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。 - 在表上停用变更数据流 - gcloud bigtable instances tables update change-streams-quickstart --instance=BIGTABLE_INSTANCE_ID \ --clear-change-stream-retention-period
- 删除 - change-streams-quickstart表:- cbt -instance=BIGTABLE_INSTANCE_ID -project=PROJECT_ID deletetable change-streams-quickstart
- 停止变更数据流流水线: - 在 Google Cloud 控制台中,前往 Dataflow 作业页面。 
- 从作业列表中选择您的流处理作业。 
- 在导航中,点击停止。 
- 在停止作业对话框中,取消流水线,然后点击停止作业。 
 
- 可选:删除实例(如果您为此快速入门创建了新实例): - cbt deleteinstance BIGTABLE_INSTANCE_ID
 后续步骤