Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0a4bd6

Browse files
billyjacobsoncrwilcoxTakashi Matsuo
authored
[bigtable] Cloud function example (GoogleCloudPlatform#3486)
* working function * use system testing instead * Remove additional test data Co-authored-by: Chris Wilcox <[email protected]> Co-authored-by: Takashi Matsuo <[email protected]>
1 parent 8cafafd commit e0a4bd6

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

functions/bigtable/main.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2020 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]
16+
from google.cloud import bigtable
17+
from google.cloud.bigtable.row_set import RowSet
18+
19+
client = bigtable.Client()
20+
21+
22+
def bigtable_read_data(request):
23+
instance = client.instance(request.headers.get("instance_id"))
24+
table = instance.table(request.headers.get("table_id"))
25+
26+
prefix = 'phone#'
27+
end_key = prefix[:-1] + chr(ord(prefix[-1]) + 1)
28+
29+
outputs = []
30+
row_set = RowSet()
31+
row_set.add_row_range_from_keys(prefix.encode("utf-8"),
32+
end_key.encode("utf-8"))
33+
34+
rows = table.read_rows(row_set=row_set)
35+
for row in rows:
36+
output = 'Rowkey: {}, os_build: {}'.format(
37+
row.row_key.decode('utf-8'),
38+
row.cells["stats_summary"]["os_build".encode('utf-8')][0]
39+
.value.decode('utf-8'))
40+
outputs.append(output)
41+
42+
return '\n'.join(outputs)
43+
44+
# [END bigtable_functions_quickstart]

functions/bigtable/main_test.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2020 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+
from requests import Request
21+
import pytest
22+
23+
import main
24+
25+
PROJECT = os.environ['GCLOUD_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('GET', headers={
60+
'instance_id': BIGTABLE_INSTANCE,
61+
'table_id': table_id
62+
})
63+
64+
response = main.bigtable_read_data(request)
65+
66+
assert """Rowkey: phone#4c410523#20190501, os_build: PQ2A.190405.003
67+
Rowkey: phone#4c410523#20190502, os_build: PQ2A.190405.004
68+
""" in response
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.4.1

functions/bigtable/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-bigtable==1.2.1

0 commit comments

Comments
 (0)