

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# EMR Serverless 的中繼存放區組態
<a name="metastore-config"></a>

*Hive 中繼存放*區是集中位置，可存放資料表的結構資訊，包括結構描述、分割區名稱和資料類型。使用 EMR Serverless， 會將此資料表中繼資料保留在可存取您任務的中繼存放區中。

Hive 中繼存放區有兩個選項：
+  AWS Glue Data Catalog
+ 外部 Apache Hive 中繼存放區

## 使用 AWS Glue Data Catalog 做為中繼存放區
<a name="glue-metastore"></a>

您可以將 Spark 和 Hive 任務設定為使用 AWS Glue Data Catalog 作為其中繼存放區。當您需要持久性中繼存放區或不同應用程式、服務或共用的中繼存放區時，我們建議您使用此組態 AWS 帳戶。如需 Data Catalog 的詳細資訊，請參閱[填入 AWS Glue Data Catalog](https://docs.aws.amazon.com/glue/latest/dg/populate-data-catalog.html)。如需 AWS Glue 定價的相關資訊，請參閱 [AWS Glue 定價](https://aws.amazon.com/glue/pricing)。

您可以將 EMR Serverless 任務設定為在 AWS 帳戶 與應用程式相同的 或不同的 中使用 AWS Glue Data Catalog AWS 帳戶。

### 設定 AWS Glue Data Catalog
<a name="glue-metastore-configure"></a>

若要設定 Data Catalog，請選擇您要使用的 EMR Serverless 應用程式類型。

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

當您使用 EMR Studio 搭配 EMR Serverless Spark 應用程式執行任務時， AWS Glue Data Catalog 是預設中繼存放區。

當您使用 SDKs或 時 AWS CLI，請在任務執行的`sparkSubmit`參數`com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory`中將`spark.hadoop.hive.metastore.client.factory.class`組態設定為 。下列範例示範如何使用 設定 Data Catalog 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 應用程式，Data Catalog 是預設中繼存放區。也就是說，當您在 EMR Serverless Hive 應用程式上執行任務時，Hive 會在 AWS 帳戶 與您應用程式相同的 資料目錄中記錄中繼存放區資訊。您不需要虛擬私有雲端 (VPC) 即可使用 Data Catalog 做為中繼存放區。

若要存取 Hive 中繼存放區資料表，請新增設定 AWS Glue [的 IAM 許可中概述的必要 Glue AWS](https://docs.aws.amazon.com/glue/latest/dg/getting-started-access.html)政策。

------

### 設定 EMR Serverless 和 Glue Data Catalog AWS 的跨帳戶存取
<a name="glue-metastore-cross-account"></a>

若要設定 EMR Serverless 的跨帳戶存取權，請先登入以下內容 AWS 帳戶：
+ `AccountA` – 您已在 AWS 帳戶 其中建立 EMR Serverless 應用程式。
+ `AccountB` – AWS 帳戶 包含您希望 EMR Serverless 任務執行存取的 AWS Glue Data Catalog。

1. 確定 中的管理員或其他授權身分將資源政策`AccountB`連接到 中的 Data Catalog`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 政策新增至 中的 EMR Serverless 任務執行期角色，`AccountA`讓該角色可以存取 中的 Data Catalog 資源`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 ]

   在 中傳遞 `spark.hadoop.hive.metastore.glue.catalogid` 屬性`sparkSubmitParameters`，如下列範例所示。{{`AccountB-catalog-id`}} 將 取代為 中資料目錄的 ID`AccountB`。

   ```
   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`}}取代為 中資料目錄的 ID`AccountB`。

   ```
   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 Data Catalog AWS 時的考量事項
<a name="glue-metastore-considerations"></a>

您可以在 Hive 指令碼`ADD JAR`中使用 新增輔助 JARs。如需其他考量，請參閱[使用 Glue Data Catalog AWS 時的考量](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-hive-metastore-glue.html#emr-hive-glue-considerations-hive)。