PHP hello world
這個程式碼範例是在 PHP 上執行的「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。
執行範例
本程式碼範例使用 PHP 適用的 Google Cloud 用戶端程式庫的 Cloud Bigtable PHP 用戶端程式庫套件,與 Bigtable 進行通訊。
如要執行這個範例程式,請按照 GitHub 網頁上的說明操作。
搭配 Cloud 用戶端程式庫使用 Bigtable
這個應用程式範例會連線至 Bigtable,示範部分基本作業。
須使用用戶端程式庫
這個範例會使用 ApiCore 的 ApiException 類別,以及 Bigtable 的 PHP 用戶端中的多個類別。
連線至 Bigtable
使用有效的Google Cloud 專案 ID、Bigtable 執行個體 ID 和資料表 ID,在應用程式中建立要使用的變數。然後例項化新的 BigtableInstanceAdminClient、BigtableTableAdminClient 和 BigtableClient 物件,用於連線至 Bigtable。
建立資料表
檢查資料表是否已存在。如果沒有,請呼叫 createtable()
方法來建立 Table
物件。資料表有單一資料欄系列,會保留每個資料欄值的一個版本。
將資料列寫入資料表
接著,使用問候語字串陣列為資料表建立幾個新的資料列。針對每則問候語,建立新的 Mutations
物件,並使用 upsert()
將其新增至 entries
。然後使用資料表的 mutateRows()
方法,將項目寫入資料表。
使用篩選器讀取資料列
讀取已寫入的資料前,請先建立篩選器,藉此限制 Bigtable 傳回的資料。即使資料表中含有每個值未經過垃圾收集處理的較舊版本,篩選器仍能指示 Bigtable 僅傳回每個值的最新版本。
建立資料列物件,然後呼叫 readRow()
方法並傳入篩選器,即可取得該資料列中每個資料欄的一個版本。
掃描所有資料表資料列
呼叫 readRows()
方法並傳入篩選器,即可取得資料表中的所有資料列。您已傳入篩選器,因此 Bigtable 只會傳回每個值的一個版本。
刪除資料表
使用管理用戶端的 deleteTable()
方法刪除資料表。
全面整合使用
以下是不含評論的完整程式碼範例。