8000 Asyncio sample (#11837) · dtest/python-docs-samples@8cf862f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cf862f

Browse files
Asyncio sample (GoogleCloudPlatform#11837)
1 parent 5b6e3d9 commit 8cf862f

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

functions/bigtable/main_async.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigtable_functions_quickstart_asyncio]
16+
import asyncio
17+
18+
import functions_framework
19+
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery, RowRange
20+
21+
event_loop = asyncio.new_event_loop()
22+
asyncio.set_event_loop(event_loop)
23+
24+
25+
# Setup: create a shared client within the context of the event loop
26+
async def create_client():
27+
# create client in the asyncio event loop context to
28+
# give background tasks a chance to initialize
29+
return BigtableDataClientAsync()
30+
31+
32+
client = event_loop.run_until_complete(create_client())
33+
34+
35+
# Actual cloud functions entrypoint, will delegate to the async one
36+
@functions_framework.http
37+
def bigtable_read_data(request):
38+
return event_loop.run_until_complete(_bigtable_read_data_async(request))
39+
40+
41+
# Actual handler
42+
async def _bigtable_read_data_async(request):
43+
async with client.get_table(
44+
request.headers.get("instance_id"), request.headers.get("table_id")
45+
) as table:
46+
47+
prefix = "phone#"
48+
end_key = prefix[:-1] + chr(ord(prefix[-1]) + 1)
49+
50+
outputs = []
51+
query = ReadRowsQuery(row_ranges=[RowRange(start_key=prefix, end_key=end_key)])
52+
53+
async for row in await table.read_rows_stream(query):
54+
print("%s" % row)
55+
output = "Rowkey: {}, os_build: {}".format(
56+
row.row_key.decode("utf-8"),
57+
row.get_cells("stats_summary", b"os_build")[0].value.decode("utf-8"),
58+
)
59+
outputs.append(output)
60+
61+
return "\n".join(outputs)
62+
63+
64+
# [END bigtable_functions_quickstart_asyncio]

functions/bigtable/main_async_test.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import os
17+
import uuid
18+
19+
from google.cloud import bigtable
20+
import pytest
21+
from requests import Request
22+
23+
import main_async
24+
25+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
26+
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
27+
TABLE_ID_PREFIX = "mobile-time-series-{}"
28+
29+
30+
@pytest.fixture(scope="module", autouse=True)
31+
def table_id():
32+
client = bigtable.Client(project=PROJECT, admin=True)
33+
instance = client.instance(BIGTABLE_INSTANCE)
34+
35+
table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
36+
table = instance.table(table_id)
37+
if table.exists():
38+
table.delete()
39+
40+
table.create(column_families={"stats_summary": None})
41+
42+
timestamp = datetime.datetime(2019, 5, 1)
43+
rows = [
44+
table.direct_row("phone#4c410523#20190501"),
45+
table.direct_row("phone#4c410523#20190502"),
46+
]
47+
48+
rows[0].set_cell("stats_summary", "os_build", "PQ2A.190405.003", timestamp)
49+
rows[1].set_cell("stats_summary", "os_build", "PQ2A.190405.004", timestamp)
50+
51+
table.mutate_rows(rows)
52+
53+
yield table_id
54+
55+
table.delete()
56+
57+
58+
def test_main(table_id):
59+
request = Request(
60+
"GET", headers={"instance_id": BIGTABLE_INSTANCE, "table_id": table_id}
61+
)
62+
63+
response = main_async.bigtable_read_data(request)
64+
65+
assert (
66+
"""Rowkey: phone#4c410523#20190501, os_build: PQ2A.190405.003
67+
Rowkey: phone#4c410523#20190502, os_build: PQ2A.190405.004"""
68+
in response
69+
)

functions/bigtable/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
google-cloud-bigtable==2.19.0
1+
functions-framework==3.7.0
2+
google-cloud-bigtable==2.23.1

0 commit comments

Comments
 (0)
0