27
27
"""
28
28
29
29
import argparse
30
- import uuid
31
30
32
31
from gcloud import bigtable
33
32
from gcloud .bigtable import happybase
34
33
35
34
36
- def main (project , cluster_id , zone , table_name ):
35
+ def main (project_id , cluster_id , zone , table_name ):
36
+ # [START connecting_to_bigtable]
37
37
# The client must be created with admin=True because it will create a
38
38
# table.
39
- client = bigtable .Client (project = project , admin = True )
39
+ client = bigtable .Client (project = project_id , admin = True )
40
40
41
41
with client :
42
42
cluster = client .cluster (zone , cluster_id )
43
43
cluster .reload ()
44
44
connection = happybase .Connection (cluster = cluster )
45
+ # [END connecting_to_bigtable]
45
46
47
+ # [START creating_a_table]
46
48
print ('Creating the {} table.' .format (table_name ))
47
49
column_family_name = 'cf1'
48
50
connection .create_table (
49
51
table_name ,
50
52
{
51
53
column_family_name : dict () # Use default options.
52
54
})
53
- table = connection . table ( table_name )
55
+ # [END creating_a_table]
54
56
57
+ # [START writing_rows]
55
58
print ('Writing some greetings to the table.' )
59
+ table = connection .table (table_name )
56
60
column_name = '{fam}:greeting' .format (fam = column_family_name )
57
61
greetings = [
58
62
'Hello World!' ,
59
63
'Hello Cloud Bigtable!' ,
60
64
'Hello HappyBase!' ,
61
65
]
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 )
66
78
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]
67
87
88
+ # [START scanning_all_rows]
68
89
print ('Scanning for all greetings:' )
69
90
for key , row in table .scan ():
70
91
print ('\t {}: {}' .format (key , row [column_name ]))
92
+ # [END scanning_all_rows]
71
93
94
+ # [START deleting_a_table]
72
95
print ('Deleting the {} table.' .format (table_name ))
73
96
connection .delete_table (table_name )
97
+ # [END deleting_a_table]
74
98
75
99
76
100
if __name__ == '__main__' :
@@ -79,7 +103,7 @@ def main(project, cluster_id, zone, table_name):
79
103
' Bigtable.' ,
80
104
formatter_class = argparse .ArgumentDefaultsHelpFormatter )
81
105
parser .add_argument (
82
- 'project ' ,
106
+ 'project_id ' ,
83
107
help = 'Google Cloud Platform project ID that contains the Cloud' +
84
108
' Bigtable cluster.' )
85
109
parser .add_argument (
@@ -92,4 +116,4 @@ def main(project, cluster_id, zone, table_name):
92
116
default = 'Hello-Bigtable' )
93
117
94
118
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