Skip to content

Commit ec07d37

Browse files
committed
Add region tags to bigtable/hello sample.
Also, change the sample to use sequential keys (with a disclaimer) to match the Java sample. I had forgotten to add a sample usage to get a specific row, so add that, too.
1 parent e934507 commit ec07d37

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

bigtable/hello/main.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
"""
2828

2929
import argparse
30-
import uuid
3130

3231
from gcloud import bigtable
3332
from gcloud.bigtable import happybase
3433

3534

3635
def main(project, cluster_id, zone, table_name):
36+
# [START connecting_to_bigtable]
37+
# project = "my-project-id"
38+
# cluster_id = "my-cluster"
39+
# zone = "your-zone-id"
40+
3741
# The client must be created with admin=True because it will create a
3842
# table.
3943
client = bigtable.Client(project=project, admin=True)
@@ -42,35 +46,60 @@ def main(project, cluster_id, zone, table_name):
4246
cluster = client.cluster(zone, cluster_id)
4347
cluster.reload()
4448
connection = happybase.Connection(cluster=cluster)
49+
# [END connecting_to_bigtable]
4550

51+
# [START creating_a_table]
52+
# table_name = 'Hello-Bigtable'
4653
print('Creating the {} table.'.format(table_name))
4754
column_family_name = 'cf1'
4855
connection.create_table(
4956
table_name,
5057
{
5158
column_family_name: dict() # Use default options.
5259
})
53-
table = connection.table(table_name)
60+
# [END creating_a_table]
5461

62+
# [START writing_rows]
5563
print('Writing some greetings to the table.')
64+
table = connection.table(table_name)
5665
column_name = '{fam}:greeting'.format(fam=column_family_name)
5766
greetings = [
5867
'Hello World!',
5968
'Hello Cloud Bigtable!',
6069
'Hello HappyBase!',
6170
]
62-
for value in greetings:
63-
# Use a random key to distribute writes more evenly across shards.
64-
# See: https://cloud.google.com/bigtable/docs/schema-design
65-
row_key = str(uuid.uuid4())
71+
for i, value in enumerate(greetings):
72+
# Note: This example uses sequential numeric IDs for simplicity,
73+
# but this can result in poor performance in a production
74+
# application. Since rows are stored in sorted order by key,
75+
# sequential keys can result in poor distribution of operations
76+
# across nodes.
77+
#
78+
# For more information about how to design a Bigtable schema for
79+
# the best performance, see the documentation:
80+
#
81+
# https://cloud.google.com/bigtable/docs/schema-design
82+
row_key = 'greeting{}'.format(i)
6683
table.put(row_key, {column_name: value})
84+
# [END writing_rows]
85+
86+
# [START getting_a_row]
87+
print('Getting a single greeting by row key.')
88+
key = 'greeting0'
89+
row = table.row(key)
90+
print('\t{}: {}'.format(key, row[column_name]))
91+
# [END getting_a_row]
6792

93+
# [START scanning_all_rows]
6894
print('Scanning for all greetings:')
6995
for key, row in table.scan():
7096
print('\t{}: {}'.format(key, row[column_name]))
97+
# [END scanning_all_rows]
7198

99+
# [START deleting_a_table]
72100
print('Deleting the {} table.'.format(table_name))
73101
connection.delete_table(table_name)
102+
# [END deleting_a_table]
74103

75104

76105
if __name__ == '__main__':

bigtable/hello/main_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import re
1717
import sys
1818

19-
from hello import main
19+
from main import main
2020

2121
import pytest
2222

@@ -41,7 +41,9 @@ def test_main(cloud_config, capsys):
4141
assert re.search(
4242
re.compile(r'Creating the Hello-Bigtable-[0-9]+ table\.'), out)
4343
assert re.search(re.compile(r'Writing some greetings to the table\.'), out)
44-
assert re.search(re.compile(r'Scanning for all greetings'), out)
44+
assert re.search(re.compile(r'Getting a single greeting by row key.'), out)
4545
assert re.search(re.compile(r'greeting0: Hello World!'), out)
46+
assert re.search(re.compile(r'Scanning for all greetings'), out)
47+
assert re.search(re.compile(r'greeting1: Hello Cloud Bigtable!'), out)
4648
assert re.search(
4749
re.compile(r'Deleting the Hello-Bigtable-[0-9]+ table\.'), out)

0 commit comments

Comments
 (0)