C++ hello world
This example is a simple "hello world" application, written in C++, that illustrates how to do the following:
- Set up authentication
- Connect to a Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Set up authentication
To use the C++ samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Running the sample
This example uses the Cloud Bigtable package of the Google Cloud client library for C++ to communicate with Bigtable.
To run this sample program, follow the instructions on GitHub.
Using the Google Cloud client library with Bigtable
The sample application connects to Bigtable and demonstrates some simple operations.
Installing and importing the client library
Download or clone the Bigtable C++ client library from GitHub, then compile it. Follow the compiler instructions on the top-level README.
Include the required headers.
Connecting to Bigtable
Use MakeBigtableTableAdminConnection()
to
construct a BigtableTableAdminClient
, which
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
BigtableTableAdminClient::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 Bigtable returns. This filter tells
Bigtable to return only the most recent version of each value,
even if the table contains older cells that have expired but have not yet been
removed by garbage collection.
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 BigtableTableAdminClient::DeleteTable()
.
Putting it all together
Here is the full example without comments.