Skip to content

Commit

Permalink
samples: Bigtable deletion code samples (googleapis#1078)
Browse files Browse the repository at this point in the history
* 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
danieljbruce and gcf-owl-bot[bot] authored Jun 20, 2022
1 parent 1a2e3fb commit 886af7a
Show file tree
Hide file tree
Showing 5 changed files with 588 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-bigtable/tr

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Delete Snippets | [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/deleteSnippets.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigtable&page=editor&open_in_editor=samples/deleteSnippets.js,samples/README.md) |
| Filter Snippets | [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/filterSnippets.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigtable&page=editor&open_in_editor=samples/filterSnippets.js,samples/README.md) |
| Instances | [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/instances.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigtable&page=editor&open_in_editor=samples/instances.js,samples/README.md) |
| Quickstart | [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigtable&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Delete Snippets](#delete-snippets)
* [Filter Snippets](#filter-snippets)
* [Instances](#instances)
* [Quickstart](#quickstart)
Expand All @@ -37,6 +38,23 @@ Before running the samples, make sure you've followed the steps outlined in



### Delete Snippets

View the [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/deleteSnippets.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigtable&page=editor&open_in_editor=samples/deleteSnippets.js,samples/README.md)

__Usage:__


`node samples/deleteSnippets.js`


-----




### Filter Snippets

View the [source code](https://github.com/googleapis/nodejs-bigtable/blob/main/samples/filterSnippets.js).
Expand Down
183 changes: 183 additions & 0 deletions samples/__snapshots__/deletes.js
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 ]
`;
156 changes: 156 additions & 0 deletions samples/deleteSnippets.js
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));
Loading

0 comments on commit 886af7a

Please sign in to comment.