Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 44c983d

Browse files
committed
feat: Add async sample for incrementing a value
1 parent 72dfdc4 commit 44c983d

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [START bigtable_write_increment_async]
2+
from google.cloud.bigtable.data import BigtableDataClientAsync
3+
from google.cloud.bigtable.data.mutations import AddToCell, RowMutationEntry
4+
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup
5+
6+
async def write_increment_async(project_id, instance_id, table_id):
7+
"""Increments a value in a Bigtable table using the async client."""
8+
async with BigtableDataClientAsync(project=project_id) as client:
9+
table = client.get_table(instance_id, table_id)
10+
row_key = "unique_device_ids_1"
11+
try:
12+
async with table.mutations_batcher() as batcher:
13+
# The AddToCell mutation increments the value of a cell.
14+
# The value must be a positive or negative integer.
15+
reading = AddToCell(
16+
family="counters",
17+
qualifier="odometer",
18+
value=32304
19+
)
20+
await batcher.append(RowMutationEntry(row_key.encode("utf-8"), [reading]))
21+
except MutationsExceptionGroup as e:
22+
# MutationsExceptionGroup contains a FailedMutationEntryError for
23+
# each mutation that failed.
24+
for sub_exception in e.exceptions:
25+
failed_entry: RowMutationEntry = sub_exception.entry
26+
cause: Exception = sub_exception.__cause__
27+
print(
28+
f"Failed mutation for row {failed_entry.row_key!r} with error: {cause!r}"
29+
)
30+
# [END bigtable_write_increment_async]

0 commit comments

Comments
 (0)