gpt4 book ai didi

hive - 如何通过列名称而不是列顺序从多个CSV在AWS Athena中创建表

转载 作者:行者123 更新时间:2023-12-03 18:12:16 26 4
gpt4 key购买 nike

我想从S3中存储的多个CSV文件在AWS Athena中创建一个表。

CSV的标题行带有列名。
我的问题是,每个CSV中的列顺序都不同,我想按列名获取列。

当我在Athena中尝试普通的CREATE TABLE时,我得到了前两列。

CREATE EXTERNAL TABLE `test`(
`id` string,
`name` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
'escapeChar'='\\',
'quoteChar'='\"',
'separatorChar'=',')
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
's3://...'
TBLPROPERTIES (
'has_encrypted_data'='false')


这是一个例子:

CSV 1:

+----+-------+-------+---------+
| id | name | price | comment |
+----+-------+-------+---------+
| 1 | shirt | 123 | abc |
| 2 | shoes | 222 | ddd |
+----+-------+-------+---------+


CSV 2:

+----+------+-------+-------+---------+
| id | size | price | color | name |
+----+------+-------+-------+---------+
| 5 | L | 100 | red | shirt |
| 6 | S | 55 | white | t-shirt |
+----+------+-------+-------+---------+


我想要的表:

+----+---------+
| id | name |
+----+---------+
| 1 | shirt |
| 2 | shoes |
| 5 | shirt |
| 6 | t-shirt |
+----+---------+


我得到的表:

+----+-------+
| id | name |
+----+-------+
| 1 | shirt |
| 2 | shoes |
| 5 | L |
| 6 | S |
+----+-------+


谢谢

最佳答案

我将为不同的CSV使用两个不同的表(您将需要将CSV存储在不同的文件夹中)。

最终,要获取两个CSV的ID,名称结构,我将使用VIEW,它将不同表中的必要列合并在一起。

关于hive - 如何通过列名称而不是列顺序从多个CSV在AWS Athena中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com