Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 5 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
30 changes: 30 additions & 0 deletions samples/snippets/writes/write_increment_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [START bigtable_write_increment_async]
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data.mutations import AddToCell, RowMutationEntry
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup

async def write_increment_async(project_id, instance_id, table_id):
"""Increments a value in a Bigtable table using the async client."""
async with BigtableDataClientAsync(project=project_id) as client:
table = client.get_table(instance_id, table_id)
row_key = "unique_device_ids_1"
Comment thread
KasiaStrz marked this conversation as resolved.
try:
async with table.mutations_batcher() as batcher:
# The AddToCell mutation increments the value of a cell.
# The value must be a positive or negative integer.
reading = AddToCell(
family="counters",
qualifier="odometer",
value=32304
)
await batcher.append(RowMutationEntry(row_key.encode("utf-8"), [reading]))
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 for row {failed_entry.row_key!r} with error: {cause!r}"
)
# [END bigtable_write_increment_async]
Comment thread
KasiaStrz marked this conversation as resolved.
12 changes: 11 additions & 1 deletion samples/snippets/writes/writes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import os
import uuid

import backoff
from google.api_core.exceptions import DeadlineExceeded
import pytest
import uuid

from .write_increment_async import write_increment_async
from .write_batch import write_batch
from .write_conditionally import write_conditional
from .write_increment import write_increment
Expand Down Expand Up @@ -71,3 +73,11 @@ def _write_batch():
_write_batch()
out, _ = capsys.readouterr()
assert "Successfully wrote 2 rows" in out

@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
def _write_increment_async():
asyncio.run(write_increment_async(PROJECT, BIGTABLE_INSTANCE, table_id))

_write_increment_async()
out, _ = capsys.readouterr()
assert "Successfully incremented row" in out
Loading