目標
本教學課程將示範如何:
- 建立 Dataproc 叢集,並在叢集上安裝 Apache HBase 和 Apache ZooKeeper
- 使用在 Dataproc 叢集主要節點上執行的 HBase Shell 建立 HBase 資料表
- 使用 Cloud Shell 將 Java 或 PySpark Spark 工作提交至 Dataproc 服務,該服務會將資料寫入 HBase 資料表,然後從該資料表讀取資料
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
如果您尚未建立 Google Cloud Platform 專案,請先建立專案。
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc and Compute Engine APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc and Compute Engine APIs.
在 Cloud Shell 工作階段終端機中執行下列指令,即可:
從 Google Cloud 控制台或 Cloud Shell 工作階段終端機,使用 SSH 連結至 Dataproc 叢集的主要節點。
在主要節點上驗證 Apache HBase Spark 連接器的安裝作業:
輸出範例:ls -l /usr/lib/spark/jars | grep hbase-spark
-rw-r--r-- 1 root root size date time hbase-spark-connector.version.jar
請將 SSH 工作階段終端機保持為開啟狀態,以便:
- 建立 HBase 資料表
- (Java 使用者):在叢集的主節點上執行指令,判斷叢集上安裝的元件版本
- 掃描 Hbase 資料表,然後執行程式碼
開啟 HBase shell:
hbase shell
建立 HBase「my-table」,並使用「cf」資料欄系列:
create 'my_table','cf'
- 如要確認表格建立作業,請在 Google Cloud 控制台中,按一下Google Cloud 控制台元件閘道連結中的「HBase」HBase,開啟 Apache HBase UI。
my-table
會列於「首頁」頁面的「資料表」區段中。
- 如要確認表格建立作業,請在 Google Cloud 控制台中,按一下Google Cloud 控制台元件閘道連結中的「HBase」HBase,開啟 Apache HBase UI。
開啟 Cloud Shell 工作階段終端機。
將 GitHub GoogleCloudDataproc/cloud-dataproc 存放區複製到 Cloud Shell 工作階段終端機:
git clone https://github.com/GoogleCloudDataproc/cloud-dataproc.git
切換至
cloud-dataproc/spark-hbase
目錄: 輸出範例:cd cloud-dataproc/spark-hbase
user-name@cloudshell:~/cloud-dataproc/spark-hbase (project-id)$
提交 Dataproc 工作。
- 在
pom.xml
檔案中設定元件版本。- Dataproc 2.0.x 版頁面列出安裝在最新和最後四個映像檔 2.0 次要版本中的 Scala、Spark 和 HBase 元件版本。
- 或者,您也可以在叢集主要節點的 SSH 工作階段終端機中執行下列指令,判斷元件版本:
- 檢查 Scala 版本:
scala -version
- 檢查 Spark 版本 (按 Ctrl + D 鍵即可退出):
spark-shell
- 檢查 HBase 版本:
hbase version
- 在 Maven
pom.xml
中找出 Spark、Scala 和 HBase 版本依附元件: 注意:<properties> <scala.version>scala full version (for example, 2.12.14)</scala.version> <scala.main.version>scala main version (for example, 2.12)</scala.main.version> <spark.version>spark version (for example, 3.1.2)</spark.version> <hbase.client.version>hbase version (for example, 2.2.7)</hbase.client.version> <hbase-spark.version>1.0.0(the current Apache HBase Spark Connector version)> </properties>
hbase-spark.version
是目前的 Spark HBase 連接器版本,請勿變更這個版本號碼。
- 檢查 Scala 版本:
- 在 Cloud Shell 編輯器中編輯
pom.xml
檔案,插入正確的 Scala、Spark 和 HBase 版本號碼。編輯完成後,按一下「Open Terminal」(開啟終端機),返回 Cloud Shell 終端機指令列。cloudshell edit .
- 在 Cloud Shell 中切換至 Java 8。建構程式碼時需要這個 JDK 版本 (你可以忽略任何外掛程式警告訊息):
sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 && export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
- 驗證 Java 8 安裝狀態:
輸出範例:java -version
openjdk version "1.8..."
- 建構
jar
檔案:mvn clean package
.jar
檔案會放在/target
子目錄中 (例如target/spark-hbase-1.0-SNAPSHOT.jar
)。 提交工作。
gcloud dataproc jobs submit spark \ --class=hbase.SparkHBaseMain \ --jars=target/filename.jar \ --region=cluster-region \ --cluster=cluster-name
--jars
:在「target/」之後和「.jar」之前插入.jar
檔案的名稱。- 如果您在建立叢集時未設定 Spark 驅動程式和執行器 HBase 類別路徑,則必須在每次提交工作時設定,方法是在工作提交指令中加入下列
‑‑properties
標記:--properties='spark.driver.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*,spark.executor.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*'
在 Cloud Shell 工作階段終端機輸出中,查看 HBase 資料表輸出內容:
Waiting for job output... ... +----+----+ | key|name| +----+----+ |key1| foo| |key2| bar| +----+----+
提交工作。
gcloud dataproc jobs submit pyspark scripts/pyspark-hbase.py \ --region=cluster-region \ --cluster=cluster-name
- 如果您在建立叢集時未設定 Spark 驅動程式和執行器 HBase 類別路徑,則必須在每次提交工作時設定,方法是在工作提交指令中加入下列
‑‑properties
標記:--properties='spark.driver.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*,spark.executor.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*'
- 如果您在建立叢集時未設定 Spark 驅動程式和執行器 HBase 類別路徑,則必須在每次提交工作時設定,方法是在工作提交指令中加入下列
在 Cloud Shell 工作階段終端機輸出中,查看 HBase 資料表輸出內容:
Waiting for job output... ... +----+----+ | key|name| +----+----+ |key1| foo| |key2| bar| +----+----+
- 開啟 HBase shell:
hbase shell
- 掃描「my-table」:
輸出範例:scan 'my_table'
ROW COLUMN+CELL key1 column=cf:name, timestamp=1647364013561, value=foo key2 column=cf:name, timestamp=1647364012817, value=bar 2 row(s) Took 0.5009 seconds
清除所用資源
完成教學課程後,您可以清除所建立的資源,這樣資源就不會繼續使用配額,也不會產生費用。下列各節將說明如何刪除或關閉這些資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除叢集
- 如要刪除叢集:
gcloud dataproc clusters delete cluster-name \ --region=${REGION}
建立 Dataproc 叢集
gcloud dataproc clusters create cluster-name \ --region=region \ --optional-components=HBASE,ZOOKEEPER \ --num-workers=3 \ --enable-component-gateway \ --image-version=2.0 \ --properties='spark:spark.driver.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*,spark:spark.executor.extraClassPath=/etc/hbase/conf:/usr/lib/hbase/*'
驗證連接器安裝作業
建立 HBase 資料表
在上一步開啟的主節點 SSH 工作階段終端機中,執行本節列出的指令。
查看 Spark 程式碼
Java
Python
執行程式碼
Java
Python
掃描 HBase 資料表
如要掃描 HBase 表格的內容,請在「驗證連接器安裝作業」中開啟主要節點 SSH 工作階段終端機,然後執行下列指令: