Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
Hive 元存储是一个集中位置,用于存储表的结构信息,包括架构、分区名称和数据类型。通过 EMR Serverless,请将此表元数据保存在有权访问您的作业的元存储中。
对于 Hive 元存储,您有两个选项:
-
Amazon Glue Data Catalog
-
外部 Apache Hive 元存储
您可以将 Spark 和 Hive 作业配置为使用 Amazon Glue Data Catalog 作为其元存储。如果需要持久元存储,或不同应用程序、服务或 Amazon Web Services 账户 应用程序共享元存储,建议使用此配置。有关 Data Catalog 的更多信息,请参阅填充 Amazon Glue Data Catalog。有关 Amazon Glue 定价的信息,请参阅 Amazon Glue 定价。
您可以将 EMR Serverless 作业配置为在与应用程序相同的 Amazon Web Services 账户 或不同的 Amazon Web Services 账户 中使用 Amazon Glue Data Catalog。
要配置数据目录,请选择要使用的 EMR Serverless 应用程序类型。
- Spark
-
当您使用 EMR Studio 通过 EMR Serverless Spark 应用程序运行作业时,Amazon Glue Data Catalog 是默认元存储。
使用 SDK 或 Amazon CLI 时,请在作业运行的 sparkSubmit 参数中将 spark.hadoop.hive.metastore.client.factory.class 配置设置为 com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory。下面的示例显示了如何使用 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 元存储表,请添加设置 Amazon Glue 的 IAM 权限中概述的所需 Amazon Glue 策略。
要设置 EMR Serverless 的跨账户访问,请首先登录以下 Amazon Web Services 账户:
-
确保 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"
}
]
}
-
将 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"
}
]
}
-
启动作业运行。此步骤略有不同,具体取决于 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"
}
}]
}'
您可以在 Hive 脚本中添加具有 ADD JAR 的辅助 JAR。有关其他注意事项,请参阅使用 Amazon Glue Data Catalog 时的注意事项。