This example is a simple "hello world" application, written in C++, that illustrates how to do the following:
- Connect to a Cloud Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Running the sample
This example uses the Cloud Bigtable package of the Google Cloud Client Library for C++ to communicate with Cloud Bigtable.
To run this sample program, follow the instructions on GitHub.
Using the Cloud Client Library with Cloud Bigtable
The sample application connects to Cloud Bigtable and demonstrates some simple operations.
Installing and importing the client library
Download or clone the Cloud Bigtable C++ client library from GitHub, then compile it. Follow the compiler instructions on the top-level README.
Include the required headers.
Connecting to Cloud Bigtable
Use TableAdmin.CreateDefaultAdminClient()
to create an
admin client that you will use to create a table.
Creating a table
Define a schema for the table that has one column family. Set a
garbage-collection rule for the column family to keep a maximum of one version
of each value. Use that schema to instantiate a table object using
TableAdmin.CreateTable()
. Then create a data
client that you can use to get data in and out of your table.
Writing rows to a table
Loop through a list of greeting strings to create some new rows for the table.
In each iteration, use SingleRowMutation()
to define
a row and assign it a row key and value. Then call Table.Apply()
to apply the mutation to the row.
Creating a filter
Before you read the data that you wrote, create a filter,
using Filter.ColumnRangeClosed()
,
to limit the data that Cloud Bigtable returns. This filter tells
Cloud Bigtable to return only the most recent version of each value,
even if the table contains older versions that haven't been garbage-collected.
Reading a row by its key
Call the Table.ReadRow()
function, passing in the row key and the filter,
to get one version of each value in that row.
Scanning all table rows
Use Table.ReadRows()
to read a range of rows
from the table.
Deleting a table
Delete the table with DeleteTable()
.
Putting it all together
Here is the full example without comments.