部署 Dataproc Metastore 服務
本頁說明如何建立 Dataproc Metastore 服務,並從 Dataproc 叢集連線至該服務。接著,您將透過 SSH 連線至叢集、啟動 Apache Hive 執行個體,並執行一些基本查詢。
Dataproc Metastore 提供完全相容的 Hive Metastore (HMS),這是開放原始碼大數據生態系統中管理技術中繼資料的既定標準。這項服務可協助您管理資料湖泊的元資料,並在您使用的各種資料處理工具之間提供互通性。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc Metastore, Dataproc APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc Metastore, Dataproc APIs.
必要的角色
如要取得建立 Dataproc Metastore 和 Dataproc 叢集所需的權限,請要求管理員授予下列 IAM 角色:
-
如要授予所有 Dataproc Metastore 資源的完整存取權,包括設定 IAM 權限:
(
roles/metastore.admin
) 在使用者帳戶或服務帳戶上 -
如要授予 Dataproc Metastore 資源的完整控制權:
在使用者帳戶或服務帳戶上,指派 Dataproc Metastore 編輯者 (
roles/metastore.editor
) -
如要建立 Dataproc 叢集,請按照下列步驟操作:
(
roles/dataproc.worker
) 在服務帳戶上
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這些預先定義的角色具備建立 Dataproc Metastore 和 Dataproc 叢集所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要建立 Dataproc Metastore 和 Dataproc 叢集,必須具備下列權限:
-
如要建立 Dataproc Metastore 服務:
metastore.services.create
在使用者帳戶或服務帳戶上 -
如要建立 Dataproc 叢集:
Dataproc worker (
在服務帳戶上roles/dataproc.worker
)
建立 Dataproc Metastore 服務
以下操作說明會引導您使用預設設定,建立基本的 Dataproc Metastore 服務。
控制台
在 Google Cloud 控制台中,前往「Dataproc Metastore」頁面。
在導覽選單中,按一下「+ 建立」。
「建立 Metastore 服務」對話方塊隨即開啟。
選取「Dataproc Metastore 2」。
在「Service name」(服務名稱) 欄位中輸入
example-service
。在「資料位置」欄位中,選取「
us-central1
」。其餘服務設定選項請使用系統提供的預設值。
如要建立並啟動服務,請按一下「Submit」(提交)。
新的 Metastore 服務會顯示在「Dataproc Metastore」頁面。服務準備就緒前,狀態會顯示「正在建立」。準備就緒後,狀態會變更為「有效」。佈建服務可能需要幾分鐘。
以下螢幕截圖顯示「建立服務」頁面範例,其中使用了一些預設值。
gcloud CLI
如要使用提供的預設值建立中繼資料存放區服務,請執行下列 gcloud metastore services create
指令:
gcloud metastore services create example-service \ --location=us-central1 \ --instance-size=MEDIUM
這個指令會在預設區域 (us-central1
) 中建立名為 example-service
的服務,並使用預設執行個體大小 (MEDIUM
)。
REST
按照 API 操作說明,使用 API Explorer 建立服務。
建立 Dataproc 叢集並連線至 Dataproc Metastore
接著,請建立 Dataproc 叢集,並從叢集連線至 Metastore。之後,叢集就會使用 Metastore 服務做為 HMS。 您在這裡建立的叢集會使用預設設定。
控制台
在 Google Cloud 控制台中,前往「Dataproc Clusters」(Dataproc 叢集) 頁面。
在導覽列中,選取「+ 建立叢集」。
「建立叢集」對話方塊隨即開啟,提供多種基礎架構選項供您選擇。
在「Cluster on Compute Engine」(Compute Engine 上的叢集) 列中,選取「Create」(建立)。
系統會開啟「在 Compute Engine 上建立 Dataproc 叢集」頁面。
在「Cluster Name」(叢集名稱) 欄位中輸入
example-cluster
。在「Region」(區域) 和「Zone」(可用區) 選單中,選取「
us-central1
」。其餘「設定叢集」選項請使用系統提供的預設值。
在導覽選單中,按一下「Customize cluster (optional)」(自訂叢集 (選用)) 分頁標籤。
在「Dataproc Metastore」專區中,選取您先前建立的 Metastore 服務。
如果您完全按照本教學課程操作,則名稱為
example-service
。其餘服務設定選項請使用系統提供的預設值。
如要建立叢集,請按一下「建立」。
新叢集會顯示在「叢集」清單中。在叢集準備好可以使用之前,叢集狀態會顯示為「佈建中」。準備就緒後,狀態會變更為「有效」。佈建叢集可能需要幾分鐘的時間。
gcloud CLI
如要使用提供的預設設定建立叢集,請執行下列 gcloud dataproc clusters create
指令:
gcloud dataproc clusters create example-cluster \ --dataproc-metastore=projects/PROJECT_ID/locations/us-central1/services/example-service \ --region=us-central1
將 PROJECT_ID
替換為您建立 Dataproc Metastore 服務的專案 ID。
REST
按照 API 操作說明,使用 API Explorer 建立叢集。
透過 Dataproc 叢集連線至 Apache Hive
接下來的步驟說明如何在 Apache Hive 中執行一些範例指令,建立資料庫和資料表。
接著,在 Dataproc 叢集上開啟 SSH 工作階段,並啟動 Hive 工作階段。
瀏覽器視窗會開啟節點上的主目錄,並顯示類似下列內容的輸出結果:
Connected, host fingerprint: ssh-rsa ...
Linux cluster-1-m 3.16.0-0.bpo.4-amd64 ...
...
example-cluster@cluster-1-m:~$
如要啟動 Hive 並建立資料庫和資料表,請在 SSH 工作階段中執行下列指令:
啟動 Hive。
hive
建立名為
myDatabase
的資料庫。create database myDatabase;
顯示您建立的資料庫。
show databases;
使用您建立的資料庫。
use myDatabase;
建立名為
myTable
的資料表。create table myTable(id int,name string);
列出
myDatabase
下方的資料表。show tables;
說明您建立的資料表結構定義。
desc MyTable;
執行這些指令後,輸出內容會類似下方示例:
$hive
hive> show databases;
OK
default
hive> create database myDatabase;
OK
hive> use myDatabase;
OK
hive> create table myTable(id int,name string);
OK
hive> show tables;
OK
myTable
hive> desc myTable;
OK
id int
name string
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
- In the Google Cloud console, go to the Manage resources page.
- If the project that you plan to delete is attached to an organization, expand the Organization list in the Name column.
- 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.
或者,您也可以刪除本教學課程中使用的資源:
刪除 Dataproc Metastore 服務。
控制台
在 Google Cloud 控制台中,開啟「Dataproc Metastore」頁面:
在服務清單中選取
example-service
。按一下導覽列中的「刪除」。
系統會開啟「刪除服務」對話方塊。
在對話方塊中按一下「刪除」。
「服務清單」中不會再顯示您的服務。
gcloud CLI
如要刪除服務,請執行下列
gcloud metastore services delete
指令。gcloud metastore services delete example-service \ --location=us-central1
REST
按照 API 操作說明,使用 API Explorer 刪除服務。
系統會立即刪除所有項目。