範例:Python「Hello World」應用程式

本範例是以 Python 編寫的簡易「hello world」應用程式,其說明如何:

  • 連線至 Cloud Bigtable 執行個體。
  • 建立新的資料表。
  • 將資料寫入資料表。
  • 讀取資料。
  • 刪除資料表。

執行範例

本範例使用 Python 適用的 Google Cloud 用戶端程式庫Cloud Bigtable 套件,與 Cloud Bigtable 進行通訊。Cloud Bigtable 套件是新應用程式的最佳選擇。若您需要將現有 HBase 工作負載移往 Cloud Bigtable,請參閱使用 HappyBase 套件的「hello world」範例

執行此範例程式時,請遵循 GitHub 範例操作說明

搭配 Cloud 用戶端程式庫使用 Cloud Bigtable

範例應用程式會連線至 Cloud Bigtable,示範部分簡易作業。

安裝及匯入用戶端程式庫

使用 PIP 將需要的 Python 套件安裝到 virtualenv 環境中。範例包含了一個定義必要套件的需求檔案

google-cloud-bigtable==0.32.1
google-cloud-core==0.29.1

匯入模組。

import datetime

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

連線至 Cloud BigTable

使用 bigtable.Client 連線至 Cloud Bigtable。

# The client must be created with admin=True because it will create a
# table.
client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)

建立資料表

使用 Instance.table() 將資料表物件實例化。建立資料欄系列並設定其垃圾收集政策,然後將資料欄系列傳送到 Table.create() 以建立資料表。

print('Creating the {} table.'.format(table_id))
table = instance.table(table_id)

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'
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))

將資料列寫入資料表

循環處理問候語字串清單,以在資料表中建立一些新的資料列。在每個疊代作業中,使用 Table.row() 定義資料列並為其指派資料列索引鍵、呼叫 Row.set_cell() 以設定目前儲存格的值,並將新的資料列附加到資料列陣列。最後呼叫 Table.mutate_rows(),將資料列新增到資料表。

print('Writing some greetings to the table.')
greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello Python!']
rows = []
column = 'greeting'.encode()
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).encode()
    row = table.row(row_key)
    row.set_cell(column_family_id,
                 column,
                 value,
                 timestamp=datetime.datetime.utcnow())
    rows.append(row)
table.mutate_rows(rows)

建立篩選器

讀取已寫入的資料前,請先使用 row_filters.CellsColumnLimitFilter() 建立篩選器,藉此限制 Cloud Bigtable 傳回的資料。即使資料表中含有每個值未經過垃圾收集處理的較舊版本,篩選器仍能指示 Cloud BigTable 僅傳回每個值的最新版本。

row_filter = row_filters.CellsColumnLimitFilter(1)

依鍵值讀取資料列

請呼叫資料表的 [Table.read_row()] api-bigtable-table-read-row 方法,透過特定資料列索引鍵取得資料列的參考資料並傳入索引鍵和篩選器,即可取得該資料列中每個資料值的一個版本。

print('Getting a single greeting by row key.')
key = 'greeting0'.encode()

row = table.read_row(key, row_filter)
cell = row.cells[column_family_id][column][0]
print(cell.value.decode('utf-8'))

掃描所有資料表資料列

使用 Table.read_rows() 讀取資料表中特定範圍的資料列。

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'))

刪除資料表

使用 Table.delete() 刪除資料表。

print('Deleting the {} table.'.format(table_id))
table.delete()

完整的程式碼範例

以下是不含評論的完整範例。



"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.

Prerequisites:

- Create a Cloud Bigtable cluster.
  https://cloud.google.com/bigtable/docs/creating-cluster
- Set your Google Application Default Credentials.
  https://developers.google.com/identity/protocols/application-default-credentials
"""

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):
    client = bigtable.Client(project=project_id, admin=True)
    instance = client.instance(instance_id)

    print('Creating the {} table.'.format(table_id))
    table = instance.table(table_id)

    print('Creating column family cf1 with Max Version GC rule...')
    max_versions_rule = column_family.MaxVersionsGCRule(2)
    column_family_id = 'cf1'
    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))

    print('Writing some greetings to the table.')
    greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello Python!']
    rows = []
    column = 'greeting'.encode()
    for i, value in enumerate(greetings):
        row_key = 'greeting{}'.format(i).encode()
        row = table.row(row_key)
        row.set_cell(column_family_id,
                     column,
                     value,
                     timestamp=datetime.datetime.utcnow())
        rows.append(row)
    table.mutate_rows(rows)

    row_filter = row_filters.CellsColumnLimitFilter(1)

    print('Getting a single greeting by row key.')
    key = 'greeting0'.encode()

    row = table.read_row(key, row_filter)
    cell = row.cells[column_family_id][column][0]
    print(cell.value.decode('utf-8'))

    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'))

    print('Deleting the {} table.'.format(table_id))
    table.delete()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('project_id', help='Your Cloud Platform project ID.')
    parser.add_argument(
        'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
    parser.add_argument(
        '--table',
        help='Table to create and destroy.',
        default='Hello-Bigtable')

    args = parser.parse_args()
    main(args.project_id, args.instance_id, args.table)
本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Bigtable 說明文件