# Create a change stream-enabled table and capture changes

Bigtable change streams let you capture and process real-time data
modifications for event-driven workflows and downstream analytics. In this
quickstart, you enable change streams on a table, run a processing pipeline, and
observe the streamed data updates.

## Before you begin

1. 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 role (`roles/resourcemanager.projectCreator`), which contains the `resourcemanager.projects.create` permission. [Learn how to grant
     roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access).

   [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)
2.
   [Verify that billing is enabled for your Google Cloud project](https://docs.cloud.google.com/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).

3.


   Enable the Dataflow, Cloud Bigtable API, and Cloud Bigtable Admin API APIs, if any are not already enabled.


   **Roles required to enable APIs**


   To enable APIs, you need the `serviceusage.services.enable` permission. If you
   created the project, then you likely already have this permission through the
   Owner role (`roles/owner`). Otherwise, you can get this permission through the
   Service Usage Admin role (`roles/serviceusage.serviceUsageAdmin`).
   [Learn how to grant roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access).

   [Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=dataflow.googleapis.com,bigtable.googleapis.com,bigtableadmin.googleapis.com)
4. In the Google Cloud console, activate Cloud Shell.

   [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)

## Create a table with a change stream enabled

1. In the Google Cloud console, go to the Bigtable **Instances** page.

   [Go to Instances](https://console.cloud.google.com/bigtable/instances)
2. Click the ID of the instance that you are using for this quickstart.

   If you don't have an instance available, [create an instance](https://docs.cloud.google.com/bigtable/docs/creating-instance) with the default
   configurations in a region near you.
3. In the left navigation pane, click **Tables**.

4. Click **Create a table**.

5. Name the table `change-streams-quickstart`.

6. Add a column family named `cf`.

7. Select **Enable change stream**.

8. Click **Create**.

## Initialize a data pipeline to capture the change stream

1. In the Cloud Shell, run the following commands to download the code and run it.

       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"

   Replace the following:
   - <var translate="no">PROJECT_ID</var>: the ID of the project that you are using
   - <var translate="no">BIGTABLE_INSTANCE_ID</var>: the ID of the instance to contain the new table
   - <var translate="no">BIGTABLE_REGION</var>: the region that your Bigtable instance is in, such as `us-east5`
2. In the Google Cloud console, go to the **Dataflow** page.

   [Go to Dataflow](https://console.cloud.google.com/dataflow)
3. Click the job with a name that begins with **changestreamquickstart**.

4. At the bottom of the screen, click **Show** to open the logs panel.

5. Click **Worker logs** to monitor the output of the change stream.

6. In the Cloud Shell, write some data to Bigtable to see the change stream process.

       cbt -instance=BIGTABLE_INSTANCE_ID -project=PROJECT_ID \
       import change-streams-quickstart quickstart-data.csv column-family=cf

7. In the Google Cloud console, make sure that **Severity** is set to at least `Info`.

8. The worker log output logs contain this:

       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

   > [!NOTE]
   > **Note:** The log might take a few minutes to appear while the job initializes.

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, follow these steps.

1. Disable the change stream on the table

       gcloud bigtable instances tables update change-streams-quickstart --instance=BIGTABLE_INSTANCE_ID \
       --clear-change-stream-retention-period

2. Delete the table `change-streams-quickstart`:

       cbt -instance=BIGTABLE_INSTANCE_ID -project=PROJECT_ID deletetable change-streams-quickstart

3. Stop the change stream pipeline:

   1. In the Google Cloud console, go to the Dataflow **Jobs** page.

      [Go to Jobs](https://console.cloud.google.com/dataflow/jobs)
   2. Select your streaming job from the job list.

   3. In the navigation, click **Stop**.

   4. In the **Stop job** dialog, cancel your pipeline, and then click **Stop job.**

4. Optional: Delete the instance if you created a new one for this quickstart:

       cbt deleteinstance BIGTABLE_INSTANCE_ID

## What's next

- [Read through the change streams documentation](https://docs.cloud.google.com/bigtable/docs/change-streams-overview).
- [Try a more complex change stream example](https://docs.cloud.google.com/bigtable/docs/change-streams-tutorial).