-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): add missing quickstart samples for asyncio (#974)
- Loading branch information
1 parent
2a2bbfd
commit f02b36f
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |