Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d994b91

Browse filesBrowse files
authored
Merge pull request GoogleCloudPlatform#376 from GoogleCloudPlatform/tswast-bigtable
Add region tags to bigtable/hello sample.
2 parents e934507 + 962352d commit d994b91
Copy full SHA for d994b91

File tree

Expand file treeCollapse file tree

2 files changed

+38
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+38
-12
lines changed

‎bigtable/hello/main.py

Copy file name to clipboardExpand all lines: bigtable/hello/main.py
+34-10Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,74 @@
2727
"""
2828

2929
import argparse
30-
import uuid
3130

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

3534

36-
def main(project, cluster_id, zone, table_name):
35+
def main(project_id, cluster_id, zone, table_name):
36+
# [START connecting_to_bigtable]
3737
# The client must be created with admin=True because it will create a
3838
# table.
39-
client = bigtable.Client(project=project, admin=True)
39+
client = bigtable.Client(project=project_id, admin=True)
4040

4141
with client:
4242
cluster = client.cluster(zone, cluster_id)
4343
cluster.reload()
4444
connection = happybase.Connection(cluster=cluster)
45+
# [END connecting_to_bigtable]
4546

47+
# [START creating_a_table]
4648
print('Creating the {} table.'.format(table_name))
4749
column_family_name = 'cf1'
4850
connection.create_table(
4951
table_name,
5052
{
5153
column_family_name: dict() # Use default options.
5254
})
53-
table = connection.table(table_name)
55+
# [END creating_a_table]
5456

57+
# [START writing_rows]
5558
print('Writing some greetings to the table.')
59+
table = connection.table(table_name)
5660
column_name = '{fam}:greeting'.format(fam=column_family_name)
5761
greetings = [
5862
'Hello World!',
5963
'Hello Cloud Bigtable!',
6064
'Hello HappyBase!',
6165
]
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())
66+
for i, value in enumerate(greetings):
67+
# Note: This example uses sequential numeric IDs for simplicity,
68+
# but this can result in poor performance in a production
69+
# application. Since rows are stored in sorted order by key,
70+
# sequential keys can result in poor distribution of operations
71+
# across nodes.
72+
#
73+
# For more information about how to design a Bigtable schema for
74+
# the best performance, see the documentation:
75+
#
76+
# https://cloud.google.com/bigtable/docs/schema-design
77+
row_key = 'greeting{}'.format(i)
6678
table.put(row_key, {column_name: value})
79+
# [END writing_rows]
80+
81+
# [START getting_a_row]
82+
print('Getting a single greeting by row key.')
83+
key = 'greeting0'
84+
row = table.row(key)
85+
print('\t{}: {}'.format(key, row[column_name]))
86+
# [END getting_a_row]
6787

88+
# [START scanning_all_rows]
6889
print('Scanning for all greetings:')
6990
for key, row in table.scan():
7091
print('\t{}: {}'.format(key, row[column_name]))
92+
# [END scanning_all_rows]
7193

94+
# [START deleting_a_table]
7295
print('Deleting the {} table.'.format(table_name))
7396
connection.delete_table(table_name)
97+
# [END deleting_a_table]
7498

7599

76100
if __name__ == '__main__':
@@ -79,7 +103,7 @@ def main(project, cluster_id, zone, table_name):
79103
' Bigtable.',
80104
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
81105
parser.add_argument(
82-
'project',
106+
'project_id',
83107
help='Google Cloud Platform project ID that contains the Cloud' +
84108
' Bigtable cluster.')
85109
parser.add_argument(
@@ -92,4 +116,4 @@ def main(project, cluster_id, zone, table_name):
92116
default='Hello-Bigtable')
93117

94118
args = parser.parse_args()
95-
main(args.project, args.cluster, args.zone, args.table)
119+
main(args.project_id, args.cluster, args.zone, args.table)

‎bigtable/hello/main_test.py

Copy file name to clipboardExpand all lines: bigtable/hello/main_test.py
+4-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.