|
26 | 26 |
|
27 | 27 | import argparse
|
28 | 28 |
|
29 |
| -from gcloud import bigtable |
| 29 | +from google.cloud import bigtable |
30 | 30 |
|
31 | 31 |
|
32 | 32 | def main(project_id, instance_id, table_id):
|
33 | 33 | # [START connecting_to_bigtable]
|
34 | 34 | # The client must be created with admin=True because it will create a
|
35 | 35 | # table.
|
36 |
| - with bigtable.Client(project=project_id, admin=True) as client: |
37 |
| - instance = client.instance(instance_id) |
38 |
| - # [END connecting_to_bigtable] |
39 |
| - |
40 |
| - # [START creating_a_table] |
41 |
| - print('Creating the {} table.'.format(table_id)) |
42 |
| - table = instance.table(table_id) |
43 |
| - table.create() |
44 |
| - column_family_id = 'cf1' |
45 |
| - cf1 = table.column_family(column_family_id) |
46 |
| - cf1.create() |
47 |
| - # [END creating_a_table] |
48 |
| - |
49 |
| - # [START writing_rows] |
50 |
| - print('Writing some greetings to the table.') |
51 |
| - column_id = 'greeting'.encode('utf-8') |
52 |
| - greetings = [ |
53 |
| - 'Hello World!', |
54 |
| - 'Hello Cloud Bigtable!', |
55 |
| - 'Hello Python!', |
56 |
| - ] |
57 |
| - |
58 |
| - for i, value in enumerate(greetings): |
59 |
| - # Note: This example uses sequential numeric IDs for simplicity, |
60 |
| - # but this can result in poor performance in a production |
61 |
| - # application. Since rows are stored in sorted order by key, |
62 |
| - # sequential keys can result in poor distribution of operations |
63 |
| - # across nodes. |
64 |
| - # |
65 |
| - # For more information about how to design a Bigtable schema for |
66 |
| - # the best performance, see the documentation: |
67 |
| - # |
68 |
| - # https://cloud.google.com/bigtable/docs/schema-design |
69 |
| - row_key = 'greeting{}'.format(i) |
70 |
| - row = table.row(row_key) |
71 |
| - row.set_cell( |
72 |
| - column_family_id, |
73 |
| - column_id, |
74 |
| - value.encode('utf-8')) |
75 |
| - row.commit() |
76 |
| - # [END writing_rows] |
77 |
| - |
78 |
| - # [START getting_a_row] |
79 |
| - print('Getting a single greeting by row key.') |
80 |
| - key = 'greeting0' |
81 |
| - row = table.read_row(key.encode('utf-8')) |
82 |
| - value = row.cells[column_family_id][column_id][0].value |
83 |
| - print('\t{}: {}'.format(key, value.decode('utf-8'))) |
84 |
| - # [END getting_a_row] |
85 |
| - |
86 |
| - # [START scanning_all_rows] |
87 |
| - print('Scanning for all greetings:') |
88 |
| - partial_rows = table.read_rows() |
89 |
| - partial_rows.consume_all() |
90 |
| - |
91 |
| - for row_key, row in partial_rows.rows.items(): |
92 |
| - key = row_key.decode('utf-8') |
93 |
| - cell = row.cells[column_family_id][column_id][0] |
94 |
| - value = cell.value.decode('utf-8') |
95 |
| - print('\t{}: {}'.format(key, value)) |
96 |
| - # [END scanning_all_rows] |
97 |
| - |
98 |
| - # [START deleting_a_table] |
99 |
| - print('Deleting the {} table.'.format(table_id)) |
100 |
| - table.delete() |
101 |
| - # [END deleting_a_table] |
| 36 | + client = bigtable.Client(project=project_id, admin=True) |
| 37 | + instance = client.instance(instance_id) |
| 38 | + # [END connecting_to_bigtable] |
| 39 | + |
| 40 | + # [START creating_a_table] |
| 41 | + print('Creating the {} table.'.format(table_id)) |
| 42 | + table = instance.table(table_id) |
| 43 | + table.create() |
| 44 | + column_family_id = 'cf1' |
| 45 | + cf1 = table.column_family(column_family_id) |
| 46 | + cf1.create() |
| 47 | + # [END creating_a_table] |
| 48 | + |
| 49 | + # [START writing_rows] |
| 50 | + print('Writing some greetings to the table.') |
| 51 | + column_id = 'greeting'.encode('utf-8') |
| 52 | + greetings = [ |
| 53 | + 'Hello World!', |
| 54 | + 'Hello Cloud Bigtable!', |
| 55 | + 'Hello Python!', |
| 56 | + ] |
| 57 | + |
| 58 | + for i, value in enumerate(greetings): |
| 59 | + # Note: This example uses sequential numeric IDs for simplicity, |
| 60 | + # but this can result in poor performance in a production |
| 61 | + # application. Since rows are stored in sorted order by key, |
| 62 | + # sequential keys can result in poor distribution of operations |
| 63 | + # across nodes. |
| 64 | + # |
| 65 | + # For more information about how to design a Bigtable schema for |
| 66 | + # the best performance, see the documentation: |
| 67 | + # |
| 68 | + # https://cloud.google.com/bigtable/docs/schema-design |
| 69 | + row_key = 'greeting{}'.format(i) |
| 70 | + row = table.row(row_key) |
| 71 | + row.set_cell( |
| 72 | + column_family_id, |
| 73 | + column_id, |
| 74 | + value.encode('utf-8')) |
| 75 | + row.commit() |
| 76 | + # [END writing_rows] |
| 77 | + |
| 78 | + # [START getting_a_row] |
| 79 | + print('Getting a single greeting by row key.') |
| 80 | + key = 'greeting0' |
| 81 | + row = table.read_row(key.encode('utf-8')) |
| 82 | + value = row.cells[column_family_id][column_id][0].value |
| 83 | + print('\t{}: {}'.format(key, value.decode('utf-8'))) |
| 84 | + # [END getting_a_row] |
| 85 | + |
| 86 | + # [START scanning_all_rows] |
| 87 | + print('Scanning for all greetings:') |
| 88 | + partial_rows = table.read_rows() |
| 89 | + partial_rows.consume_all() |
| 90 | + |
| 91 | + for row_key, row in partial_rows.rows.items(): |
| 92 | + key = row_key.decode('utf-8') |
| 93 | + cell = row.cells[column_family_id][column_id][0] |
| 94 | + value = cell.value.decode('utf-8') |
| 95 | + print('\t{}: {}'.format(key, value)) |
| 96 | + # [END scanning_all_rows] |
| 97 | + |
| 98 | + # [START deleting_a_table] |
| 99 | + print('Deleting the {} table.'.format(table_id)) |
| 100 | + table.delete() |
| 101 | + # [END deleting_a_table] |
102 | 102 |
|
103 | 103 |
|
104 | 104 | if __name__ == '__main__':
|
|
0 commit comments