Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigtable: update helloworld example #1670

Merged
merged 2 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions bigtable/hello/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
"""

import argparse
import datetime

from google.cloud import bigtable
from google.cloud.bigtable import column_family
from google.cloud.bigtable import row_filters


def main(project_id, instance_id, table_id):
Expand All @@ -38,23 +41,25 @@ def main(project_id, instance_id, table_id):
# [END connecting_to_bigtable]

# [START creating_a_table]
print('Creating the {} table.'.format(table_id))
print 'Creating the {} table.'.format(table_id)
table = instance.table(table_id)
table.create()
print 'Creating column family cf1 with Max Version GC rule...'
# Create a column family with GC policy : most recent N versions
# Define the GC policy to retain only the most recent 2 versions
max_versions_rule = column_family.MaxVersionsGCRule(2)
column_family_id = 'cf1'
cf1 = table.column_family(column_family_id)
cf1.create()
column_families = {column_family_id: max_versions_rule}
if not table.exists():
table.create(column_families=column_families)
else:
print "Table {} already exists.".format(table_id)
# [END creating_a_table]

# [START writing_rows]
print('Writing some greetings to the table.')
column_id = 'greeting'.encode('utf-8')
greetings = [
'Hello World!',
'Hello Cloud Bigtable!',
'Hello Python!',
]

print 'Writing some greetings to the table.'
greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello Python!']
rows = []
column = 'greeting'
for i, value in enumerate(greetings):
# Note: This example uses sequential numeric IDs for simplicity,
# but this can result in poor performance in a production
Expand All @@ -68,35 +73,35 @@ def main(project_id, instance_id, table_id):
# https://cloud.google.com/bigtable/docs/schema-design
row_key = 'greeting{}'.format(i)
row = table.row(row_key)
row.set_cell(
column_family_id,
column_id,
value.encode('utf-8'))
row.commit()
row.set_cell(column_family_id,
column,
value,
timestamp=datetime.datetime.utcnow())
rows.append(row)
table.mutate_rows(rows)
# [END writing_rows]

# [START getting_a_row]
print('Getting a single greeting by row key.')
print 'Getting a single greeting by row key.'
key = 'greeting0'
row = table.read_row(key.encode('utf-8'))
value = row.cells[column_family_id][column_id][0].value
print('\t{}: {}'.format(key, value.decode('utf-8')))

# Only retrieve the most recent version of the cell.
row_filter = row_filters.CellsColumnLimitFilter(1)
row = table.read_row(key, row_filter)
print row.cell_value(column_family_id, column)
# [END getting_a_row]

# [START scanning_all_rows]
print('Scanning for all greetings:')
partial_rows = table.read_rows()
partial_rows.consume_all()

for row_key, row in partial_rows.rows.items():
key = row_key.decode('utf-8')
cell = row.cells[column_family_id][column_id][0]
value = cell.value.decode('utf-8')
print('\t{}: {}'.format(key, value))
print 'Scanning for all greetings:'
partial_rows = table.read_rows(filter_=row_filter)

for row in partial_rows:
cell = row.cells[column_family_id][column][0]
print cell.value.decode('utf-8')
# [END scanning_all_rows]

# [START deleting_a_table]
print('Deleting the {} table.'.format(table_id))
print 'Deleting the {} table.'.format(table_id)
table.delete()
# [END deleting_a_table]

Expand Down
2 changes: 1 addition & 1 deletion bigtable/hello/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-bigtable==0.29.0
google-cloud-bigtable==0.30.0
google-cloud-core==0.28.1