HappyBase API hello world
本範例是以 Python 編寫的簡易「hello world」應用程式,其說明如何:
- 設定驗證方法
- 連線至 Bigtable 執行個體。
- 建立新的資料表。
- 將資料寫入資料表。
- 讀取資料。
- 刪除資料表。
設定驗證方法
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
執行範例
本範例使用 Python 適用的 Google Cloud 用戶端程式庫的 HappyBase 套件 (即 HappyBase API 實作),與 Bigtable 進行通訊。如要將現有的 HBase 工作負載移至 Bigtable,請使用 HappyBase 套件。如要瞭解新應用程式,請參閱使用 Bigtable 套件的「hello world」範例。
如要執行這個範例程式,請依照 GitHub 網頁上的說明操作。
搭配 Bigtable 使用 HappyBase API
這個應用程式範例會連線至 Bigtable,示範部分簡易作業。
安裝及匯入用戶端程式庫
您可以使用 PIP 將需要的 Python 套件安裝到 virtualenv 環境中。範例包含了一個定義必要套件的需求檔案。
之後模組便可匯入。
連線至 Bigtable
將 bigtable.Client
傳遞至 happybase.Connection
,即可連線至 Bigtable。
建立資料表
使用 Connection.create_table()
建立資料表及其資料欄系列。
將資料列寫入資料表
使用 Connection.table()
取得現有的 Table
。然後使用 Table.put()
將資料列寫入資料表。
依索引鍵讀取資料列
透過 Table.row()
使用資料列的索引鍵來直接取得資料列。
掃描所有資料表資料列
使用 Table.scan()
取得特定範圍的資料列。
刪除資料表
使用 Connection.delete_table()
刪除資料表。
全面整合使用
以下是沒有註解的完整範例。