2727"""
2828
2929import argparse
30- import uuid
3130
3231from gcloud import bigtable
3332from 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
76100if __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 )
0 commit comments