Go hello world
這個範例是一個非常簡單的「hello world」應用程式 (以 Go 語言撰寫),可以協助您瞭解如何進行下列各項操作:
- 設定驗證方法
- 連線至 Bigtable 執行個體。
- 建立新的資料表。
- 將資料寫入資料表。
- 讀取資料。
- 刪除資料表。
設定驗證方法
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 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。
執行範例
本範例使用 Go 適用的 Google Cloud 用戶端程式庫的 Cloud Bigtable 套件,與 Bigtable 進行通訊。
如要執行這個範例程式,請依照 GitHub 網頁上的說明操作。
搭配 Cloud 用戶端程式庫使用 Bigtable
這個應用程式範例會連線至 Bigtable,示範部分簡易作業。
匯入用戶端程式庫
本範例使用下列匯入功能:
連線至 Bigtable 以管理資料表
如要管理資料表,請使用 bigtable.NewAdminClient()
連線至 Bigtable。
建立資料表
使用 AdminClient.CreateTable()
建立資料表,然後使用 AdminClient.TableInfo()
取得資料表相關資訊。
使用 AdminClient.CreateColumnFamily()
建立資料欄系列。
連線至 Bigtable 以管理資料
如要管理資料,請使用 bigtable.NewClient()
連線至 Bigtable。
將資料列寫入資料表
開啟您想要寫入的資料表。使用 bigtable.NewMutation()
在單一資料列上建立突變,然後使用 Mutation.Set()
在資料列中設定值。為每一個資料列產生一個不同的列鍵值。重複這些步驟,以建立多筆變異。最後,使用 Table.ApplyBulk()
將所有變異套用到資料表。
依索引鍵讀取資料列
透過 Table.ReadRow()
使用資料列的索引鍵來直接取得資料列。
掃描所有資料表資料列
使用 Table.ReadRows()
掃描資料表中的所有資料列。
使用完畢後,請關閉資料用戶端。
刪除資料表
使用 AdminClient.DeleteTable()
刪除資料表。使用完畢後,請關閉管理客戶端。
全面整合使用
以下是沒有註解的完整範例。