从具有不同记录格式的文件为csv文件创建外部HIVE表

时间:2017-08-01 18:14:40

标签: hive hdfs

我有一个CSV文件,其中包含由第一列值定义的不同记录格式: 样本数据:

"EL","XXXXXXX", 2017-07-17
"EH","XXXXXXX",1,2017-07-17,"AAA"
"BI","XXXXXXX","AAA","BBBB"

在这种情况下,我将获得具有3种已定义记录类型的文件。 有没有办法将其加载到不同的配置表?

1 个答案:

答案 0 :(得分:1)

<强>演示

create table el (s1 string,d1 date);
create table eh (s1 string,i1 int,dt1 date,s2 string);
create table bi (s1 string,s2 string,s3 string);
create external table myfile 
(
    c1  string
   ,c2  string
   ,c3  string
   ,c4  string
   ,c5  string
)

row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
with serdeproperties 
(
    'separatorChar' = ','
   ,'quoteChar'     = '"'
   ,'escapeChar'    = '\\'
)  
stored as textfile
;
select * from myfile;

+-----+----------+--------------+-------------+-------+
| c1  |    c2    |      c3      |     c4      |  c5   |
+-----+----------+--------------+-------------+-------+
| EL  | XXXXXXX  |  2017-07-17  | NULL        | NULL  |
| EH  | XXXXXXX  | 1            | 2017-07-17  | AAA   |
| BI  | XXXXXXX  | AAA          | BBBB        | NULL  |
+-----+----------+--------------+-------------+-------+
from myfile
insert into el select c2,c3       where c1='EL'
insert into eh select c2,c3,c4,c5 where c1='EH'
insert into bi select c2,c3,c4    where c1='BI'
;
select * from el;

+----------+-------------+
|    s1    |     d1      |
+----------+-------------+
| XXXXXXX  | 2017-07-17  |
+----------+-------------+
select * from eh;

+----------+-----+-------------+------+
|    s1    | i1  |     dt1     |  s2  |
+----------+-----+-------------+------+
| XXXXXXX  | 1   | 2017-07-17  | AAA  |
+----------+-----+-------------+------+
select * from bi;

+----------+------+-------+
|    s1    |  s2  |  s3   |
+----------+------+-------+
| XXXXXXX  | AAA  | BBBB  |
+----------+------+-------+