Skip to content

Commit

Permalink
samples: Add BigTable delete samples (#590)
Browse files Browse the repository at this point in the history
* doc: Add BigTable delete samples

- Add the deleteion snippets
- Add test cases
- Update the row_key_prefix value
- Remove `_sample` from function names.
- Refactor the snapshot match assertion.
- Renamed column to column_family_obj

Closes internal issue #213629071

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
Mariatta and gcf-owl-bot[bot] authored Jun 21, 2022
1 parent 6905cf9 commit 16fa5ad
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 3 deletions.
6 changes: 3 additions & 3 deletions samples/snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

## Python Samples for Cloud Bigtable

This directory contains samples for Cloud Bigtable, which may be used as a refererence for how to use this product.
Samples, quickstarts, and other documentation are available at <a href="https://cloud.google.com/bigtable">cloud.google.com</a>.
This directory contains samples for Cloud Bigtable, which may be used as a reference for how to use this product.
Samples, quickstarts, and other documentation are available at [cloud.google.com](https://cloud.google.com/bigtable).


### Snippets
Expand All @@ -17,7 +17,7 @@ This folder contains snippets for Python Cloud Bigtable.
## Additional Information

You can read the documentation for more details on API usage and use GitHub
to <a href="https://github.com/googleapis/python-bigtable">browse the source</a> and [report issues][issues].
to [browse the source](https://github.com/googleapis/python-bigtable) and [report issues][issues].

### Contributing
View the [contributing guidelines][contrib_guide], the [Python style guide][py_style] for more information.
Expand Down
122 changes: 122 additions & 0 deletions samples/snippets/deletes/deletes_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python

# 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.


from google.cloud import bigtable

# Write your code here.


# [START bigtable_delete_from_column]
def delete_from_column(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
row.delete_cell(column_family_id="cell_plan", column="data_plan_01gb")
row.commit()


# [END bigtable_delete_from_column]

# [START bigtable_delete_from_column_family]
def delete_from_column_family(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
row.delete_cells(
column_family_id="cell_plan", columns=["data_plan_01gb", "data_plan_05gb"]
)
row.commit()


# [END bigtable_delete_from_column_family]


# [START bigtable_delete_from_row]
def delete_from_row(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
row.delete()
row.commit()


# [END bigtable_delete_from_row]

# [START bigtable_streaming_and_batching]
def streaming_and_batching(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
batcher = table.mutations_batcher(flush_count=2)
rows = table.read_rows()
for row in rows:
row = table.row(row.row_key)
row.delete_cell(column_family_id="cell_plan", column="data_plan_01gb")

batcher.mutate_rows(rows)


# [END bigtable_streaming_and_batching]

# [START bigtable_check_and_mutate]
def check_and_mutate(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
row.delete_cell(column_family_id="cell_plan", column="data_plan_01gb")
row.delete_cell(column_family_id="cell_plan", column="data_plan_05gb")
row.commit()


# [END bigtable_check_and_mutate]


# [START bigtable_drop_row_range]
def drop_row_range(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row_key_prefix = "phone#4c410523"
table.drop_by_prefix(row_key_prefix, timeout=200)


# [END bigtable_drop_row_range]

# [START bigtable_delete_column_family]
def delete_column_family(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
column_family_id = "stats_summary"
column_family_obj = table.column_family(column_family_id)
column_family_obj.delete()


# [END bigtable_delete_column_family]

# [START bigtable_delete_table]
def delete_table(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
table.delete()


# [END bigtable_delete_table]
139 changes: 139 additions & 0 deletions samples/snippets/deletes/deletes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Copyright 2020, 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.


import datetime
import os
import time
import uuid

from google.cloud import bigtable
import pytest

import deletes_snippets

PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
TABLE_ID_PREFIX = "mobile-time-series-{}"


@pytest.fixture(scope="module", autouse=True)
def table_id():
from google.cloud.bigtable.row_set import RowSet

client = bigtable.Client(project=PROJECT, admin=True)
instance = client.instance(BIGTABLE_INSTANCE)

table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
table = instance.table(table_id)
if table.exists():
table.delete()

table.create(column_families={"stats_summary": None, "cell_plan": None})

timestamp = datetime.datetime(2019, 5, 1)
timestamp_minus_hr = datetime.datetime(2019, 5, 1) - datetime.timedelta(hours=1)

row_keys = [
"phone#4c410523#20190501",
"phone#4c410523#20190502",
"phone#4c410523#20190505",
"phone#5c10102#20190501",
"phone#5c10102#20190502",
]

rows = [table.direct_row(row_key) for row_key in row_keys]

rows[0].set_cell("stats_summary", "connected_cell", 1, timestamp)
rows[0].set_cell("stats_summary", "connected_wifi", 1, timestamp)
rows[0].set_cell("stats_summary", "os_build", "PQ2A.190405.003", timestamp)
rows[0].set_cell("cell_plan", "data_plan_01gb", "true", timestamp_minus_hr)
rows[0].set_cell("cell_plan", "data_plan_01gb", "false", timestamp)
rows[0].set_cell("cell_plan", "data_plan_05gb", "true", timestamp)
rows[1].set_cell("stats_summary", "connected_cell", 1, timestamp)
rows[1].set_cell("stats_summary", "connected_wifi", 1, timestamp)
rows[1].set_cell("stats_summary", "os_build", "PQ2A.190405.004", timestamp)
rows[1].set_cell("cell_plan", "data_plan_05gb", "true", timestamp)
rows[2].set_cell("stats_summary", "connected_cell", 0, timestamp)
rows[2].set_cell("stats_summary", "connected_wifi", 1, timestamp)
rows[2].set_cell("stats_summary", "os_build", "PQ2A.190406.000", timestamp)
rows[2].set_cell("cell_plan", "data_plan_05gb", "true", timestamp)
rows[3].set_cell("stats_summary", "connected_cell", 1, timestamp)
rows[3].set_cell("stats_summary", "connected_wifi", 1, timestamp)
rows[3].set_cell("stats_summary", "os_build", "PQ2A.190401.002", timestamp)
rows[3].set_cell("cell_plan", "data_plan_10gb", "true", timestamp)
rows[4].set_cell("stats_summary", "connected_cell", 1, timestamp)
rows[4].set_cell("stats_summary", "connected_wifi", 0, timestamp)
rows[4].set_cell("stats_summary", "os_build", "PQ2A.190406.000", timestamp)
rows[4].set_cell("cell_plan", "data_plan_10gb", "true", timestamp)

table.mutate_rows(rows)

# Ensure mutations have propagated.
row_set = RowSet()

for row_key in row_keys:
row_set.add_row_key(row_key)

fetched = list(table.read_rows(row_set=row_set))

while len(fetched) < len(rows):
time.sleep(5)
fetched = list(table.read_rows(row_set=row_set))

yield table_id


def assert_snapshot_match(capsys, snapshot):
out, _ = capsys.readouterr()
snapshot.assert_match(out)


def test_delete_from_column(capsys, snapshot, table_id):
deletes_snippets.delete_from_column(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_delete_from_column_family(capsys, snapshot, table_id):
deletes_snippets.delete_from_column_family(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_delete_from_row(capsys, snapshot, table_id):
deletes_snippets.delete_from_row(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_streaming_and_batching(capsys, snapshot, table_id):
deletes_snippets.streaming_and_batching(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_check_and_mutate(capsys, snapshot, table_id):
deletes_snippets.check_and_mutate(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_drop_row_range(capsys, snapshot, table_id):
deletes_snippets.drop_row_range(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_delete_column_family(capsys, snapshot, table_id):
deletes_snippets.delete_column_family(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)


def test_delete_table(capsys, snapshot, table_id):
deletes_snippets.delete_table(PROJECT, BIGTABLE_INSTANCE, table_id)
assert_snapshot_match(capsys, snapshot)
Loading

0 comments on commit 16fa5ad

Please sign in to comment.