使用 cbt CLI 建立執行個體並寫入資料
如果您正在學習 Bigtable,可以完成快速入門導覽,瞭解在實際環境中大規模使用的基本概念。
在本快速入門導覽課程中,您將執行下列操作:
- 連線至 Bigtable 執行個體。
- 執行基本管理工作。
- 將資料寫入資料表。
- 從資料表讀取資料。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:
gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/bigtable.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
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
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Bigtable and Cloud Bigtable Admin APIs:
gcloud services enable bigtable.googleapis.com
bigtableadmin.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/bigtable.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- 執行下列指令,安裝
cbt
CLI :gcloud components install cbt
在 Google Cloud 控制台中開啟「建立執行個體」頁面。
在「Instance name」(執行個體名稱) 中輸入
Quickstart instance
。在「Instance ID」(執行個體 ID) 中輸入
quickstart-instance
。按一下「繼續」。
在「儲存類型」部分,請選擇「SSD」。
按一下「繼續」。
在「Cluster ID」(叢集 ID) 中輸入
quickstart-instance-c1
。在「區域」部分,選取您附近的區域。
在「可用區」部分,選取「任何」。
針對「節點資源調度模式」,選擇「手動分配」。
在「數量」部分,選取「1」。
按一下「建立」,建立執行個體。
建立
.cbtrc
檔案,將PROJECT_ID
替換為您建立 Bigtable 執行個體的專案 ID,藉此設定cbt
CLI 使用您的專案和執行個體:echo project = PROJECT_ID >> ~/.cbtrc && echo instance = quickstart-instance >> ~/.cbtrc
確認您已正確設定
.cbtrc
檔案:cat ~/.cbtrc
終端機會顯示
.cbtrc
檔案的內容,類似於下列內容:project = PROJECT_ID instance = quickstart-instance
現在您可以使用
cbt
CLI 執行個體。建立名為
my-table
的資料表。cbt createtable my-table
列出您的資料表︰
cbt ls
此指令會顯示類似下面內容的輸出:
my-table
新增名為
cf1
的資料欄系列:cbt createfamily my-table cf1
列出您的資料欄系列︰
cbt ls my-table
此指令會顯示類似下面內容的輸出:
Family Name GC Policy ----------- --------- cf1 <never>
使用資料欄系列
cf1
和資料欄限定詞c1
,將值test-value1
和test-value2
寫入資料列r1
:cbt set my-table r1 cf1:c1=test-value1 cbt set my-table r1 cf1:c1=test-value2
使用
cbt read
指令讀取您新增至資料表的資料:cbt read my-table
shell 會顯示類似以下內容的輸出︰
---------------------------------------- r1 cf1:c1 @ 2023/03/22-06:56:11.323000 "test-value1" cf1:c1 @ 2023/03/22-06:56:04.361000 "test-value2"
同一欄在第
r1
列中儲存了兩個含有時間戳記的值。刪除資料表
my-table
:cbt deletetable my-table
刪除執行個體:
cbt deleteinstance quickstart-instance
刪除
.cbtrc
檔案:rm ~/.cbtrc
選用:從 gcloud CLI 撤銷憑證:
gcloud auth revoke
- 逐步演練 Bigtable 程式碼研究室。
- 查看
cbt
CLI 參考說明文件。 - 請參閱詐欺偵測用途的範例原始碼。
- 以 C#、C++、Go、Java、Node.js、PHP、Python、Ruby 或 適用於 Java 的 HBase 用戶端,編寫 Hello World 應用程式。
建立 Bigtable 執行個體
連線至執行個體
讀取和寫入資料
Bigtable 會將資料儲存在資料表中,資料表內含資料列。每一資料列係由一「資料列索引鍵」識別。
資料列中的資料會組織成「資料欄系列」,即為資料欄的群組。「資料欄限定詞」識別資料欄系列中的單一資料欄。
資料列和資料欄的交集可以有多個含時間戳記的儲存格。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本快速入門導覽課程中所用資源的費用,請刪除執行個體。刪除 .cbtrc
檔案後,您就可以開始處理其他專案。