创建配置单元 table 并加载数据引用 CSV

Create hive table and load data Quoted CSV

我有一个 csv 文件,它的列在示例中可以包含“,”粗体

样本

23,"we,are",100

23,"you,are",100

要求加载到配置单元 table (col1 int ,col2 array, col3 int) ;

如果您的 Hive 版本为 0.14 及以上,您可以使用 CSV Serde (https://cwiki.apache.org/confluence/display/Hive/CSV+Serde)。 DEFAULT_QUOTE_CHARACTER 对于这个 SerDe 是“

如果您有以前的 Hive 版本,请尝试手动添加此 serde https://github.com/ogrodnek/csv-serde

问题是 Serde 会将您的数组视为字符串。这不是什么大问题,您可以在执行 select 时将列转换为数组,或者为其创建其他视图。

示例:

DROP TABLE my_table;
CREATE EXTERNAL TABLE my_table(col1 int , col2 string, col3 int)
row format SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
stored as textfile;

我创建了文本文件并将其放在 table 位置。

文件内容:

23,"we,are",100
23,"you,are",100

现在,获取数据:

hive> select col1, split(col2,",") as col2, col3 from my_table;
OK
23      ["we","are"]    100
23      ["you","are"]   100

或者您可以创建一个视图:

hive> create view my_table_view as select col1, split(col2,",") as col2, col3 from my_table;
OK
Time taken: 0.427 seconds
hive> select * from my_table_view;
OK
23      ["we","are"]    100
23      ["you","are"]   100
Time taken: 0.369 seconds, Fetched: 2 row(s)

--Select数组元素:

hive> select col1,col2[0] as col2_1, col2[1] as col2_2, col3 from my_table_view;
OK
23      we      are      100
23      you     are     100
Time taken: 0.09 seconds, Fetched: 2 row(s)