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

Alo*_*onG 4 ddl hive amazon-athena

我想从 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')
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

.csv 1:

+----+-------+-------+---------+
| id | name  | price | comment |
+----+-------+-------+---------+
|  1 | shirt |   123 | abc     |
|  2 | shoes |   222 | ddd     |
+----+-------+-------+---------+
Run Code Online (Sandbox Code Playgroud)

.csv 2:

+----+------+-------+-------+---------+
| id | size | price | color |  name   |
+----+------+-------+-------+---------+
|  5 | L    |   100 | red   | shirt   |
|  6 | S    |    55 | white | t-shirt |
+----+------+-------+-------+---------+
Run Code Online (Sandbox Code Playgroud)

我想要的表:

+----+---------+
| id |  name   |
+----+---------+
|  1 | shirt   |
|  2 | shoes   |
|  5 | shirt   |
|  6 | t-shirt |
+----+---------+
Run Code Online (Sandbox Code Playgroud)

我得到的表:

+----+-------+
| id | name  |
+----+-------+
|  1 | shirt |
|  2 | shoes |
|  5 | L     |
|  6 | S     |
+----+-------+
Run Code Online (Sandbox Code Playgroud)

谢谢

Rya*_*uck 5

在我看来,Glue Crawler 没有设置来获取列名并使用它们来定义表的架构,这有点疯狂。我们遇到了这个问题(S3 中同一文件夹中的架构更改),以下是我们解决该问题的方法。

注意- 如果您可以将架构(标头顺序)映射到特定的 S3 路径,则以下解决方案有效。

源数据

我们有四个文件。a.csvb.csv共享相同的模式,而c.csvd.csv具有不同的模式。

$ cat a.csv
a,b
1,2
3,4
$ cat b.csv
a,b
5,6
3,4
$ cat c.csv
a,b,c
1,2,3
4,5,6
$ cat d.csv
a,c,d,x
6,7,8,9
1,2,3,4
Run Code Online (Sandbox Code Playgroud)

这些保存在 S3 中:

$ aws s3 ls s3://example-s3-bucket/
2019-01-04 09:47:42         12 a.csv
2019-01-04 09:49:49         12 b.csv
2019-01-04 09:49:53         18 c.csv
2019-01-04 09:49:56         24 d.csv
Run Code Online (Sandbox Code Playgroud)

每个模式创建一个表

每个架构创建一个表,只需传入相同的 S3 位置即可。

请注意,为了简洁起见,我省略了分隔符和字段分隔符定义。

create external table athena_testing_ab (
  a int,
  b int
)
LOCATION 's3://example-s3-bucket/'
;

create external table athena_testing_c (
  a int,
  b int,
  c int
)
LOCATION 's3://example-s3-bucket/'
;

create external table athena_testing_d (
  a int,
  c int,
  d int,
  x int
)
LOCATION 's3://example-s3-bucket/'
;
Run Code Online (Sandbox Code Playgroud)

使用UNIONs查询所有表

现在,我们查询这 3 个表以及UNION它们全部,过滤每个表的适当 S3 路径。

您可能希望使用正则表达式或子字符串解析来更优雅地过滤$PATH,特别是如果您的存储桶中有数百或数千个文件。

select
  a,
  b,
  null as c,
  null as d,
  null as x
from
  athena_testing_ab
where "$PATH" in  ('s3://example-s3-bucket/a.csv', 's3://example-s3-bucket/b.csv')

union all

select
  a,
  b,
  c,
  null as d,
  null as x
from
  athena_testing_c
where "$PATH" in  ('s3://example-s3-bucket/c.csv')

union all

select
  a,
  null as b,
  c,
  d,
  x
from
  athena_testing_d
where "$PATH" in  ('s3://example-s3-bucket/d.csv')
Run Code Online (Sandbox Code Playgroud)