Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit cf04e10

Browse files
danieljbrucegoogle-labs-jules[bot]gcf-owl-bot[bot]
authored
docs: Update all the samples targeting the BigtableTableAdminClient to use the generated layer instead (#1710)
* Export the GCRuleMaker class * Use the admin client for the first sample * feat: updated samples to use GCRuleMaker * feat: updated samples to use GCRuleMaker * feat: Update restoreTable sample to use generated client This commit updates the `backups.restore.js` sample to use the `BigtableTableAdminClient` directly for restoring a table from a backup, instead of going through the handwritten `bigtable.instance().createTableFromBackup()` method. This change is based on the design document for the Node.js Bigtable Admin API autogeneration. The corresponding test for the sample in `samples/test/backups.js` has also been updated to pass the correct arguments to the updated sample script. * table id to table name * The rule should be a union - not nested instance of the admin client should be used * Use instance of admin client - not admin client itself * Update the rules so they won’t throw errors * change family.id to family.name * max age has to be at least 1 millisecond * Run linter * Refresh project id token * Get rid of projectId token * revert change to backups method called * Use the admin client for createTable instead * snippets should include admin client * This code snippet should include project fetch * Comment out options * Use the admin client to create a table * Use the generated layer for maxVersions * Don’t use instance.name in parent path * Update the family snippets * Update createTable in the Row samples * Eliminate the need for any default projectId fetch * Don’t hardcode the project id * don’t hardcode project id * commit examples * Don’t create table twice * options is not used. remove it * table admin * Hardcode instance name * delete the examples folder * remove options * add header * The rest of the samples should use BigtableTableAdminClient instread * Update more tables code * commit the api-reference-doc-snippets * correct the backups samples * Revert "correct the backups samples" This reverts commit 7241dbe. * Modify the reads samples * Update the functions script * Make separate column family creation calls This makes the test pass * Fix the deletes sample changes * change the fixture don’t wrap the return value in brackets * return value should be false literal * Correct the fixture for the deletes samples * Reintroduce the backup samples * Replace write.js code with calls to generated layr * removed unused table * backups from instance as well * Fix the index.js sample * Fixed a bunch of syntax highlighting errors * run the linter * Changes to pass the linting check * Eliminate unused cluster * Add comment for restore table LRO * Add a script for the consistency token * Update the restore table snippet * Setup the polling harness for optimized restore tb * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * update snippets - optimization * Correct header * Add code that makes the optimized restore table call --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 326961f commit cf04e10

20 files changed

Lines changed: 693 additions & 253 deletions

samples/__snapshots__/deletes.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,4 @@ exports['deletes should delete a column family 1'] = `
177177
178178
`;
179179

180-
exports['deletes should delete a table 1'] = `
181-
[ false ]
182-
183-
`;
180+
exports['deletes should delete a table 1'] = 'false\n';

samples/api-reference-doc-snippets/backups.create.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ async function main(
1919
backupId = 'YOUR_BACKUP_ID',
2020
) {
2121
// [START bigtable_api_create_backup]
22-
const {Bigtable} = require('@google-cloud/bigtable');
23-
const bigtable = new Bigtable();
22+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
23+
const tableAdminClient = new BigtableTableAdminClient();
2424

2525
async function createBackup() {
2626
/**
@@ -30,19 +30,23 @@ async function main(
3030
// const tableId = 'YOUR_TABLE_ID';
3131
// const clusterId = 'YOUR_CLUSTER_ID';
3232
// const backupId = 'YOUR_BACKUP_ID';
33+
const projectId = await tableAdminClient.getProjectId();
3334

34-
const instance = bigtable.instance(instanceId);
35-
const cluster = instance.cluster(clusterId);
35+
const request = {
36+
parent: tableAdminClient.clusterPath(projectId, instanceId, clusterId),
37+
backupId: backupId,
38+
backup: {
39+
sourceTable: tableAdminClient.tablePath(projectId, instanceId, tableId),
40+
expireTime: new Date(Date.now() + 7 * 60 * 60 * 1000), // 7 hours from now
41+
},
42+
};
3643

37-
const [backup, operation] = await cluster.createBackup(backupId, {
38-
table: tableId,
39-
expireTime: new Date(Date.now() + 7 * 60 * 60 * 1000), // 7 hours from now
40-
});
44+
const [operation] = await tableAdminClient.createBackup(request);
4145

4246
console.log('Started a table backup operation.');
43-
await operation.promise();
47+
const [backup] = await operation.promise();
4448

45-
console.log(`Backup "${backup.id}" is now ready for use.`);
49+
console.log(`Backup "${backup.name}" is now ready for use.`);
4650
}
4751

4852
await createBackup();

samples/api-reference-doc-snippets/backups.delete.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@
1414

1515
async function main(
1616
instanceId = 'YOUR_INSTANCE_ID',
17-
tableId = 'YOUR_TABLE_ID',
1817
clusterId = 'YOUR_CLUSTER_ID',
1918
backupId = 'YOUR_BACKUP_ID',
2019
) {
2120
// [START bigtable_api_delete_backup]
22-
const {Bigtable} = require('@google-cloud/bigtable');
23-
const bigtable = new Bigtable();
21+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
22+
const tableAdminClient = new BigtableTableAdminClient();
2423

2524
async function deleteBackup() {
2625
/**
2726
* TODO(developer): Uncomment these variables before running the sample.
2827
*/
2928
// const instanceId = 'YOUR_INSTANCE_ID';
30-
// const tableId = 'YOUR_TABLE_ID';
3129
// const clusterId = 'YOUR_CLUSTER_ID';
3230
// const backupId = 'YOUR_BACKUP_ID';
33-
const instance = bigtable.instance(instanceId);
34-
const table = instance.table(tableId);
35-
const cluster = table.cluster(clusterId);
36-
const backup = cluster.backup(backupId);
37-
38-
await backup.delete();
31+
const projectId = await tableAdminClient.getProjectId();
32+
const request = {
33+
name: tableAdminClient.backupPath(
34+
projectId,
35+
instanceId,
36+
clusterId,
37+
backupId,
38+
),
39+
};
40+
await tableAdminClient.deleteBackup(request);
3941
console.log(`Backup ${backupId} was deleted successfully.`);
4042
}
4143

samples/api-reference-doc-snippets/backups.get.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,40 @@
1414

1515
async function main(
1616
instanceId = 'YOUR_INSTANCE_ID',
17-
tableId = 'YOUR_TABLE_ID',
1817
clusterId = 'YOUR_CLUSTER_ID',
1918
backupId = 'YOUR_BACKUP_ID',
2019
) {
2120
// [START bigtable_api_get_backup]
22-
const {Bigtable} = require('@google-cloud/bigtable');
23-
const bigtable = new Bigtable();
21+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
22+
const tableAdminClient = new BigtableTableAdminClient();
2423

2524
async function getBackup() {
2625
/**
2726
* TODO(developer): Uncomment these variables before running the sample.
2827
*/
2928
// const instanceId = 'YOUR_INSTANCE_ID';
30-
// const tableId = 'YOUR_TABLE_ID';
3129
// const clusterId = 'YOUR_CLUSTER_ID';
3230
// const backupId = 'YOUR_BACKUP_ID';
33-
const instance = bigtable.instance(instanceId);
34-
const table = instance.table(tableId);
35-
const cluster = table.cluster(clusterId);
31+
const projectId = await tableAdminClient.getProjectId();
3632

37-
// Create a reference to the backup before performing operations on it.
38-
const backup = cluster.backup(backupId);
33+
const request = {
34+
name: tableAdminClient.backupPath(
35+
projectId,
36+
instanceId,
37+
clusterId,
38+
backupId,
39+
),
40+
};
3941

40-
// Get the backup's metadata, with information such as when it will expire
41-
// and how big it is.
42-
const [metadata] = await backup.getMetadata();
42+
const [metadata] = await tableAdminClient.getBackup(request);
4343
console.log(`The backup is ${metadata.sizeBytes} bytes.`);
4444

4545
// Time properties have Date helpers to convert to a `PreciseDate`.
4646
console.log(
47-
`The backup will auto-delete at ${metadata.expireDate.toISOString()}`,
47+
`The backup will auto-delete at ${new Date(metadata.expireTime.seconds * 1000).toISOString()}`,
4848
);
4949
console.log(
50-
`The backup finished being created at ${metadata.endTime.toISOString()}`,
50+
`The backup finished being created at ${new Date(metadata.endTime.seconds * 1000).toISOString()}`,
5151
);
5252
}
5353

samples/api-reference-doc-snippets/backups.list.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,25 @@
1414

1515
async function main(
1616
instanceId = 'YOUR_INSTANCE_ID',
17-
tableId = 'YOUR_TABLE_ID',
1817
clusterId = 'YOUR_CLUSTER_ID',
1918
) {
2019
// [START bigtable_api_list_backups]
21-
const {Bigtable} = require('@google-cloud/bigtable');
22-
const bigtable = new Bigtable();
20+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
21+
const tableAdminClient = new BigtableTableAdminClient();
2322

2423
async function listBackups() {
2524
/**
2625
* TODO(developer): Uncomment these variables before running the sample.
2726
*/
2827
// const instanceId = 'YOUR_INSTANCE_ID';
29-
// const tableId = 'YOUR_TABLE_ID';
3028
// const clusterId = 'YOUR_CLUSTER_ID';
31-
const instance = bigtable.instance(instanceId);
32-
const table = instance.table(tableId);
33-
const cluster = table.cluster(clusterId);
29+
const projectId = await tableAdminClient.getProjectId();
3430

35-
const [backupsFromInstance] = await instance.listBackups();
36-
console.log(
37-
`${backupsFromInstance.length} backups returned from the instance.`,
38-
);
31+
const request = {
32+
parent: tableAdminClient.clusterPath(projectId, instanceId, clusterId),
33+
};
3934

40-
const [backupsFromCluster] = await cluster.listBackups();
35+
const [backupsFromCluster] = await tableAdminClient.listBackups(request);
4136
console.log(
4237
`${backupsFromCluster.length} backups returned from the cluster.`,
4338
);

samples/api-reference-doc-snippets/backups.restore.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ async function main(
1919
backupId = 'YOUR_BACKUP_ID',
2020
) {
2121
// [START bigtable_api_restore_backup]
22+
// eslint-disable-next-line n/no-extraneous-require
23+
const gax = require('google-gax');
2224
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
2325

2426
async function restoreBackup() {
@@ -34,7 +36,7 @@ async function main(
3436
const projectId = await adminClient.getProjectId();
3537

3638
// Restore a table to an instance.
37-
const [operation] = await adminClient.restoreTable({
39+
const [restoreLRO] = await adminClient.restoreTable({
3840
parent: adminClient.instancePath(projectId, instanceId),
3941
tableId,
4042
backup: adminClient.backupPath(
@@ -44,10 +46,30 @@ async function main(
4446
backupId,
4547
),
4648
});
49+
console.log('Waiting for restoreTable operation to complete...');
50+
const [table, metadata] = await restoreLRO.promise();
51+
console.log(`Table ${table.name} restored successfully.`);
4752

48-
const [table] = await operation.promise();
53+
// Await the secondary optimize table operation
54+
const optimizeTableOperationName = metadata.optimizeTableOperationName;
55+
if (optimizeTableOperationName) {
56+
console.log(
57+
`Waiting for optimize table operation: ${optimizeTableOperationName}`,
58+
);
59+
const [rawOptimizeLRO] = await adminClient.operationsClient.getOperation({
60+
name: optimizeTableOperationName,
61+
});
62+
const optimizeRestoreTableLRO = gax.operation(
63+
rawOptimizeLRO,
64+
adminClient.descriptors.longrunning.restoreTable,
65+
{},
66+
);
67+
const [, , info] = await optimizeRestoreTableLRO.promise();
4968

50-
console.log(`Table restored to ${table.name} successfully.`);
69+
console.log(`Optimized table restored to ${info.name} successfully.`);
70+
} else {
71+
console.log('No optimize table operation name found in metadata.');
72+
}
5173
}
5274

5375
await restoreBackup();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
async function main(
16+
instanceId = 'YOUR_INSTANCE_ID',
17+
tableId = 'YOUR_TABLE_ID',
18+
clusterId = 'YOUR_CLUSTER_ID',
19+
backupId = 'YOUR_BACKUP_ID',
20+
) {
21+
// [START bigtable_api_restore_backup_no_optimization]
22+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
23+
24+
async function restoreBackup() {
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const instanceId = 'YOUR_INSTANCE_ID';
29+
// const tableId = 'YOUR_TABLE_ID';
30+
// const clusterId = 'YOUR_CLUSTER_ID';
31+
// const backupId = 'YOUR_BACKUP_ID';
32+
33+
const adminClient = new BigtableTableAdminClient();
34+
const projectId = await adminClient.getProjectId();
35+
36+
// Restore a table to an instance.
37+
const [restoreLRO] = await adminClient.restoreTable({
38+
parent: adminClient.instancePath(projectId, instanceId),
39+
tableId,
40+
backup: adminClient.backupPath(
41+
projectId,
42+
instanceId,
43+
clusterId,
44+
backupId,
45+
),
46+
});
47+
console.log('Waiting for restoreTable operation to complete...');
48+
const [table] = await restoreLRO.promise();
49+
console.log(`Table ${table.name} restored successfully.`);
50+
}
51+
52+
await restoreBackup();
53+
// [END bigtable_api_restore_backup_no_optimization]
54+
}
55+
56+
const args = process.argv.slice(2);
57+
main(...args).catch(console.error);

samples/api-reference-doc-snippets/backups.update.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,41 @@
1414

1515
async function main(
1616
instanceId = 'YOUR_INSTANCE_ID',
17-
tableId = 'YOUR_TABLE_ID',
1817
clusterId = 'YOUR_CLUSTER_ID',
1918
backupId = 'YOUR_BACKUP_ID',
2019
) {
2120
// [START bigtable_api_update_backup]
22-
const {Bigtable} = require('@google-cloud/bigtable');
23-
const bigtable = new Bigtable();
21+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
22+
const tableAdminClient = new BigtableTableAdminClient();
2423

2524
async function updateBackup() {
2625
/**
2726
* TODO(developer): Uncomment these variables before running the sample.
2827
*/
2928
// const instanceId = 'YOUR_INSTANCE_ID';
30-
// const tableId = 'YOUR_TABLE_ID';
3129
// const clusterId = 'YOUR_CLUSTER_ID';
3230
// const backupId = 'YOUR_BACKUP_ID';
33-
const instance = bigtable.instance(instanceId);
34-
const table = instance.table(tableId);
35-
const cluster = table.cluster(clusterId);
36-
const backup = cluster.backup(backupId);
31+
const projectId = await tableAdminClient.getProjectId();
3732

38-
// Extend a backup's life by updating its expiry date.
39-
const [metadata] = await backup.setMetadata({
40-
expireTime: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
41-
});
42-
console.log(`The backup will now auto-delete at ${metadata.expireDate}.`);
33+
const request = {
34+
backup: {
35+
name: tableAdminClient.backupPath(
36+
projectId,
37+
instanceId,
38+
clusterId,
39+
backupId,
40+
),
41+
expireTime: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
42+
},
43+
updateMask: {
44+
paths: ['expire_time'],
45+
},
46+
};
47+
48+
const [metadata] = await tableAdminClient.updateBackup(request);
49+
console.log(
50+
`The backup will now auto-delete at ${new Date(metadata.expireTime.seconds * 1000)}.`,
51+
);
4352
}
4453

4554
await updateBackup();

0 commit comments

Comments
 (0)