部署 Dataproc Metastore 服务
本页面介绍如何创建 Dataproc Metastore 服务并从 Dataproc 集群连接到该服务。之后,通过 SSH 连接到集群,启动 Apache Hive 实例,并运行一些基本查询。
Dataproc Metastore 为您提供完全兼容的 Hive Metastore (HMS),这是开源大数据生态系统中用于管理技术元数据的既定标准。此服务可帮助您管理数据湖的元数据,并在您使用的各种数据处理工具之间提供互操作性。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
启用 Dataproc Metastore, Dataproc API。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
启用 Dataproc Metastore, Dataproc API。
必需的角色
如需获取创建 Dataproc Metastore 和 Dataproc 集群所需的权限,请让管理员向您授予以下 IAM 角色:
-
如需授予对所有 Dataproc Metastore 资源的完整访问权限(包括设置 IAM 权限),请执行以下操作:
针对用户帐号或服务帐号的 (
roles/metastore.admin
) -
如需授予对 Dataproc Metastore 资源的完全控制权,请执行以下操作:
用户帐号或服务帐号的 Dataproc Metastore Editor (
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。
在服务名称字段中,输入
example-service
。在数据位置字段中,选择
us-central1
。对于其余服务配置选项,请使用提供的默认值。
如需创建并启动服务,请点击提交。
您的新 Metastore 服务会显示在 Dataproc Metastore 页面上。状态显示为正在创建,直到服务可供使用。准备就绪后,状态会更改为 Active。预配服务可能需要几分钟时间。
以下屏幕截图显示了使用一些提供的默认值的创建服务页面示例。
gcloud CLI
如需使用提供的默认值创建 Metastore 服务,请运行以下 gcloud metastore services create
命令:
gcloud metastore services create example-service \ --location=us-central1 \ --instance-size=MEDIUM
此命令会在默认区域 (us-central1
) 中以默认实例大小 (MEDIUM
) 创建名为 example-service
的服务。
REST
按照 API 说明使用 APIs Explorer 创建服务。
创建 Dataproc 集群并连接到 Dataproc Metastore
接下来,您将创建一个 Dataproc 集群,并从该集群连接到您的 Metastore。之后,您的集群会将 Metastore 服务当作 HMS 使用。您在此处创建的集群会使用系统提供的默认设置。
控制台
在 Google Cloud 控制台中,转到 Dataproc 集群页面。
在导航栏中,选择 +创建集群。
系统将打开创建集群对话框,其中提供了多个基础架构选项供您选择。
在 Compute Engine 上的集群行中,选择创建。
此时会打开在 Compute Engine 上创建 Dataproc 集群页面。
在集群名称字段中,输入
example-cluster
。在区域和可用区菜单中,选择
us-central1
。对于其余的设置集群选项,使用提供的默认值。
在导航菜单中,点击自定义集群(可选)标签页。
在 Dataproc Metastore 部分中,选择您之前创建的 Metastore 服务。
如果您按原样按照本教程进行操作,它将被命名为
example-service
。对于其余服务配置选项,请使用提供的默认值。
如需创建集群,请点击创建。
您的新集群会显示在集群列表中。在集群可供使用之前,集群状态会显示正在预配。准备就绪后,状态会更改为 Active。预配集群可能需要几分钟时间。
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 会话。
- 在 Google Cloud 控制台中,转到虚拟机实例页面。
- 在虚拟机实例列表中,点击
example-cluster
旁边的 SSH。
在该节点上的主目录中打开了一个浏览器窗口,其输出类似于以下内容:
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 账号产生费用,请按照以下步骤操作。
- 在 Google Cloud 控制台中,进入管理资源页面。
- 如果您打算删除的项目已附加到某个组织,请展开名称列中的组织列表。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
或者,您可以删除本教程中使用的资源:
删除 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 说明使用 APIs Explorer 删除服务。
所有删除操作都会立即成功执行。