FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add region tags to bigtable/hello sample. by tswast · Pull Request #376 · GoogleCloudPlatform/python-docs-samples · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
44 changes: 34 additions & 10 deletions bigtable/hello/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,74 @@
"""

import argparse
import uuid

from gcloud import bigtable
from gcloud.bigtable import happybase


def main(project, cluster_id, zone, table_name):
def main(project_id, cluster_id, zone, table_name):
# [START connecting_to_bigtable]
# The client must be created with admin=True because it will create a
# table.
client = bigtable.Client(project=project, admin=True)
client = bigtable.Client(project=project_id, admin=True)

with client:
cluster = client.cluster(zone, cluster_id)
cluster.reload()
connection = happybase.Connection(cluster=cluster)
# [END connecting_to_bigtable]

# [START creating_a_table]
print('Creating the {} table.'.format(table_name))
column_family_name = 'cf1'
connection.create_table(
table_name,
{
column_family_name: dict() # Use default options.
})
table = connection.table(table_name)
# [END creating_a_table]

# [START writing_rows]
print('Writing some greetings to the table.')
table = connection.table(table_name)
column_name = '{fam}:greeting'.format(fam=column_family_name)
greetings = [
'Hello World!',
'Hello Cloud Bigtable!',
'Hello HappyBase!',
]
for value in greetings:
# Use a random key to distribute writes more evenly across shards.
# See: https://cloud.google.com/bigtable/docs/schema-design
row_key = str(uuid.uuid4())
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
# application. Since rows are stored in sorted order by key,
# sequential keys can result in poor distribution of operations
# across nodes.
#
# For more information about how to design a Bigtable schema for
# the best performance, see the documentation:
#
# https://cloud.google.com/bigtable/docs/schema-design
row_key = 'greeting{}'.format(i)
table.put(row_key, {column_name: value})
# [END writing_rows]

# [START getting_a_row]
print('Getting a single greeting by row key.')
key = 'greeting0'
row = table.row(key)
print('\t{}: {}'.format(key, row[column_name]))
# [END getting_a_row]

# [START scanning_all_rows]
print('Scanning for all greetings:')
for key, row in table.scan():
print('\t{}: {}'.format(key, row[column_name]))
# [END scanning_all_rows]

# [START deleting_a_table]
print('Deleting the {} table.'.format(table_name))
connection.delete_table(table_name)
# [END deleting_a_table]


if __name__ == '__main__':
Expand All @@ -79,7 +103,7 @@ def main(project, cluster_id, zone, table_name):
' Bigtable.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'project',
'project_id',
help='Google Cloud Platform project ID that contains the Cloud' +
' Bigtable cluster.')
parser.add_argument(
Expand All @@ -92,4 +116,4 @@ def main(project, cluster_id, zone, table_name):
default='Hello-Bigtable')

args = parser.parse_args()
main(args.project, args.cluster, args.zone, args.table)
main(args.project_id, args.cluster, args.zone, args.table)
6 changes: 4 additions & 2 deletions bigtable/hello/main_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re
import sys

from hello import main
from main import main

import pytest

Expand All @@ -41,7 +41,9 @@ def test_main(cloud_config, capsys):
assert re.search(
re.compile(r'Creating the Hello-Bigtable-[0-9]+ table\.'), out)
assert re.search(re.compile(r'Writing some greetings to the table\.'), out)
assert re.search(re.compile(r'Scanning for all greetings'), out)
assert re.search(re.compile(r'Getting a single greeting by row key.'), out)
assert re.search(re.compile(r'greeting0: Hello World!'), out)
assert re.search(re.compile(r'Scanning for all greetings'), out)
assert re.search(re.compile(r'greeting1: Hello Cloud Bigtable!'), out)
assert re.search(
re.compile(r'Deleting the Hello-Bigtable-[0-9]+ table\.'), out)

Back | FazBrowse Home | New Git URL