我有一个 csv 文件,其内容如下,其标题位于第一行。
id,name
1234,Rodney
8984,catherine
现在我可以在配置单元中创建一个表来跳过标题并适当地读取数据。 Hive 中的表
CREATE EXTERNAL TABLE table_id(
`tmp_id` string,
`tmp_name` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'field.delim'=',',
'serialization.format'=',')
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 's3://some-testing/test/data/'
tblproperties ("skip.header.line.count"="1");
Hive 中的结果
select * from table_id;
OK
1234 Rodney
8984 catherine
Time taken: 1.219 seconds, Fetched: 2 row(s)
但是,当我在 pyspark 中使用相同的表(运行相同的查询)时,我什至看到 pyspark 结果中文件的标题,如下所示。
>>> spark.sql("select * from table_id").show(10,False)
+------+---------+
|tmp_id|tmp_name |
+------+---------+
|id |name |
|1234 |Rodney |
|8984 |catherine|
+------+---------+
现在,我如何忽略 pyspark 结果中显示的这些内容。 我知道我们可以读取 csv 文件并添加 .option("header",True) 来实现此目的,但是,我想知道是否有办法在查询表时在 pyspark 中执行类似的操作。
有人可以建议我一种方法吗...提前谢谢🙏!!
u 可以使用以下两个属性: serdies 属性 和 表属性,您将能够通过跳过两个环境中的标头来从 hive 和 Spark 访问表。
CREATE EXTERNAL TABLE `student_test_score_1`(
student string,
age string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'delimiter'=',',
'field.delim'=',',
'header'='true',
'skip.header.line.count'='1',
'path'='hdfs:<path>')
LOCATION
'hdfs:<path>'
TBLPROPERTIES (
'spark.sql.sources.provider'='CSV')
Spark-11374
中的已知问题,已关闭为 won't fix
。
在查询中,您可以使用 where 子句来选择除
'id'
和 'name'
之外的所有记录。
spark.sql("select * from table_id where tmp_id <> 'id' and tmp_name <> 'name'").show(10,False)
#or
spark.sql("select * from table_id where tmp_id != 'id' and tmp_name != 'name'").show(10,False)
另一种方法是使用从 HDFS 和
.option("header","true")
读取文件。
只需在语法中添加选项('header'='true')。 经验: Spark.sql(“使用csv选项创建外部表(order_id字符串,位置字符串,项目字符串,order_date字符串,数量长)('header'='true')位置''”)