範例:透過 HappyBase API 執行 Python 適用的「Hello World」應用程式

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

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

執行範例

本範例使用 Python 適用的 Google Cloud 用戶端程式庫 HappyBase 套件 (即 HappyBase API 實作) 來與 Cloud Bigtable 進行通訊。您可使用 HappyBase 套件將現有 HBase 工作負載移往 Cloud Bigtable。若為新應用程式,請參閱使用 Cloud Bigtable 套件的「hello world」範例

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

以 Cloud Bigtable 使用 HappyBase API

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

安裝及匯入用戶端程式庫

您可以使用 PIP 將需要的 Python 套件安裝到 virtualenv 環境中。範例包括需求檔案,定義所需的套件。

google-cloud-happybase==0.33.0

之後模組便可匯入。

from google.cloud import bigtable
from google.cloud import happybase

連線至 Cloud BigTable

bigtable.Client 傳送到 happybase.Connection 即可連線至 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)
connection = happybase.Connection(instance=instance)

建立資料表

使用 Connection.create_table() 建立資料表及其資料欄系列。

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

將列寫入至資料表

使用 Connection.table() 取得現有的 Table,然後使用 Table.put() 將資料列寫入資料表。

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 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.encode('utf-8'): value.encode('utf-8')}
    )

依索引鍵讀取資料列

透過 Table.row() 使用資料列的索引鍵來直接取得資料列。

print('Getting a single greeting by row key.')
key = 'greeting0'.encode('utf-8')
row = table.row(key)
print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

掃描所有資料表列

使用 Table.scan() 取得特定範圍的資料列。

print('Scanning for all greetings:')

for key, row in table.scan():
    print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

刪除資料表

使用 Connection.delete_table() 刪除資料表。

print('Deleting the {} table.'.format(table_name))
connection.delete_table(table_name)

完整的程式碼範例

以下是沒有註解的完整範例。


"""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
from google.cloud import bigtable
from google.cloud import happybase

def main(project_id, instance_id, table_name):
    client = bigtable.Client(project=project_id, admin=True)
    instance = client.instance(instance_id)
    connection = happybase.Connection(instance=instance)

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

        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 i, value in enumerate(greetings):
            row_key = 'greeting{}'.format(i)
            table.put(
                row_key, {column_name.encode('utf-8'): value.encode('utf-8')}
            )

        print('Getting a single greeting by row key.')
        key = 'greeting0'.encode('utf-8')
        row = table.row(key)
        print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

        print('Scanning for all greetings:')

        for key, row in table.scan():
            print('\t{}: {}'.format(key, row[column_name.encode('utf-8')]))

        print('Deleting the {} table.'.format(table_name))
        connection.delete_table(table_name)

    finally:
        connection.close()

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 說明文件