C++ hello world
本範例是以 C++ 編寫的簡易「hello world」應用程式,說明如何執行以下操作:
- 設定驗證方法
- 連線至 Bigtable 執行個體。
- 建立新的資料表。
- 將資料寫入資料表。
- 讀取資料。
- 刪除資料表。
設定驗證方法
如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 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。
執行範例
本範例使用 Google Cloud C++ 用戶端程式庫的 Cloud Bigtable 套件,與 Bigtable 進行通訊。
如要執行這個範例程式,請按照 GitHub 上的操作說明進行。
搭配 Bigtable 使用 Google Cloud 用戶端程式庫
這個應用程式範例會連線至 Bigtable,示範部分簡易作業。
安裝及匯入用戶端程式庫
從 GitHub 下載或複製 Bigtable C++ 用戶端程式庫,然後進行編譯。按照頂層 README 上的編譯器操作說明進行操作。
加入必要的標頭。
連線至 Bigtable
使用 MakeBigtableTableAdminConnection()
建構 BigtableTableAdminClient
,用於建立資料表。
建立資料表
為具有一個資料欄系列的資料表定義結構定義,並為該資料欄系列設定垃圾收集規則,讓每個值最多只有一個版本。利用該結構定義使用 BigtableTableAdminClient::CreateTable()
將資料表物件例項化。然後建立一個資料用戶端,以便將資料輸入和輸出資料表。
將資料列寫入資料表
循環處理問候語字串清單,以在資料表中建立一些新的資料列。
在每個疊代作業中,使用 SingleRowMutation
定義資料列,並為其指派資料列索引鍵和值。然後呼叫 Table::Apply()
,將變異套用到該資料列。
建立篩選器
讀取已寫入的資料前,請先使用 Filter::ColumnRangeClosed()
建立篩選器,藉此限制 Bigtable 傳回的資料。即使資料表中含有已過期但尚未透過垃圾收集程序移除的舊儲存格,篩選器仍能指示 Bigtable 僅傳回每個值的最新版本。
依索引鍵讀取資料列
呼叫 Table::ReadRow()
函式並傳入資料列索引鍵和篩選器,即可取得該資料列中每個資料值的一個版本。
掃描所有資料表資料列
使用 Table::ReadRows()
讀取資料表中特定範圍的資料列。
刪除資料表
使用 BigtableTableAdminClient::DeleteTable()
刪除資料表。
全面整合使用
以下是沒有註解的完整範例。