2727"""
2828
2929import argparse
30- import uuid
3130
3231from gcloud import bigtable
3332from gcloud .bigtable import happybase
3433
3534
3635def 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
76105if __name__ == '__main__' :
0 commit comments