首页 > 解决方案 > Spark 2.3.1 AWS EMR 不返回某些列的数据,但在 Athena/Presto 和 Spectrum 中有效

问题描述

我在 AWS EMR (Python 2.7.14) 上的 Spark 2.3.1 上使用 PySpark

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL data source example") \
    .config("hive.metastore.client.factory.class", "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory") \
    .config("hive.exec.dynamic.partition", "true") \
    .config("hive.exec.dynamic.partition.mode", "nonstrict") \
    .config("spark.debug.maxToStringFields", 100) \
    .enableHiveSupport() \
    .getOrCreate()


spark.sql('select `message.country` from datalake.leads_notification where `message.country` is not null').show(10)

这不返回任何数据,找到 0 行。上表中每一行的每个值都返回 Null。数据存储在PARQUET 中。

当我在 AWS Athena/Presto 或 AWS Redshift Spectrum 上运行相同的 SQL 查询时,我会正确返回所有列数据(大多数列值不为空)。

这是返回正确数据的 Athena SQL 和 Redshift SQL 查询:

select "message.country" from datalake.leads_notification where "message.country" is not null limit 10;

我在所有情况下都使用 AWS Glue 目录。上面的列未分区,但表在其他列上分区。我尝试使用修复表,它没有帮助。即 MSCK REPAIR TABLE datalake.leads_notification

我尝试了 Schema Merge = True 像这样:

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL data source example") \
    .config("hive.metastore.client.factory.class", "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory") \
    .config("hive.exec.dynamic.partition", "true") \
    .config("spark.sql.parquet.mergeSchema", "true") \
    .config("hive.exec.dynamic.partition.mode", "nonstrict") \
    .config("spark.debug.maxToStringFields", 200) \
    .enableHiveSupport() \
    .getOrCreate()

没有区别,即使有些不为空,一列的每个值仍然为空。

此列作为最后一列添加到表中,因此大多数数据确实为空,但有些行不为空。该列最终列在目录中的列列表中,位于分区列的正上方。

尽管如此,Athena/Presto 可以检索所有非空值,Redshift Spectrum 也是如此,但是 EMR Spark 2.3.1 PySpark 将此列的所有值显示为“空”。正确检索 Spark 中的所有其他列。

任何人都可以帮我调试这个问题吗?

由于输出格式,Hive Schema 很难在此处剪切和粘贴。

***CREATE TABLE datalake.leads_notification(
  message.environment.siteorigin string, 
  dcpheader.dcploaddateutc string, 
  message.id int, 
  message.country string, 
  message.financepackage.id string, 
  message.financepackage.version string)
PARTITIONED BY ( 
  partition_year_utc string, 
  partition_month_utc string, 
  partition_day_utc string, 
  job_run_guid string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
  's3://blahblah/leads_notification/leads_notification/'
TBLPROPERTIES (
  'CrawlerSchemaDeserializerVersion'='1.0', 
  'CrawlerSchemaSerializerVersion'='1.0', 
  'UPDATED_BY_CRAWLER'='weekly_datalake_crawler', 
  'averageRecordSize'='3136', 
  'classification'='parquet', 
  'compressionType'='none', 
  'objectCount'='2', 
  'recordCount'='897025', 
  'sizeKey'='1573529662', 
  'spark.sql.create.version'='2.2 or prior', 
  'spark.sql.sources.schema.numPartCols'='4', 
  'spark.sql.sources.schema.numParts'='3', 
  'spark.sql.sources.schema.partCol.0'='partition_year_utc', 
  'spark.sql.sources.schema.partCol.1'='partition_month_utc', 
  'spark.sql.sources.schema.partCol.2'='partition_day_utc', 
  'spark.sql.sources.schema.partCol.3'='job_run_guid', 
  'typeOfData'='file')***

最后 3 列在 Spark 中都有相同的问题:

message.country string, 
message.financepackage.id string, 
message.financepackage.version string

所有使用相同目录的 Athena/Presto 和 Redshift Spectrum 都返回 OK。

我为我的编辑道歉。

谢谢你

标签: apache-sparkamazon-emr

解决方案


进行第 5 步模式检查: http ://www.openkb.info/2015/02/how-to-build-and-use-parquet-tools-to.html

我敢打赌,镶木地板定义中的这些新列名要么是大写的(而其他列名是小写的),要么是镶木地板定义中的新列名要么是小写的(而其他列名是大写的)

请参阅阅读镶木地板文件的 Spark 问题 https://medium.com/@an_chee/why-using-mixed-case-field-names-in-hive-spark-sql-is-a-bad-idea-95da8b6ec1e0


推荐阅读