|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Demonstrates how to connect to Cloud Bigtable and run some basic operations. |
| 18 | +
|
| 19 | +Prerequisites: |
| 20 | +
|
| 21 | +- Create a Cloud Bigtable cluster. |
| 22 | + https://cloud.google.com/bigtable/docs/creating-cluster |
| 23 | +- Set your Google Application Default Credentials. |
| 24 | + https://developers.google.com/identity/protocols/application-default-credentials |
| 25 | +""" |
| 26 | + |
| 27 | +import argparse |
| 28 | + |
| 29 | +from gcloud import bigtable |
| 30 | + |
| 31 | + |
| 32 | +def main(project_id, cluster_id, zone, table_id): |
| 33 | + # [START connecting_to_bigtable] |
| 34 | + # The client must be created with admin=True because it will create a |
| 35 | + # table. |
| 36 | + with bigtable.Client(project=project_id, admin=True) as client: |
| 37 | + cluster = client.cluster(zone, cluster_id) |
| 38 | + # [END connecting_to_bigtable] |
| 39 | + |
| 40 | + # [START creating_a_table] |
| 41 | + print('Creating the {} table.'.format(table_id)) |
| 42 | + table = cluster.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.encode('utf-8'), |
| 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.encode('utf-8')][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.encode('utf-8')][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 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + parser = argparse.ArgumentParser( |
| 106 | + description=__doc__, |
| 107 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 108 | + parser.add_argument('project_id', help='Your Cloud Platform project ID.') |
| 109 | + parser.add_argument( |
| 110 | + 'cluster', help='ID of the Cloud Bigtable cluster to connect to.') |
| 111 | + parser.add_argument( |
| 112 | + 'zone', help='Zone that contains the Cloud Bigtable cluster.') |
| 113 | + parser.add_argument( |
| 114 | + '--table', |
| 115 | + help='Table to create and destroy.', |
| 116 | + default='Hello-Bigtable') |
| 117 | + |
| 118 | + args = parser.parse_args() |
| 119 | + main(args.project_id, args.cluster, args.zone, args.table) |
0 commit comments