forked from googleapis/nodejs-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: Bigtable deletion code samples (googleapis#1078)
* First code samples * Add the end exclude tag * Update the comments * Add integration test for delete code snippets * Three more test cases * Add the final test case * Add header * linting fix * Add call options * Use before instead of beforeEach * PR updates * Remove comment that is unimportant * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove start exclude and end exclude tags * row range feedback * Delete row snapshot * Slight change to fix samples test * switch batch delete to batch insert * Removed line of code that is shortening timestamp * Correct snapshot * Revert "Removed line of code that is shortening timestamp" This reverts commit 96adcdbee42e26e53aa270a43713732342cf902c. * Fix to short snapshot. * Revert "Fix to short snapshot." This reverts commit 73736e694014e0730acba2635186b6659961a610. * Revert "Correct snapshot" This reverts commit f86b6fab4207aba417e974d147f3f048a423208f. * Revert "switch batch delete to batch insert" This reverts commit 72cef5d5da00041cd09a49d155f05c4402143e3f. * streaming and batching changes Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1a2e3fb
commit 886af7a
Showing
5 changed files
with
588 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright 2022 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. | ||
|
||
exports['deletes should delete column from a row 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.003 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_01gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190401.002 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 0 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
`; | ||
|
||
exports['deletes should delete column family from a row 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.003 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190401.002 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 0 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
`; | ||
|
||
exports['deletes should delete a row 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_05gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190401.002 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 0 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
`; | ||
|
||
exports['deletes should stream rows and then do a batch delete 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190401.002 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 0 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
`; | ||
|
||
exports['deletes should check and mutate 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190401.002 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 0 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
Column Family cell_plan | ||
\tdata_plan_10gb: @1559347200000000 | ||
`; | ||
|
||
exports['deletes should delete a whole range of rows 1'] = ` | ||
Column Family stats_summary | ||
\tconnected_cell: 1 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190405.004 @1559347200000000 | ||
Column Family stats_summary | ||
\tconnected_cell: 0 @1559347200000000 | ||
\tconnected_wifi: 1 @1559347200000000 | ||
\tos_build: PQ2A.190406.000 @1559347200000000 | ||
`; | ||
|
||
exports['deletes should delete a column family 1'] = ` | ||
`; | ||
|
||
exports['deletes should delete a table 1'] = ` | ||
[ false ] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright 2022 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. | ||
|
||
'use strict'; | ||
|
||
async function main( | ||
instanceId = 'YOUR_INSTANCE_ID', | ||
tableId = 'YOUR_TABLE_ID', | ||
deleteType = 'readRow' | ||
) { | ||
// [START bigtable_deletes_print] | ||
const {Bigtable} = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
|
||
// TODO: We need to import Mutation.methods.DELETE from mutation.ts, but I guess we need to compile typescript? | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const instanceId = 'YOUR_INSTANCE_ID'; | ||
// const tableId = 'YOUR_TABLE_ID'; | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
switch (deleteType) { | ||
case 'deleteFromColumn': { | ||
// [START bigtable_delete_from_column] | ||
await table.mutate({ | ||
key: 'phone#4c410523#20190501', | ||
method: 'delete', | ||
data: { | ||
column: 'cell_plan:data_plan_05gb', | ||
}, | ||
}); | ||
await printRows(); | ||
// [END bigtable_delete_from_column] | ||
break; | ||
} | ||
case 'deleteFromFamily': { | ||
// [START bigtable_delete_from_column_family] | ||
await table.mutate({ | ||
key: 'phone#4c410523#20190501', | ||
method: 'delete', | ||
data: { | ||
column: 'cell_plan', | ||
}, | ||
}); | ||
await printRows(); | ||
// [END bigtable_delete_from_column_family] | ||
break; | ||
} | ||
case 'deleteFromRow': { | ||
// [START bigtable_deletes_from_row] | ||
const row = table.row('phone#4c410523#20190501'); | ||
await row.delete(); | ||
await printRows(); | ||
// [END bigtable_deletes_from_row] | ||
break; | ||
} | ||
case 'streamingAndBatching': { | ||
// [START bigtable_streaming_and_batching] | ||
const rows = (await table.getRows({limit: 2}))[0]; | ||
const entries = rows.map(row => { | ||
return { | ||
key: row.id, | ||
method: 'delete', | ||
data: { | ||
column: 'cell_plan:data_plan_05gb', | ||
}, | ||
}; | ||
}); | ||
await table.mutate(entries); | ||
await printRows(); | ||
// [END bigtable_streaming_and_batching] | ||
break; | ||
} | ||
case 'checkAndMutate': { | ||
// [START bigtable_check_and_mutate] | ||
const row = table.row('phone#4c410523#20190502'); | ||
await row.filter( | ||
{ | ||
column: 'data_plan_05gb', | ||
}, | ||
{ | ||
onMatch: { | ||
key: 'phone#4c410523#20190502', | ||
method: 'delete', | ||
}, | ||
} | ||
); | ||
await printRows(); | ||
// [END bigtable_check_and_mutate] | ||
break; | ||
} | ||
case 'dropRowRange': { | ||
// [START bigtable_drop_row_range] | ||
await table.deleteRows('phone#5c10102'); | ||
await printRows(); | ||
// [END bigtable_drop_row_range] | ||
break; | ||
} | ||
case 'deleteColumnFamily': { | ||
// [START bigtable_delete_column_family] | ||
const cf = table.family('stats_summary'); | ||
await cf.delete(); | ||
await printRows(); | ||
// [END bigtable_delete_column_family] | ||
break; | ||
} | ||
case 'deleteTable': { | ||
// [START bigtable_delete_table] | ||
await table.delete(); | ||
console.log(await table.exists({})); | ||
// [END bigtable_delete_table] | ||
break; | ||
} | ||
} | ||
|
||
async function printRows() { | ||
const rows = (await table.getRows({}))[0]; | ||
for (const row of rows) { | ||
const rowData = row.data; | ||
for (const columnFamily of Object.keys(rowData)) { | ||
const columnFamilyData = rowData[columnFamily]; | ||
console.log(`Column Family ${columnFamily}`); | ||
|
||
for (const columnQualifier of Object.keys(columnFamilyData)) { | ||
const col = columnFamilyData[columnQualifier]; | ||
|
||
for (const cell of col) { | ||
const labels = cell.labels.length | ||
? ` [${cell.labels.join(',')}]` | ||
: ''; | ||
console.log( | ||
`\t${columnQualifier}: ${cell.value} @${cell.timestamp}${labels}` | ||
); | ||
} | ||
} | ||
} | ||
} | ||
console.log(); | ||
} | ||
} | ||
// [END bigtable_deletes_print] | ||
|
||
main(...process.argv.slice(2)); |
Oops, something went wrong.