【问题标题】:How to ignore headers in PySpark when using Athena and AWS Glue Data Catalog使用 Athena 和 AWS Glue 数据目录时如何忽略 PySpark 中的标头
【发布时间】:2018-05-16 07:46:58
【问题描述】:

假设我有一个这样的 CSV 文件:

"Col1Name", "Col2Name"
"a", "b"
"c", "d"

假设我在 Athena 中发出以下 CREATE EXTERNAL TABLE 命令:

CREATE EXTERNAL TABLE test.sometable (
   col1name string,
   col2name string
) 
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
with serdeproperties (
   'separatorChar' = ',',
   'quoteChar' = '\"',
   'escapeChar' = '\\'
) 
stored as textfile
location 's3://somebucket/some/path/'
tblproperties("skip.header.line.count"="1")

然后我发出以下 SELECT:

SELECT * FROM test.sometable

我希望得到以下结果:

+----------+----------+
|  col1name|  col2name|
+----------+----------+
|         a|         b|
|         c|         d|
+----------+----------+

...果然,这正是我得到的。

在使用 Spark 中的 AWS Glue 元数据目录的 EMR 集群上,我在 pyspark REPL 中发出以下命令:

a = spark.sql("select * from test.sometable")
a.show()

我希望收到相同的输出,但相反,我得到了:

+----------+----------+
|  col1name|  col2name|
+----------+----------+
|  col1name|  col2name|
|         a|         b|
|         c|         d|
+----------+----------+

显然,Athena 尊重“skip.header.line.count”tblproperty,但 PySpark 似乎忽略了它。

如何让 PySpark 像 Athena 一样忽略此标题行?

【问题讨论】:

标签: pyspark amazon-emr pyspark-sql aws-glue


【解决方案1】:

这两种方法中的任何一种都应该对您有所帮助:

(1)在参数中设置要跳过的标题行数:

'skip.header.line.count'='1'

(2) 或者,在选择查询中使用 where 子句过滤该行。说:

SELECT * FROM test.sometable where col1name <> 'col1name'

【讨论】:

  • 'skip.header.line.count'='1' 不适用于 pyspark 例如在 EMR 中,它使用胶水作为元存储。这是 hive 上的胶水实现中的一个大错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 1970-01-01
  • 2018-09-26
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多