Java hello world 的 HBase API
這個範例是一個「hello world」應用程式,使用 Java 適用的 Bigtable HBase 用戶端程式庫,為您說明如何執行以下作業:
- 設定驗證方法
- 連線至 Bigtable 執行個體。
- 建立新的資料表。
- 將資料寫入資料表。
- 讀取資料。
- 刪除資料表。
設定驗證方法
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 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。
執行範例
本範例使用 HBase API 與 Bigtable 通訊。您可以在 GitHub 存放區 GoogleCloudPlatform/cloud-bigtable-examples 的 java/hello-world
目錄中找到本範例的程式碼。
如要執行這個範例程式,請依照 GitHub 網頁上的說明操作。
使用 HBase API
這個應用程式範例會連線至 Bigtable,示範部分簡易作業。
安裝及匯入用戶端程式庫
本範例使用 Java 適用的 Bigtable HBase 用戶端和 Maven。請參閱使用用戶端程式庫的操作說明。
下面列出了這個範例所匯入的程式庫:
連線至 Bigtable
使用 BigtableConfiguration
類別連線至 Bigtable。
建立資料表
使用 Admin API 建立資料表。
將資料列寫入資料表
使用 Table
類別將資料列放入資料表。如要提升總處理量,建議使用 BigtableBufferedMutator
類別。
依索引鍵讀取資料列
直接使用資料列的索引鍵來擷取資料列。
掃描所有資料表資料列
使用 Scan
類別擷取特定範圍的資料列。
刪除資料表
使用 Admin API 刪除資料表。
全面整合使用
以下是沒有註解的完整範例。