部署 Dataproc Metastore 服务

本页面介绍如何创建 Dataproc Metastore 服务以及如何从 Dataproc 集群连接到该服务。之后,您可以通过 SSH 登录到集群,启动 Apache Hive 实例,并运行一些基本查询。

Dataproc Metastore 为您提供完全兼容的 Hive Metastore (HMS),这是开源大数据生态系统中用于管理技术元数据的既定标准。此服务可帮助您管理数据湖的元数据,并在您使用的各种数据处理工具之间提供互操作性。


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


准备工作

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Dataproc Metastore, Dataproc APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Dataproc Metastore, Dataproc APIs.

    Enable the APIs

所需的角色

如需获得创建 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 IAM 概览

创建 Dataproc Metastore 服务

以下说明介绍了如何使用提供的默认设置创建基本的 Dataproc Metastore 服务。

控制台

  1. 在 Google Cloud 控制台中,前往 Dataproc Metastore 页面。

    前往 Dataproc Metastore

  2. 在导航菜单中,点击 + 创建

    系统会打开创建 Metastore 服务对话框。

  3. 选择 Dataproc Metastore 2

  4. 服务名称字段中,输入 example-service

  5. 数据位置字段中,选择 us-central1

  6. 对于其余的服务配置选项,请使用提供的默认值。

  7. 如需创建并启动服务,请点击提交

新 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 说明使用 APIs Explorer 创建服务

创建 Dataproc 集群并连接到 Dataproc Metastore

接下来,您将创建一个 Dataproc 集群,并从该集群连接到您的 metastore。之后,您的集群会将该 Metastore 服务用作其 HMS。 您在此处创建的集群将使用提供的默认设置。

控制台

  1. 在 Google Cloud 控制台中,前往 Dataproc 集群页面。

    前往 Dataproc 集群

  2. 在导航栏中,选择 + 创建集群

    系统会打开创建集群对话框,其中提供了多种基础架构供您选择。

  3. 在 Compute Engine 上创建集群行中,选择创建

    系统会打开在 Compute Engine 上创建 Dataproc 集群页面。

  4. 集群名称字段中,输入 example-cluster

  5. 区域可用区菜单中,选择 us-central1

  6. 对于其余的设置集群选项,请使用提供的默认值。

  7. 在导航菜单中,点击自定义集群(可选)标签页。

  8. Dataproc Metastore 部分中,选择您之前创建的 metastore 服务。

    如果您完全按照本教程操作,则该目录名为 example-service

  9. 对于其余的服务配置选项,请使用提供的默认值。

  10. 如需创建集群,请点击创建

    新集群会显示在集群列表中。在集群准备好投入使用之前,集群状态会显示为正在配置。准备就绪后,状态会变为有效。配置集群可能需要几分钟的时间。

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 会话。

  1. 在 Google Cloud 控制台中,前往虚拟机实例页面。
  2. 在虚拟机实例列表中,点击 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 会话中运行以下命令:

  1. 启动 Hive。

    hive
    
  2. 创建一个名为 myDatabase 的数据库。

    create database myDatabase;
    
  3. 显示您创建的数据库。

    show databases;
    
  4. 使用您创建的数据库。

    use myDatabase;
    
  5. 创建名为 myTable 的表。

    create table myTable(id int,name string);
    
  6. 列出 myDatabase 下的表格。

    show tables;
    
  7. 描述您创建的表的架构。

    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 账号产生费用,请按照以下步骤操作。

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. If the project that you plan to delete is attached to an organization, expand the Organization list in the Name column.
  3. In the project list, select the project that you want to delete, and then click Delete.
  4. In the dialog, type the project ID, and then click Shut down to delete the project.

或者,您可以删除本教程中使用的资源:

  1. 删除 Dataproc Metastore 服务。

    控制台

    1. 在 Google Cloud 控制台中,打开 Dataproc Metastore 页面:

      前往 Dataproc Metastore

    2. 在服务列表中,选择 example-service

    3. 在导航栏中,点击删除

      系统会打开删除服务对话框。

    4. 在该对话框中,点击删除

      您的服务将不再出现在服务列表中。

    gcloud CLI

    如需删除服务,请运行以下 gcloud metastore services delete 命令。

     gcloud metastore services delete example-service \
         --location=us-central1

    REST

    按照 API 说明使用 APIs Explorer 删除服务

    所有删除操作都会立即成功执行。

  2. 删除 Dataproc Metastore 服务的 Cloud Storage 存储桶

  3. 删除使用 Dataproc Metastore 服务的 Dataproc 集群

后续步骤