-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtableadmin.js
257 lines (230 loc) · 8.26 KB
/
tableadmin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Imports the Google Cloud client library
const {Bigtable} = require('@google-cloud/bigtable');
async function runTableOperations(instanceID, tableID) {
const bigtable = new Bigtable();
const instance = bigtable.instance(instanceID);
const table = instance.table(tableID);
// Check if table exists
console.log();
console.log('Checking if table exists...');
const [tableExists] = await table.exists();
if (!tableExists) {
// Create table if does not exist
console.log(`Table does not exist. Creating table ${tableID}`);
await table.create();
} else {
console.log('Table exists.');
}
console.log();
console.log('Listing tables in current project...');
// [START bigtable_list_tables]
// List tables in current project
const [tables] = await instance.getTables();
tables.forEach(table => {
console.log(table.id);
});
// [END bigtable_list_tables]
console.log();
console.log('Printing table metadata...');
// [START bigtable_get_table_metadata]
// Get table metadata, and apply a view to the table fields
// Supported views include ID, schema or full
// View defaults to schema if unspecified.
const options = {
view: 'id',
};
const [tableMetadata] = await table.getMetadata(options);
console.log(`Metadata: ${JSON.stringify(tableMetadata)}`);
// [END bigtable_get_table_metadata]
console.log();
console.log('Creating column family cf1 with max age GC rule...');
// [START bigtable_create_family_gc_max_age]
// Create a column family with GC policy : maximum age
// where age = current time minus cell timestamp
// Define the GC rule to retain data with max age of 5 days
const maxAgeRule = {
rule: {
age: {
// Value must be atleast 1 millisecond
seconds: 60 * 60 * 24 * 5,
nanos: 0,
},
},
};
let [family] = await table.createFamily('cf1', maxAgeRule);
console.log(`Created column family ${family.id}`);
// [END bigtable_create_family_gc_max_age]
console.log();
console.log('Creating column family cf2 with max versions GC rule...');
// [START bigtable_create_family_gc_max_versions]
// Create a column family with GC policy : most recent N versions
// where 1 = most recent version
// Define the GC policy to retain only the most recent 2 versions
const maxVersionsRule = {
rule: {
versions: 2,
},
};
// Create a column family with given GC rule
[family] = await table.createFamily('cf2', maxVersionsRule);
console.log(`Created column family ${family.id}`);
// [END bigtable_create_family_gc_max_versions]
console.log();
console.log('Creating column family cf3 with union GC rule...');
// [START bigtable_create_family_gc_union]
// Create a column family with GC policy to drop data that matches at least one condition.
// Define a GC rule to drop cells older than 5 days or not the most recent version
const unionRule = {
rule: {
versions: 1,
age: {
seconds: 60 * 60 * 24 * 5,
nanos: 0,
},
union: true,
},
};
[family] = await table.createFamily('cf3', unionRule);
console.log(`Created column family ${family.id}`);
// [END bigtable_create_family_gc_union]
console.log();
console.log('Creating column family cf4 with intersect GC rule...');
// [START bigtable_create_family_gc_intersection]
// Create a column family with GC policy to drop data that matches all conditions
// GC rule: Drop cells older than 5 days AND older than the most recent 2 versions
const intersectionRule = {
rule: {
versions: 2,
age: {
seconds: 60 * 60 * 24 * 5,
nanos: 0,
},
intersection: true,
},
};
[family] = await table.createFamily('cf4', intersectionRule);
console.log(`Created column family ${family.id}`);
// [END bigtable_create_family_gc_intersection]
console.log();
console.log('Creating column family cf5 with a nested GC rule...');
// [START bigtable_create_family_gc_nested]
// Create a nested GC rule:
// Drop cells that are either older than the 10 recent versions
// OR
// Drop cells that are older than a month AND older than the 2 recent versions
const nestedRule = {
union: true,
versions: 10,
rule: {
versions: 2,
age: {
// one month
seconds: 60 * 60 * 24 * 30,
nanos: 0,
},
},
};
[family] = await table.createFamily('cf5', nestedRule);
console.log(`Created column family ${family.id}`);
// [END bigtable_create_family_gc_nested]
console.log();
console.log('Printing ID and GC Rule for all column families...');
// [START bigtable_list_column_families]
// List all families in the table with GC rules
const [families] = await table.getFamilies();
// Print ID, GC Rule for each column family
families.forEach(family => {
const metadata = JSON.stringify(family.metadata);
console.log(`Column family: ${family.id}, Metadata: ${metadata}`);
/* Sample output:
Column family: projects/{{projectId}}/instances/my-instance/tables/my-table/columnFamilies/cf4,
Metadata: {"gcRule":{"intersection":{"rules":[{"maxAge":{"seconds":"432000","nanos":0},"rule":"maxAge"},{"maxNumVersions":2,"rule":"maxNumVersions"}]},"rule":"intersection"}}
*/
});
// [END bigtable_list_column_families]
console.log('\nUpdating column family cf1 GC rule...');
// [START bigtable_update_gc_rule]
// Update the column family metadata to update the GC rule
// Create a reference to the column family
family = table.family('cf1');
// Update a column family GC rule
const updatedMetadata = {
rule: {
versions: 1,
},
};
const [apiResponse] = await family.setMetadata(updatedMetadata);
console.log(`Updated GC rule: ${JSON.stringify(apiResponse)}`);
// [END bigtable_update_gc_rule]
console.log('\nPrint updated column family cf1 GC rule...');
// [START bigtable_family_get_gc_rule]
// Retrieve column family metadata (Id, column family GC rule)
const [metadata] = await family.getMetadata();
console.log(`Metadata: ${JSON.stringify(metadata)}`);
// [END bigtable_family_get_gc_rule]
console.log('\nDelete a column family cf2...');
// [START bigtable_delete_family]
// Delete a column family
await family.delete();
console.log(`${family.id} deleted successfully\n`);
// [END bigtable_delete_family]
console.log(
'Run node $0 delete --instance [instanceID] --table [tableID] to delete the table.\n'
);
}
async function deleteTable(instanceID, tableID) {
// const instanceID = "my-instance";
// const tableID = "my-bigtable-ID";
const bigtableClient = new Bigtable();
const instance = bigtableClient.instance(instanceID);
const table = instance.table(tableID);
// [START bigtable_delete_table]
// Delete the entire table
console.log('Delete the table.');
await table.delete();
console.log(`Table deleted: ${table.id}`);
// [END bigtable_delete_table]
}
require('yargs')
.demand(1)
.command(
'run',
'Create a table (if does not exist) and run basic table operations.',
{},
argv => runTableOperations(argv.instance, argv.table)
)
.example(
'node $0 run --instance [instanceID] --table [tableID]',
'Create a table (if does not exist) and run basic table operations.'
)
.wrap(120)
.command('delete', 'Delete table.', {}, argv =>
deleteTable(argv.instance, argv.table)
)
.example(
'node $0 delete --instance [instanceID] --table [tableID]',
'Delete a table.'
)
.wrap(120)
.nargs('instance', 1)
.nargs('table', 1)
.describe('instance', 'Cloud Bigtable Instance ID')
.describe('table', 'Cloud Bigtable Table ID')
.demandOption(['instance', 'table'])
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/bigtable/docs')
.help()
.strict().argv;