This code sample is a "hello world" application that runs on Node.js. The sample illustrates how to complete the following tasks:
- 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 code sample uses the Cloud Bigtable package of the Google Cloud Client Library for Node.js to communicate with Cloud Bigtable.
To run this sample program, follow the instructions for the sample on GitHub.
Using the Cloud Client Library with Cloud Bigtable
The sample application connects to Cloud Bigtable and demonstrates some simple operations.
Requiring the client library
The sample requires the @google-cloud/bigtable
module, which provides the
Bigtable
class.
Connecting to Cloud Bigtable
To connect to Cloud Bigtable, create a new
Bigtable
object. Then call its
instance()
method to get an
Instance
object that represents your
Cloud Bigtable instance.
Creating a table
Call the instance's table()
method to get a
Table
object that represents the table for "hello world"
greetings. If the table doesn't exist, call the table's
create()
method to create a table with a single
column family that retains one version of each value.
Writing rows to a table
Use an array of greeting strings to create some new rows for the table: call the
array's map()
method to create a new array of objects that represent rows,
then call the table's insert()
method to add the
rows to the table.
Creating a filter
Before you read the data that you wrote, create a filter 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 row key
Call the table's row()
method to get a reference to
the row with a specific row key. Then call the row's
get()
method, passing in the filter, to get one version
of each value in that row.
Scanning all table rows
Call the table's getRows()
method, passing in the
filter, to get all of the rows in the table. Because you passed in the filter,
Cloud Bigtable returns only one version of each value.
Deleting a table
Delete the table with the table's delete()
method.
Putting it all together
Here is the full code sample without comments.