Create and update counters in Bigtable
Learn how to create and update counters in Bigtable using aggregates,
table cells that aggregate values at write time. This quickstart uses the
Google Cloud CLI and the
cbt CLI
to create three counters:
- A counter that keeps a running sum
- A counter that keeps track of the minimum of all added values
- A counter that keeps track of the maximum of all added values
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
After initializing the gcloud CLI, update it and install the required components:
gcloud components update gcloud components install cbt
-
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 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.gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
After initializing the gcloud CLI, update it and install the required components:
gcloud components update gcloud components install cbt
-
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 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.gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com - Run the following commands to ensure that the gcloud CLI is up
to date and includes the
cbtCLI :gcloud components updategcloud components install cbt
Create a Bigtable instance
Use the
bigtable instances createcommand to create an instance.gcloud bigtable instances create counters-quickstart-instance \ --display-name="Counters quickstart instance" \ --cluster-config=id="counters-quickstart-cluster",zone="us-east1-c"
Connect to your instance
Configure the
cbtCLI to use your project and instance by creating a.cbtrcfile.echo project = PROJECT_ID >> ~/.cbtrc && echo instance = counters-quickstart-instance >> ~/.cbtrcReplace PROJECT_ID with the ID of the project that you are using.
Verify that you set up the
.cbtrcfile correctly.cat ~/.cbtrcThe terminal displays the contents of the
.cbtrcfile, which looks similar to the following:project = PROJECT_ID instance = counters-quickstart-instance
Now you can use the
cbtCLI with your instance.
Create a table with aggregate column families
Use the
cbt createtablecommand to create a table namedcounters_quickstart_tablethat has three aggregate column families. Configure each column family with a different aggregation type:- Column family
max_familyis typeMaxwith an input type ofInteger. - Column family
min_familyis typeMinwith an input type ofInteger. - Column family
sum_familyis typeSumwith an input type ofInteger.
cbt createtable counters_quickstart_table families=sum_family:never:intsum,min_family:never:intmin,max_family:never:intmax- Column family
List your column families by running the
cbt lscommand.cbt ls counters_quickstart_tableThe shell displays output similar to the following:
Family Name GC Policy ----------- --------- max_family <never> min_family <never> sum_family <never>
Create counters in the table
Use the
cbt addtocellcommand to write an initial value of5to a new column in each of the three column families, using a row key ofrow-key1and a timestamp of0. This operation creates aggregate cells, which you use as counters.cbt addtocell counters_quickstart_table row-key1 sum_family:sum_column=5@0 cbt addtocell counters_quickstart_table row-key1 min_family:min_column=5@0 cbt addtocell counters_quickstart_table row-key1 max_family:max_column=5@0
Read the data
To view the counter values as integers rather than bytes, define a
yamlfile thatcbtCLI can use to format the output. Run the following:echo "families:" > cbtformat.yaml echo " max_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yaml echo " min_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yaml echo " sum_family:" >> cbtformat.yaml echo " default_encoding: BigEndian" >> cbtformat.yaml echo " default_type: INT64" >> cbtformat.yamlVerify that you set up the
cbtformat.yamlfile correctly.cat ~/cbtformat.yamlThe terminal displays the contents of the
cbtformat.yamlfile, which looks similar to the following:families: max_family: default_encoding: BigEndian default_type: INT64 min_family: default_encoding: BigEndian default_type: INT64 sum_family: default_encoding: BigEndian default_type: INT64Use the
cbt readcommand to pass theyamlfile and read the data that you added to the table. The table now has three columns, each with a different aggregation type.cbt read counters_quickstart_table format-file=$HOME/cbtformat.yamlThe shell displays output similar to the following. The values are formatted as integers, and the timestamps are in UTC format.
row-key1 max_family:max_column @ 1970/01/01-00:00:00.000000 5 min_family:min_column @ 1970/01/01-00:00:00.000000 5 sum_family:sum_column @ 1970/01/01-00:00:00.000000 5
Update the counters
Add a value of 3 to each column in the table, using the same timestamps that you used when you created the cells. In each column, the cell value is merged with the existing value based on the cell's aggregation type.
cbt addtocell counters_quickstart_table row-key1 sum_family:sum_column=3@0 cbt addtocell counters_quickstart_table row-key1 min_family:min_column=3@0 cbt addtocell counters_quickstart_table row-key1 max_family:max_column=3@0Use the
cbt readcommand again to read the data in the table. Each cell now contains an aggregated value.cbt read counters_quickstart_table format-file=$HOME/cbtformat.yamlThe
sum_columncontains the sum of 5 and 3 (8),min_columncontains the minimum of the two values that were written to it (3), andmax_columncontains the maximum of the two values that were written to it (5).row-key1 max_family:max_column @ 1970/01/01-00:00:00.000000 5 min_family:min_column @ 1970/01/01-00:00:00.000000 3 sum_family:sum_column @ 1970/01/01-00:00:00.000000 8Optional: Query the table in the Google Cloud console with SQL.
In the Google Cloud console, open the Bigtable instances page.
Select
counters-quickstart-instancefrom the list.In the navigation menu, click Bigtable Studio.
Click the Editor tab.
Paste this query into the editor:
SELECT * FROM `counters_quickstart_table`Click Run. The results of the query are displayed in the Results table and look similar to the following:
_key max_family min_family sum_family row-key1 { "max_column": 5 } { "min_column": 5 } { "sum_column": 8 }
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.
In the terminal, delete the table
counters_quickstart_table:cbt deletetable counters_quickstart_tableDelete the instance:
cbt deleteinstance counters-quickstart-instanceDelete the
.cbtrcfile:rm ~/.cbtrcDelete the formatting file:
rm ~/cbtformat.yamlOptional: Revoke credentials from the gcloud CLI:
gcloud auth revoke