Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(docs): improve write_batch async sample #966

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions samples/snippets/data_client/data_client_snippets_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,34 @@ async def write_batch(table):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data.mutations import SetCell
from google.cloud.bigtable.data.mutations import RowMutationEntry
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup

async def write_batch(project_id, instance_id, table_id):
async with BigtableDataClientAsync(project=project_id) as client:
async with client.get_table(instance_id, table_id) as table:
family_id = "stats_summary"

async with table.mutations_batcher() as batcher:
mutation_list = [
SetCell(family_id, "connected_cell", 1),
SetCell(family_id, "connected_wifi", 1),
SetCell(family_id, "os_build", "12155.0.0-rc1"),
]
batcher.append(
RowMutationEntry("tablet#a0b81f74#20190501", mutation_list)
)
batcher.append(
RowMutationEntry("tablet#a0b81f74#20190502", mutation_list)
)
try:
async with table.mutations_batcher() as batcher:
mutation_list = [
SetCell(family_id, "connected_cell", 1),
SetCell(family_id, "connected_wifi", 1),
SetCell(family_id, "os_build", "12155.0.0-rc1"),
]
# awaiting the batcher.append method adds the RowMutationEntry
# to the batcher's queue to be written in the next flush.
await batcher.append(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment that this await is for scheduling the mutation and not for applying the mutation...that will only happen when the context manager exits or batcher.close() is called

RowMutationEntry("tablet#a0b81f74#20190501", mutation_list)
)
await batcher.append(
RowMutationEntry("tablet#a0b81f74#20190502", mutation_list)
)
except MutationsExceptionGroup as e:
# MutationsExceptionGroup contains a FailedMutationEntryError for
# each mutation that failed.
for sub_exception in e.exceptions:
failed_entry: RowMutationEntry = sub_exception.entry
cause: Exception = sub_exception.__cause__
print(f"Failed mutation: {failed_entry.row_key} with error: {cause!r}")
# [END bigtable_async_writes_batch]
await write_batch(table.client.project, table.instance_id, table.table_id)

Expand Down
Loading