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

Commit 326961f

Browse files
docs: For restoreTable and garbage collection rules samples use the admin client, not the handwritten layer (#1697)
* 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 --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 7457ac3 commit 326961f

10 files changed

Lines changed: 318 additions & 97 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,39 @@
1515
async function main(
1616
instanceId = 'YOUR_INSTANCE_ID',
1717
tableId = 'YOUR_TABLE_ID',
18+
clusterId = 'YOUR_CLUSTER_ID',
1819
backupId = 'YOUR_BACKUP_ID',
1920
) {
2021
// [START bigtable_api_restore_backup]
21-
const {Bigtable} = require('@google-cloud/bigtable');
22-
const bigtable = new Bigtable();
22+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
2323

2424
async function restoreBackup() {
2525
/**
2626
* TODO(developer): Uncomment these variables before running the sample.
2727
*/
2828
// const instanceId = 'YOUR_INSTANCE_ID';
2929
// const tableId = 'YOUR_TABLE_ID';
30+
// const clusterId = 'YOUR_CLUSTER_ID';
3031
// const backupId = 'YOUR_BACKUP_ID';
31-
const instance = bigtable.instance(instanceId);
32+
33+
const adminClient = new BigtableTableAdminClient();
34+
const projectId = await adminClient.getProjectId();
3235

3336
// Restore a table to an instance.
34-
const [table, operation] = await instance.createTableFromBackup({
35-
table: tableId,
36-
backup: backupId,
37+
const [operation] = await adminClient.restoreTable({
38+
parent: adminClient.instancePath(projectId, instanceId),
39+
tableId,
40+
backup: adminClient.backupPath(
41+
projectId,
42+
instanceId,
43+
clusterId,
44+
backupId,
45+
),
3746
});
3847

39-
await operation.promise();
40-
console.log(`Table restored to ${table.id} successfully.`);
48+
const [table] = await operation.promise();
49+
50+
console.log(`Table restored to ${table.name} successfully.`);
4151
}
4252

4353
await restoreBackup();

samples/api-reference-doc-snippets/instance.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,38 +110,54 @@ const snippets = {
110110
// [END bigtable_api_create_app_profile]
111111
},
112112

113-
createTable: (instanceId, tableId) => {
113+
createTable: async (instanceId, tableId) => {
114114
// [START bigtable_api_create_table]
115-
const {Bigtable} = require('@google-cloud/bigtable');
116-
const bigtable = new Bigtable();
117-
const instance = bigtable.instance(instanceId);
118-
119-
const options = {
120-
families: ['follows'],
115+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
116+
const adminClient = new BigtableTableAdminClient();
117+
const projectId = await adminClient.getProjectId();
118+
119+
const request = {
120+
parent: adminClient.instancePath(projectId, instanceId),
121+
tableId: tableId,
122+
table: {
123+
columnFamilies: {
124+
follows: {},
125+
},
126+
},
121127
};
122128

123129
// You can also specify garbage collection rules for your column families.
124130
// See {@link Table#createFamily} for more information about
125131
// column families and garbage collection rules.
126132
//-
127-
// const options = {
128-
// families: [
129-
// {
130-
// name: 'follows',
131-
// rule: {
132-
// age: {
133-
// seconds: 0,
134-
// nanos: 5000
133+
// const request = {
134+
// parent: instance.name,
135+
// tableId: tableId,
136+
// table: {
137+
// columnFamilies: {
138+
// follows: {
139+
// gcRule: {
140+
// union: {
141+
// rules: [
142+
// {
143+
// maxAge: {
144+
// seconds: 0,
145+
// nanos: 5000,
146+
// },
147+
// },
148+
// {
149+
// maxNumVersions: 3,
150+
// },
151+
// ],
152+
// },
135153
// },
136-
// versions: 3,
137-
// union: true
138-
// }
139-
// }
140-
// ]
154+
// },
155+
// },
156+
// },
141157
// };
142158

143-
instance
144-
.createTable(tableId, options)
159+
adminClient
160+
.createTable(request)
145161
.then(result => {
146162
const newTable = result[0];
147163
// const apiResponse = result[1];

samples/api-reference-doc-snippets/table.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const {Bigtable} = require('@google-cloud/bigtable');
15+
const {Bigtable, GCRuleMaker} = require('@google-cloud/bigtable');
1616
const bigtable = new Bigtable();
1717

1818
const snippets = {
19-
createTable: (instanceId, tableId) => {
20-
const instance = bigtable.instance(instanceId);
21-
const table = instance.table(tableId);
22-
19+
createTable: async (instanceId, tableId) => {
2320
// [START bigtable_api_create_table]
24-
table
25-
.create()
21+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
22+
const adminClient = new BigtableTableAdminClient();
23+
const projectId = await adminClient.getProjectId();
24+
const request = {
25+
parent: adminClient.instancePath(projectId, instanceId),
26+
tableId: tableId,
27+
table: {
28+
columnFamilies: {
29+
follows: {},
30+
},
31+
},
32+
};
33+
adminClient
34+
.createTable(request)
2635
.then(result => {
2736
const table = result[0];
2837
// let apiResponse = result[1];
@@ -83,23 +92,34 @@ const snippets = {
8392
// [END bigtable_api_get_table_meta]
8493
},
8594

86-
createFamily: (instanceId, tableId, familyId) => {
87-
const instance = bigtable.instance(instanceId);
88-
const table = instance.table(tableId);
89-
95+
createFamily: async (instanceId, tableId, familyId) => {
9096
// [START bigtable_api_create_family]
97+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
98+
const adminClient = new BigtableTableAdminClient();
99+
const projectId = await adminClient.getProjectId();
100+
// The request will only work if the projectName doesn't contain the {{projectId}} token.
91101
const options = {};
92-
// options.rule = {
93-
// age: {
94-
// seconds: 0,
95-
// nanos: 5000
96-
// },
97-
// versions: 3,
98-
// union: true
99-
// };
100-
101-
table
102-
.createFamily(familyId, options)
102+
// {
103+
// ruleType: 'union',
104+
// maxVersions: 3,
105+
// maxAge: {
106+
// seconds: 1,
107+
// nanos: 5000,
108+
// },
109+
// };
110+
111+
adminClient
112+
.modifyColumnFamilies({
113+
name: adminClient.tablePath(projectId, instanceId, tableId),
114+
modifications: [
115+
{
116+
id: familyId,
117+
create: {
118+
gcRule: GCRuleMaker.makeRule(options),
119+
},
120+
},
121+
],
122+
})
103123
.then(result => {
104124
const family = result[0];
105125
// const apiResponse = result[1];

samples/hello-world/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,22 @@ const getRowGreeting = row => {
5151
const [tableExists] = await table.exists();
5252
if (!tableExists) {
5353
console.log(`Creating table ${TABLE_ID}`);
54-
const options = {
55-
families: [
56-
{
57-
name: COLUMN_FAMILY_ID,
58-
rule: {
59-
versions: 1,
54+
const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
55+
const adminClient = new BigtableTableAdminClient();
56+
const projectId = await adminClient.getProjectId();
57+
await adminClient.createTable({
58+
parent: adminClient.instancePath(projectId, INSTANCE_ID),
59+
tableId: TABLE_ID,
60+
table: {
61+
columnFamilies: {
62+
[COLUMN_FAMILY_ID]: {
63+
gcRule: {
64+
maxNumVersions: 1,
65+
},
6066
},
6167
},
62-
],
63-
};
64-
await table.create(options);
68+
},
69+
});
6570
}
6671
// [END bigtable_hw_create_table]
6772

0 commit comments

Comments
 (0)