跳至內容

如何在 Amazon EMR 上使用 Hive 和 Spark 查詢不同帳戶中的 Data Catalog?

3 分的閱讀內容
0

我想要使​​用 Apache Hive 和 Apache Spark 存取和查詢 Amazon EMR 中另一個 AWS 帳戶的 AWS Glue Data Catalog。

簡短描述

若要動態存取不同帳戶中的 Data Catalog,請在 Hive 或 Spark 組態中指定屬性 aws.glue.catalog.separator。您也可以在組態中指定屬性 hive.metastore.glue.catalogid 來存取另一個帳戶中的特定 Data Catalog。

解決方法

授予跨帳戶存取權至 AWS Glue。確認 AWS Glue 資料表所指向的 Amazon Simple Storage Service (Amazon S3) 儲存貯體已設定為跨帳戶存取。如需詳細資訊,請參閱如何設定 EMR 檔案系統的跨帳戶存取權?

動態存取不同帳戶中的 Data Catalog

將組態變更套用到新叢集

啟動 EMR 叢集時,請新增類似以下範例的組態物件。如需詳細資訊,請參閱建立叢集時設定應用程式

[
    {
        "Classification": "hive-site",
        "Properties": {
            "aws.glue.catalog.separator": "/",
            "hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"
        }
    },
    {
        "Classification": "spark-hive-site",
        "Properties": {
            "aws.glue.catalog.separator": "/",
            "hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"
        }
    }
]

將組態變更套用到正在執行的叢集

請完成下列步驟:

  1. 開啟 Amazon EMR console (Amazon EMR 主控台)。
  2. 在叢集清單中的 Cluster Id (叢集 ID) 下,選擇要重新設定的作用中叢集。
  3. 在叢集詳細資訊頁面中,選擇 Applications (應用程式) 索引標籤。
  4. Instance group configurations (執行個體群組組態) 下,選取執行個體群組,然後選擇 Reconfigure (重新設定)。
  5. 選擇 Add new configuration (新增組態),然後輸入以下資訊:
    Classification (分類) 中,輸入 hive-site
    Property (屬性) 中,輸入 aws.glue.catalog.separator
    Value (值) 中,輸入 /
    Classification (分類) 中,輸入 hive-site
    Property (屬性) 中,輸入 hive.metastore.client.factory.class
    Value (值) 中,輸入 com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
    Classification (分類) 中,輸入 spark-hive-site
    Property (屬性) 中,輸入 aws.glue.catalog.separator
    Value (值) 中,輸入 /
    Classification (分類) 中,輸入 spark-hive-site
    Property (屬性) 中,輸入 hive.metastore.client.factory.class
    Value (值) 中,輸入 com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
  6. 選取 Apply this configuration to all active instance groups (將此組態套用到所有活動執行個體群組),然後選擇 Save changes (儲存變更)。如需詳細資訊,請參閱重新設定正在執行叢集中的執行個體群組

查詢另一個帳戶中的資料表

**注意:**在 Amazon EMR 6.5 版上執行 Spark 作業時,傳遞組態屬性 spark.sql.catalogImplementation=hive。如需詳細資訊,請參閱 Apache Spark 網站上的動態載入 Spark 屬性

若要查詢不同帳戶中的資料表,請在查詢中指定帳戶。帳戶編號與目錄 ID 相同。例如,若要查詢帳戶 111122223333 中的 demodb.tab1,請執行下列命令。

Hive:

SELECT * FROM `111122223333/demodb.tab1` LIMIT 5;

Spark:

spark.sql("SELECT * FROM `111122223333/demodb`.tt1 LIMIT 5").show()

spark-submit 指令碼中執行上述 Spark 命令,或將其作為筆記本 Shell 命令執行。

您也可以跨兩個目錄聯結資料表。

Hive:

SELECT * FROM `111122223333/demodb.tab1` t1 INNER JOIN `444455556666/demodb.tab2` t2 ON t1.col1 = t2.col2

Spark:

spark.sql(SELECT * FROM `111122223333/demodb.tab1` t1 INNER JOIN `444455556666/demodb.tab2` t2 ON t1.col1 = t2.col2).show()

spark-submit 指令碼中執行上述 Spark 命令,或將其作為筆記本 Shell 命令執行。

存取另一個帳戶中的特定 Data Catalog

將組態變更套用到新叢集

啟動 EMR 叢集時,請新增類似以下範例的組態物件。如需詳細資訊,請參閱建立叢集時設定應用程式

[
    {
        "Classification": "hive-site",
        "Properties": {
            "hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory",
            "hive.metastore.glue.catalogid": "account-id"
        }
    },
    {
        "Classification": "spark-hive-site",
        "Properties": {
            "hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory",
            "hive.metastore.glue.catalogid": "account-id"
        }
    }
]

將組態變更套用到正在執行的叢集

請完成下列步驟:

  1. 開啟 Amazon EMR console (Amazon EMR 主控台)。
  2. 在叢集清單中的 Cluster Id (叢集 ID) 下,選擇要重新設定的作用中叢集。
  3. 在叢集詳細資訊頁面中,選擇 Applications (應用程式) 索引標籤。
  4. Instance group configurations (執行個體群組組態) 下,選取執行個體群組,然後選擇 Reconfigure (重新設定)。
  5. 選擇 Add new configuration (新增組態),然後輸入以下資訊:
    Classification (分類) 中,輸入 hive-site
    Property (屬性) 中,輸入 hive.metastore.glue.catalogid
    Value (值) 中,輸入 account-id
    Classification (分類) 中,輸入 hive-site
    Property (屬性) 中,輸入 hive.metastore.client.factory.class
    Value (值) 中,輸入 com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
    Classification (分類) 中,輸入 spark-hive-site
    Property (屬性) 中,輸入 hive.metastore.glue.catalogid
    Value (值) 中,輸入 account-id
    Classification (分類) 中,輸入 spark-hive-site
    Property (屬性) 中,輸入 hive.metastore.client.factory.class
    Value (值) 中,輸入 com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
  6. 勾選 Apply this configuration to all active instance groups (將此組態套用到所有活動執行個體群組),然後選擇 Save changes (儲存變更)。如需詳細資訊,請參閱重新設定正在執行叢集中的執行個體群組

查詢另一個帳戶中的資料表

若要查詢儲存在指定 Data Catalog 中的資料表,請執行類似下列範例的命令。

Hive:

SELECT * FROM `demodb.tab1` LIMIT 5;

Spark:

spark.sql("SELECT * FROM `demodb`.tt1 LIMIT 5").show()
AWS 官方已更新 6 個月前