本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Hive 元存储是一个集中位置,用于存储表的结构信息,包括架构、分区名称和数据类型。通过 EMR Serverless,可将表元数据保存在可以访问作业的元存储中。
对于 Hive 元存储,您有两个选项:
-
AWS Glue 数据目录
-
外部 Apache Hive 元存储
你可以将 Spark 和 Hive 作业配置为使用 Glue AWS 数据目录作为其元数据库。如果需要持久元存储,或不同应用程序、服务或 AWS 账户应用程序共享元存储,建议使用此配置。有关数据目录的更多信息,请参阅填充 AWS Glue 数据目录。有关 AWS Glue 定价的信息,请参阅 AWS Glue 定价。
您可以将 EMR Serverless 作业配置为在与您的应用程序 AWS 账户 相同或不同的应用程序中使用 AWS Glue 数据目录。 AWS 账户
要配置数据目录,请选择要使用的 EMR Serverless 应用程序类型。
- Spark
-
当你使用 EMR Studio 通过 EMR Serverless Spark 应用程序运行作业时,Glue 数据目录是默认的 AWS 元数据库。
使用 SDKs 或时 AWS CLI,可以在作业运行的sparkSubmit
参数com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
中将spark.hadoop.hive.metastore.client.factory.class
配置设置为。下面的示例显示了如何使用 AWS 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 在数据目录中以与应用程序相同的形式记录元存储信息。 AWS 账户 您不需要虚拟私有云(VPC)即可将数据目录用作元存储。
要访问 Hive 元数据仓表,请添加为 G AWS lue 设置 IAM 权限中概述的必需 G AWS lue 策略。
要为 EMR Serverless 设置跨账户访问权限,您必须先登录以下网站: AWS 账户
-
确保 AccountB
中的管理员或其他授权身份将资源策略附加到 AccountB
中的数据目录。此策略授予 AccountA
特定的跨账户权限,以便对 AccountB
目录中的资源执行操作。
{
"Version" : "2012-10-17",
"Statement" : [ {
"Effect" : "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::accountA
:role/job-runtime-role-A"
]},
"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:region:AccountB
:catalog"]
} ]
}
-
将 IAM 策略添加到 AccountA
中的 EMR Serverless 作业运行时角色,以便该角色可以访问 AccountB
中的数据目录资源。
{
"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:region:AccountB
:catalog"]
}
]
}
-
启动作业运行。此步骤略有不同,具体取决于 AccountA
的 EMR Serverless 应用程序类型。
- Spark
-
如以下示例所示,在 hive-site
分类中设置 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": {
"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": {
"spark.hadoop.hive.metastore.glue.catalogid": "AccountB-catalog-id
"
}
}]
}'
- 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
中 JARs 使用添加辅助工具。有关其他注意事项,请参阅使用 AWS Glue 数据目录时的注意事项。