

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# EMR Serverless 的元存储配置
<a name="metastore-config"></a>

*Hive 元存储*是一个集中位置，用于存储表的结构信息，包括架构、分区名称和数据类型。通过 EMR Serverless，请将此表元数据保存在有权访问您的作业的元存储中。

对于 Hive 元存储，您有两个选项：
+  Amazon Glue 数据目录
+ 外部 Apache Hive 元存储

## 使用 Amazon Glue 数据目录作为元数据库
<a name="glue-metastore"></a>

你可以将 Spark 和 Hive 作业配置为使用 Glue Amazon 数据目录作为其元数据库。如果需要持久元存储，或不同应用程序、服务或 Amazon Web Services 账户应用程序共享元存储，建议使用此配置。有关数据目录的更多信息，请参阅[填充 Amazon Glue 数据目录](https://docs.amazonaws.cn/glue/latest/dg/populate-data-catalog.html)。有关 Amazon Glue 定价的信息，请参阅 [Amazon Glue 定价](https://www.amazonaws.cn/glue/pricing)。

您可以将 EMR Serverless 作业配置为在与您的应用程序 Amazon Web Services 账户 相同或不同的应用程序中使用 Amazon Glue 数据目录。 Amazon Web Services 账户

### 配置 Amazon Glue 数据目录
<a name="glue-metastore-configure"></a>

要配置数据目录，请选择要使用的 EMR Serverless 应用程序类型。

------
#### [ Spark ]

当你使用 EMR Studio 通过 EMR Serverless Spark 应用程序运行作业时，Glue 数据目录是默认的 Amazon 元数据库。

使用 SDKs 或时 Amazon CLI，在作业运行的`sparkSubmit`参数`com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory`中将`spark.hadoop.hive.metastore.client.factory.class`配置设置为。下面的示例显示了如何使用 Amazon CLI配置数据目录。

```
aws emr-serverless start-job-run \
    --application-id application-id \
    --execution-role-arn job-role-arn \
    --job-driver '{
        "sparkSubmit": {
            "entryPoint": "s3://amzn-s3-demo-bucket/code/pyspark/extreme_weather.py",
            "sparkSubmitParameters": "--conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory --conf spark.driver.cores=1 --conf spark.driver.memory=3g --conf spark.executor.cores=4 --conf spark.executor.memory=3g"
        }
    }'
```

或者，您可以在 Spark 代码中创建新 `SparkSession` 时设置此配置。

```
from pyspark.sql import SparkSession

spark = (
    SparkSession.builder.appName("SparkSQL")
    .config(
        "spark.hadoop.hive.metastore.client.factory.class",
        "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory",
    )
    .enableHiveSupport()
    .getOrCreate()
)

# we can query tables with SparkSQL
spark.sql("SHOW TABLES").show()

# we can also them with native Spark
print(spark.catalog.listTables())
```

------
#### [ Hive ]

对于 EMR Serverless Hive 应用程序，数据目录是默认的元存储。也就是说，当你在 EMR Serverless Hive 应用程序上运行作业时，Hive 在数据目录中以与应用程序相同的形式记录元存储信息。 Amazon Web Services 账户 您不需要虚拟私有云（VPC）即可将数据目录用作元存储。

要访问 Hive 元数据仓表，请添加为 G Amazon lue [设置 IAM 权限中概述的必需 G Amazon](https://docs.amazonaws.cn/glue/latest/dg/getting-started-access.html) lue 策略。

------

### 为 EMR Serverless Amazon 和 Glue 数据目录配置跨账户访问权限
<a name="glue-metastore-cross-account"></a>

要为 EMR Serverless 设置跨账户访问权限，请先登录以下网站： Amazon Web Services 账户
+ `AccountA`— 这是您创建了 EMR 无服务器应用程序 Amazon Web Services 账户 的地方。
+ `AccountB`— 包 Amazon Web Services 账户 含 Amazon Glue 数据目录，你想让 EMR Serverless 作业运行访问该目录。

1. 确保 `AccountB` 中的管理员或其他授权身份将资源策略附加到 `AccountB` 中的数据目录。此策略授予 `AccountA` 特定的跨账户权限，以便对 `AccountB` 目录中的资源执行操作。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "glue:GetDatabase",
           "glue:CreateDatabase",
           "glue:GetDataBases",
           "glue:CreateTable",
           "glue:GetTable",
           "glue:UpdateTable",
           "glue:DeleteTable",
           "glue:GetTables",
           "glue:GetPartition",
           "glue:GetPartitions",
           "glue:CreatePartition",
           "glue:BatchCreatePartition",
           "glue:GetUserDefinedFunctions"
         ],
         "Resource": [
           "arn:aws:glue:*:123456789012:catalog"
         ],
         "Sid": "AllowGLUEGetdatabase"
       }
     ]
   }
   ```

------

1. 将 IAM 策略添加到 `AccountA` 中的 EMR Serverless 作业运行时角色，以便该角色可以访问 `AccountB` 中的数据目录资源。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "glue:GetDatabase",
           "glue:CreateDatabase",
           "glue:GetDataBases",
           "glue:CreateTable",
           "glue:GetTable",
           "glue:UpdateTable",
           "glue:DeleteTable",
           "glue:GetTables",
           "glue:GetPartition",
           "glue:GetPartitions",
           "glue:CreatePartition",
           "glue:BatchCreatePartition",
           "glue:GetUserDefinedFunctions"
         ],
         "Resource": [
           "arn:aws:glue:*:123456789012:catalog"
         ],
         "Sid": "AllowGLUEGetdatabase"
       }
     ]
   }
   ```

