Node.js hello world
This code sample is a "hello world" application that runs on Node.js. The sampleillustrates how to complete the following tasks:
- 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 Node.js 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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Running the sample
This code sample uses theBigtable package of theGoogle Cloud Client Library for Node.js to communicate withBigtable.
To run this sample program, follow theinstructions for the sample onGitHub.
Using the Cloud Client Library with Bigtable
The sample application connects to Bigtable and demonstrates somesimple operations.
Requiring the client library
The sample requires the@google-cloud/bigtable module, which provides theBigtable class.
const{Bigtable}=require('@google-cloud/bigtable');Connecting to Bigtable
To connect to Bigtable, create a newBigtable object. Then call itsinstance() method to get anInstance object that represents yourBigtable instance.
constbigtableClient=newBigtable();constinstance=bigtableClient.instance(INSTANCE_ID);consttable=instance.table(TABLE_ID);Creating a table
Call the instance'stable() method to get aTable object that represents the table for "hello world"greetings. If the table doesn't exist, call the table'screate() method to create a table with a singlecolumn family that retains one version of each value.
const{BigtableTableAdminClient}=require('@google-cloud/bigtable').v2;constadminClient=newBigtableTableAdminClient();constprojectId=awaitadminClient.getProjectId();lettableExists=true;try{awaitadminClient.getTable({name:`projects/${projectId}/instances/${INSTANCE_ID}/tables/${TABLE_ID}`,});}catch(e){if(e.code===5){tableExists=false;}}if(!tableExists){console.log(`Creating table${TABLE_ID}`);const{BigtableTableAdminClient}=require('@google-cloud/bigtable').v2;constadminClient=newBigtableTableAdminClient();constprojectId=awaitadminClient.getProjectId();awaitadminClient.createTable({parent:`projects/${projectId}/instances/${INSTANCE_ID}`,tableId:TABLE_ID,table:{columnFamilies:{[COLUMN_FAMILY_ID]:{gcRule:{maxNumVersions:1,},},},},});}Writing rows to a table
Use an array of greeting strings to create some new rows for the table: call thearray'smap() method to create a new array of objects that represent rows,then call the table'sinsert() method to add therows to the table.
console.log('Write some greetings to the table');constgreetings=['Hello World!','Hello Bigtable!','Hello Node!'];constrowsToInsert=greetings.map((greeting,index)=>({// Note: This example uses sequential numeric IDs for simplicity, but this// pattern can result in poor performance in a production application.// Rows are stored in sorted order by key, so sequential keys can result// in poor distribution of operations across nodes.//// For more information about how to design an effective schema for Cloud// Bigtable, see the documentation:// https://cloud.google.com/bigtable/docs/schema-designkey:`greeting${index}`,data:{[COLUMN_FAMILY_ID]:{[COLUMN_QUALIFIER]:{// Setting the timestamp allows the client to perform retries. If// server-side time is used, retries may cause multiple cells to// be generated.timestamp:newDate(),value:greeting,},},},}));awaittable.insert(rowsToInsert);Creating a filter
Before you read the data that you wrote, create a filter to limit the data thatBigtable returns. This filter tells Bigtable toreturn only the most recent cell for each column, even if the column containsolder cells.
constfilter=[{column:{cellLimit:1,// Only retrieve the most recent version of the cell.},},];Reading a row by its row key
Call the table'srow() method to get a reference tothe row with a specific row key. Then call the row'sget() method, passing in the filter, to get one versionof each value in that row.
console.log('Reading a single row by row key');const[singleRow]=awaittable.row('greeting0').get({filter});console.log(`\tRead:${getRowGreeting(singleRow)}`);Scanning all table rows
Call the table'sgetRows() method, passing in thefilter, to get all of the rows in the table. Because you passed in the filter,Bigtable returns only one version of each value.
console.log('Reading the entire table');// Note: For improved performance in production applications, call// `Table#readStream` to get a stream of rows. See the API documentation:// https://cloud.google.com/nodejs/docs/reference/bigtable/latest/Table#createReadStreamconst[allRows]=awaittable.getRows({filter});for(constrowofallRows){console.log(`\tRead:${getRowGreeting(row)}`);}Deleting a table
Delete the table with the table'sdelete() method.
console.log('Delete the table');constrequest={name:`projects/${projectId}/instances/${INSTANCE_ID}/tables/${TABLE_ID}`,};awaitadminClient.deleteTable(request);Putting it all together
Here is the full code sample without comments.
const{Bigtable}=require('@google-cloud/bigtable');constTABLE_ID='Hello-Bigtable';constCOLUMN_FAMILY_ID='cf1';constCOLUMN_QUALIFIER='greeting';constINSTANCE_ID=process.env.INSTANCE_ID;if(!INSTANCE_ID){thrownewError('Environment variables for INSTANCE_ID must be set!');}constgetRowGreeting=row=>{returnrow.data[COLUMN_FAMILY_ID][COLUMN_QUALIFIER][0].value;};(async()=>{try{constbigtableClient=newBigtable();constinstance=bigtableClient.instance(INSTANCE_ID);consttable=instance.table(TABLE_ID);const{BigtableTableAdminClient}=require('@google-cloud/bigtable').v2;constadminClient=newBigtableTableAdminClient();constprojectId=awaitadminClient.getProjectId();lettableExists=true;try{awaitadminClient.getTable({name:`projects/${projectId}/instances/${INSTANCE_ID}/tables/${TABLE_ID}`,});}catch(e){if(e.code===5){tableExists=false;}}if(!tableExists){console.log(`Creating table${TABLE_ID}`);const{BigtableTableAdminClient}=require('@google-cloud/bigtable').v2;constadminClient=newBigtableTableAdminClient();constprojectId=awaitadminClient.getProjectId();awaitadminClient.createTable({parent:`projects/${projectId}/instances/${INSTANCE_ID}`,tableId:TABLE_ID,table:{columnFamilies:{[COLUMN_FAMILY_ID]:{gcRule:{maxNumVersions:1,},},},},});}console.log('Write some greetings to the table');constgreetings=['Hello World!','Hello Bigtable!','Hello Node!'];constrowsToInsert=greetings.map((greeting,index)=>({key:`greeting${index}`,data:{[COLUMN_FAMILY_ID]:{[COLUMN_QUALIFIER]:{timestamp:newDate(),value:greeting,},},},}));awaittable.insert(rowsToInsert);constfilter=[{column:{cellLimit:1,// Only retrieve the most recent version of the cell.},},];console.log('Reading a single row by row key');const[singleRow]=awaittable.row('greeting0').get({filter});console.log(`\tRead:${getRowGreeting(singleRow)}`);console.log('Reading the entire table');const[allRows]=awaittable.getRows({filter});for(constrowofallRows){console.log(`\tRead:${getRowGreeting(row)}`);}console.log('Delete the table');constrequest={name:`projects/${projectId}/instances/${INSTANCE_ID}/tables/${TABLE_ID}`,};awaitadminClient.deleteTable(request);}catch(error){console.error('Something went wrong:',error);}})();Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.