Create an instance and write data with the cbt CLI
If you're learning about Bigtable, you can work through a quickstart to get a taste of the fundamentals that you would use at scale in a production environment.
In this quickstart, you perform the following actions:
- Connect to a Bigtable instance.
- Perform basic administrative tasks.
- Write data to a table.
- Read data from a table.
Before you begin
- 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:
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.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="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.
- Replace
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:
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.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="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.
- Replace
- Run the following command to install the
cbt
CLI :gcloud components install cbt
Create a Bigtable instance
Open the Create instance page in the Google Cloud console.
For Instance name, enter
Quickstart instance
.For Instance ID, enter
quickstart-instance
.Click Continue.
For Storage type, select SSD.
Click Continue.
For Cluster ID, enter
quickstart-instance-c1
.For Region, select a region near you.
For Zone, select Any.
For Node scaling mode, choose Manual allocation.
For Quantity, select 1.
Click Create to create the instance.
Connect to your instance
Configure the
cbt
CLI to use your project and instance by creating a.cbtrc
file, replacingPROJECT_ID
with the ID for the project where you created your Bigtable instance:echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc
Verify that you set up the
.cbtrc
file correctly:cat ~/.cbtrc
The terminal displays the contents of the
.cbtrc
file, which looks similar to the following:project = PROJECT_ID instance = quickstart-instance
Now you can use the
cbt
CLI with your instance.
Read and write data
Bigtable stores data in tables, which contain rows. Each row is identified by a row key.
Data in a row is organized into column families, which are groups of columns. A column qualifier identifies a single column within a column family.
There can be multiple time-stamped cells at the intersection of a row and column.
Create a table named
my-table
.cbt createtable my-table
List your tables:
cbt ls
The command displays output similar to the following:
my-table
Add one column family named
cf1
:cbt createfamily my-table cf1
List your column families:
cbt ls my-table
The command displays output similar to the following:
Family Name GC Policy ----------- --------- cf1 <never>
Write the values
test-value1
andtest-value2
to the rowr1
, using the column familycf1
and the column qualifierc1
:cbt set my-table r1 cf1:c1=test-value1 cbt set my-table r1 cf1:c1=test-value2
Use the
cbt read
command to read the data you added to the table:cbt read my-table
The shell displays output similar to the following:
---------------------------------------- r1 cf1:c1 @ 2023/03/22-06:56:11.323000 "test-value1" cf1:c1 @ 2023/03/22-06:56:04.361000 "test-value2"
Two timestamped values are stored for the same column in row
r1
.
Clean up
To avoid incurring charges to your Google Cloud account for the resources
used in this quickstart, delete the instance. Deleting the .cbtrc
file leaves
you ready to work on a different project.
Delete the table
my-table
:cbt deletetable my-table
Delete the instance:
cbt deleteinstance quickstart-instance
Delete the
.cbtrc
file:rm ~/.cbtrc
Optional: Revoke credentials from the gcloud CLI:
gcloud auth revoke
What's next
- Work through a Bigtable codelab.
- View the
cbt
CLI reference documentation. - See sample source code for a fraud detection use case.
- Write a Hello World application in C#, C++, Go, Java, Node.js, PHP, Python, Ruby, or using the HBase client for Java.