建立測試資料表
本頁說明如何建立小型 Bigtable 資料表,用於測試程式碼片段。這個表格包含智慧型手機和平板電腦的時間序列資料。
這些操作說明使用 Google Cloud 控制台和 cbt
CLI,這是專為 Bigtable 建構的指令列介面。
事前準備
建立測試資料表前,請先完成下列先決條件。
設定驗證方法
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
如要瞭解如何設定正式版環境的驗證作業,請參閱 Set up Application Default Credentials for code running on Google Cloud。
授予 IAM 角色
如要取得建立測試資料表所需的權限,請要求管理員授予您專案的 Bigtable 管理員 (roles/bigtable.admin
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
安裝 cbt
CLI
執行下列指令,安裝
cbt
CLI
:
gcloud components install cbt
建立執行個體
在所選專案中,建立測試執行個體來儲存測試資料表。 這個資料表很小,因此您只需要單一節點執行個體。
在 Google Cloud 控制台中開啟「建立執行個體」頁面。
在「Instance name」(執行個體名稱) 中輸入
Test instance
。在「Instance ID」(執行個體 ID) 中輸入
test-instance
。在「儲存類型」部分,請選擇「SSD」。
在「Cluster ID」(叢集 ID) 中輸入
test-instance-c1
。在「區域」部分,選取您附近的區域。
在「可用區」部分,選取您所選區域中的可用區。
在「Nodes」(節點) 下方輸入
1
。按一下「建立」,建立執行個體。
連線至執行個體
建立
.cbtrc
檔案,將PROJECT_ID
替換為您建立 Bigtable 執行個體的專案 ID,藉此設定cbt
CLI 使用您的專案和執行個體:echo project = PROJECT_ID >> ~/.cbtrc && echo instance = test-instance >> ~/.cbtrc
確認您已正確設定
.cbtrc
檔案:cat ~/.cbtrc
終端機會顯示
.cbtrc
檔案的內容,類似於下列內容:project = PROJECT_ID instance = test-instance
現在您可以使用
cbt
CLI 執行個體。
建立測試資料表
建立名為
test_table
的資料表,其中包含兩個資料欄系列。為每個資料欄系列設定垃圾收集政策,讓每個資料欄最多保留 2 個儲存格:cbt createtable test_table "families=stats_summary:maxversions=2,stats_detail:maxversions=2,cell_plan:maxversions=2"
列出資料欄系列:
cbt ls test_table
終端機顯示的輸出內容會與下列內容類似:
Family Name GC Policy ----------- --------- stats_detail versions() > 2 stats_summary versions() > 2 cell_plan versions() > 2
填入資料表資料
將下列指令複製到終端機視窗,然後按下
Enter
。cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=true cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=true cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=0 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190406.000 cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=1 stats_summary:connected_wifi=1 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=true cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=1 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=true
使用
cbt read
指令讀取您新增至資料表的資料:cbt read test_table
終端機顯示的輸出內容類似如下。請注意,由於寫入要求未包含時間戳記,因此系統會自動將時間戳記指派給儲存格。
---------------------------------------- phone#4c410523#20190501 cell_plan:data_plan_01gb @ 2023/09/23-11:44:10.535000 "true" cell_plan:data_plan_05gb @ 2023/09/23-11:44:10.535000 "true" stats_summary:connected_cell @ 2023/09/23-11:44:10.535000 "1" stats_summary:connected_wifi @ 2023/09/23-11:44:10.535000 "1" stats_summary:os_build @ 2023/09/23-11:44:10.535000 "PQ2A.190405.003" ---------------------------------------- phone#4c410523#20190502 cell_plan:data_plan_05gb @ 2023/09/23-11:44:11.545000 "true" stats_summary:connected_cell @ 2023/09/23-11:44:11.545000 "1" stats_summary:connected_wifi @ 2023/09/23-11:44:11.545000 "1" stats_summary:os_build @ 2023/09/23-11:44:11.545000 "PQ2A.190405.004" ---------------------------------------- phone#4c410523#20190505\ stats_summary:connected_cell @ 2023/09/23-11:44:12.503000 "0" stats_summary:connected_wifi @ 2023/09/23-11:44:12.503000 "1" stats_summary:os_build @ 2023/09/23-11:44:12.503000 "PQ2A.190406.000" ---------------------------------------- phone#5c10102#20190501 cell_plan:data_plan_10gb @ 2023/09/23-11:44:13.553000 "true" stats_summary:connected_cell @ 2023/09/23-11:44:13.553000 "1" stats_summary:connected_wifi @ 2023/09/23-11:44:13.553000 "1" stats_summary:os_build @ 2023/09/23-11:44:13.553000 "PQ2A.190401.002" ---------------------------------------- tablet#5c10102#20190502 cell_plan:data_plan_10gb @ 2023/09/23-11:44:14.480000 "true" stats_summary:connected_cell @ 2023/09/23-11:44:14.480000 "1" stats_summary:connected_wifi @ 2023/09/23-11:44:14.480000 "0" stats_summary:os_build @ 2023/09/23-11:44:14.480000 "PQ2A.190406.000"
選用:在相同資料列和資料欄中寫入新值。 Bigtable 會建立含有新時間戳記的新儲存格,用來儲存新版本的資料。將下列指令複製到終端機視窗,然後按下
Enter
。cbt set test_table phone#4c410523#20190501 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.003 cell_plan:data_plan_01gb=true cell_plan:data_plan_05gb=false cbt set test_table phone#4c410523#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=5 stats_summary:os_build=PQ2A.190405.004 cell_plan:data_plan_05gb=false cbt set test_table phone#4c410523#20190505 stats_summary:connected_cell=1 stats_summary:connected_wifi=4 stats_summary:os_build=PQ2A.190406.000 cbt set test_table phone#5c10102#20190501 stats_summary:connected_cell=3 stats_summary:connected_wifi=3 stats_summary:os_build=PQ2A.190401.002 cell_plan:data_plan_10gb=false cbt set test_table tablet#5c10102#20190502 stats_summary:connected_cell=2 stats_summary:connected_wifi=0 stats_summary:os_build=PQ2A.190406.000 cell_plan:data_plan_10gb=false
使用
cbt read
指令讀取資料表中的所有資料:cbt read test_table
終端機顯示的輸出內容類似如下。每個資料欄現在都包含兩個儲存格,且各有專屬的時間戳記。
---------------------------------------- phone#4c410523#20190501 cell_plan:data_plan_01gb @ 2024/07/09-17:48:21.191000 "true" cell_plan:data_plan_01gb @ 2024/07/09-17:46:09.369000 "true" cell_plan:data_plan_05gb @ 2024/07/09-17:48:21.191000 "false" cell_plan:data_plan_05gb @ 2024/07/09-17:46:09.369000 "true" stats_summary:connected_cell @ 2024/07/09-17:48:21.191000 "2" stats_summary:connected_cell @ 2024/07/09-17:46:09.369000 "1" stats_summary:connected_wifi @ 2024/07/09-17:48:21.191000 "5" stats_summary:connected_wifi @ 2024/07/09-17:46:09.369000 "1" stats_summary:os_build @ 2024/07/09-17:48:21.191000 "PQ2A.190405.003" stats_summary:os_build @ 2024/07/09-17:46:09.369000 "PQ2A.190405.003" ---------------------------------------- phone#4c410523#20190502 cell_plan:data_plan_05gb @ 2024/07/09-17:48:22.205000 "false" cell_plan:data_plan_05gb @ 2024/07/09-17:46:10.455000 "true" stats_summary:connected_cell @ 2024/07/09-17:48:22.205000 "2" stats_summary:connected_cell @ 2024/07/09-17:46:10.455000 "1" stats_summary:connected_wifi @ 2024/07/09-17:48:22.205000 "5" stats_summary:connected_wifi @ 2024/07/09-17:46:10.455000 "1" stats_summary:os_build @ 2024/07/09-17:48:22.205000 "PQ2A.190405.004" stats_summary:os_build @ 2024/07/09-17:46:10.455000 "PQ2A.190405.004" ---------------------------------------- phone#4c410523#20190505 stats_summary:connected_cell @ 2024/07/09-17:48:23.206000 "1" stats_summary:connected_cell @ 2024/07/09-17:46:11.402000 "0" stats_summary:connected_wifi @ 2024/07/09-17:48:23.206000 "4" stats_summary:connected_wifi @ 2024/07/09-17:46:11.402000 "1" stats_summary:os_build @ 2024/07/09-17:48:23.206000 "PQ2A.190406.000" stats_summary:os_build @ 2024/07/09-17:46:11.402000 "PQ2A.190406.000" ---------------------------------------- phone#5c10102#20190501 cell_plan:data_plan_10gb @ 2024/07/09-17:48:24.172000 "false" cell_plan:data_plan_10gb @ 2024/07/09-17:46:12.388000 "true" stats_summary:connected_cell @ 2024/07/09-17:48:24.172000 "3" stats_summary:connected_cell @ 2024/07/09-17:46:12.388000 "1" stats_summary:connected_wifi @ 2024/07/09-17:48:24.172000 "3" stats_summary:connected_wifi @ 2024/07/09-17:46:12.388000 "1" stats_summary:os_build @ 2024/07/09-17:48:24.172000 "PQ2A.190401.002" stats_summary:os_build @ 2024/07/09-17:46:12.388000 "PQ2A.190401.002" ---------------------------------------- tablet#5c10102#20190502 cell_plan:data_plan_10gb @ 2024/07/09-17:48:25.194000 "false" cell_plan:data_plan_10gb @ 2024/07/09-17:46:13.391000 "true" stats_summary:connected_cell @ 2024/07/09-17:48:25.194000 "2" stats_summary:connected_cell @ 2024/07/09-17:46:13.391000 "1" stats_summary:connected_wifi @ 2024/07/09-17:48:25.194000 "0" stats_summary:connected_wifi @ 2024/07/09-17:46:13.391000 "0" stats_summary:os_build @ 2024/07/09-17:48:25.194000 "PQ2A.190406.000" stats_summary:os_build @ 2024/07/09-17:46:13.391000 "PQ2A.190406.000"
使用測試資料
現在可以使用 test-instance
上的 test_table
測試程式碼範例或查詢。
清除所用資源
為避免系統向您的 Google Cloud 帳戶收取這些步驟建立的資源費用,請在完成測試後立即刪除執行個體。刪除 .cbtrc 檔案後,您就可以開始處理其他專案。
如要保留執行個體但刪除資料表,可以使用
deletetable
指令:cbt deletetable test_table
如果您不打算使用執行個體進行進一步測試,請刪除執行個體。這項操作也會刪除資料表。
cbt deleteinstance test-instance
刪除 .cbtrc 檔案:
rm ~/.cbtrc