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): add missing quickstart samples for asyncio #974

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions samples/quickstart/main_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python

# Copyright 2024 Google Inc.
#
# 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.

# [START bigtable_quickstart_asyncio]
import argparse
import asyncio

from google.cloud.bigtable.data import BigtableDataClientAsync


async def main(project_id="project-id", instance_id="instance-id", table_id="my-table"):
# Create a Cloud Bigtable client.
client = BigtableDataClientAsync(project=project_id)

# Open an existing table.
table = client.get_table(instance_id, table_id)

row_key = "r1"
row = await table.read_row(row_key)

column_family_id = "cf1"
column_id = b"c1"
value = row.get_cells(column_family_id, column_id)[0].value.decode("utf-8")

await table.close()
await client.close()

print("Row key: {}\nData: {}".format(row_key, value))


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("project_id", help="Your Cloud Platform project ID.")
parser.add_argument(
"instance_id", help="ID of the Cloud Bigtable instance to connect to."
)
parser.add_argument(
"--table", help="Existing table used in the quickstart.", default="my-table"
)

args = parser.parse_args()
asyncio.get_event_loop().run_until_complete(
main(args.project_id, args.instance_id, args.table)
)

# [END bigtable_quickstart_asyncio]
78 changes: 78 additions & 0 deletions samples/quickstart/main_async_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2024 Google Inc.
#
# 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 os
from typing import AsyncGenerator

from google.cloud.bigtable.data import BigtableDataClientAsync, SetCell
import pytest, pytest_asyncio

from main_async import main


PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
TABLE_ID_FORMAT = "quickstart-test-{}"


@pytest_asyncio.fixture
async def table_id() -> AsyncGenerator[str, None]:
table_id = _create_table()
await _populate_table(table_id)

yield table_id

_delete_table(table_id)


def _create_table():
from google.cloud import bigtable
import uuid

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

table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8])
table = instance.table(table_id)
if table.exists():
table.delete()

table.create(column_families={"cf1": None})

client.close()
return table_id


async def _populate_table(table_id: str):
async with BigtableDataClientAsync(project=PROJECT) as client:
async with client.get_table(BIGTABLE_INSTANCE, table_id) as table:
await table.mutate_row("r1", SetCell("cf1", "c1", "test-value"))


def _delete_table(table_id: str):
from google.cloud import bigtable

client = bigtable.Client(project=PROJECT, admin=True)
instance = client.instance(BIGTABLE_INSTANCE)
table = instance.table(table_id)
table.delete()
client.close()


@pytest.mark.asyncio
async def test_main(capsys, table_id):
await main(PROJECT, BIGTABLE_INSTANCE, table_id)

out, _ = capsys.readouterr()
assert "Row key: r1\nData: test-value\n" in out
Loading