------

1.  启动作业运行。此步骤略有不同，具体取决于 `AccountA` 的 EMR Serverless 应用程序类型。

------
#### [ Spark ]

   如以下示例所示传递 `sparkSubmitParameters` 中的 `spark.hadoop.hive.metastore.glue.catalogid` 属性。将 *`AccountB-catalog-id`* 替换为 `AccountB` 中数据目录的 ID。

   ```
   aws emr-serverless start-job-run \
   --application-id "application-id" \
   --execution-role-arn "job-role-arn" \
   --job-driver '{
       "sparkSubmit": {
           "entryPoint": "s3://amzn-s3-demo-bucket/scripts/test.py",
            "sparkSubmitParameters": "--conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory --conf spark.hadoop.hive.metastore.glue.catalogid=AccountB-catalog-id --conf spark.executor.cores=1 --conf spark.executor.memory=1g --conf spark.driver.cores=1 --conf spark.driver.memory=1g --conf spark.executor.instances=1"
       }
     }' \
   --configuration-overrides '{
       "monitoringConfiguration": {
       "s3MonitoringConfiguration": {
       "logUri": "s3://amzn-s3-demo-bucket/logs/"
       }
     }
   }'
   ```

------
#### [ Hive ]

   如以下示例所示，在 `hive-site` 分类中设置 `hive.metastore.glue.catalogid` 属性。将 *`AccountB-catalog-id`* 替换为 `AccountB` 中数据目录的 ID。

   ```
   aws emr-serverless start-job-run \
   --application-id "application-id" \
   --execution-role-arn "job-role-arn" \
   --job-driver '{
       "hive": {
       "query": "s3://amzn-s3-demo-bucket/hive/scripts/create_table.sql",
       "parameters": "--hiveconf hive.exec.scratchdir=s3://amzn-s3-demo-bucket/hive/scratch --hiveconf hive.metastore.warehouse.dir=s3://amzn-s3-demo-bucket/hive/warehouse"
       }
   }' \
   --configuration-overrides '{
       "applicationConfiguration": [{
           "classification": "hive-site",
           "properties": {
               "hive.metastore.glue.catalogid": "AccountB-catalog-id"
           }
       }]
   }'
   ```

------

### 使用 Glue 数据 Amazon 目录时的注意事项
<a name="glue-metastore-considerations"></a>

您可以在 Hive 脚本`ADD JAR`中 JARs 使用添加辅助工具。有关其他注意事项，请参阅[使用 Amazon Glue 数据目录时的注意事项](https://docs.amazonaws.cn/emr/latest/ReleaseGuide/emr-hive-metastore-glue.html#emr-hive-glue-considerations-hive)。