【问题标题】:How to pass array with same delimiter as of collection field delimiter in Hive?如何传递与 Hive 中的集合字段分隔符相同的分隔符的数组?
【发布时间】:2015-08-05 16:03:15
【问题描述】:

我有一个文件。它包含 4 个字段,其中最后两个字段是数组。所以我在 Hive 中创建了表:

create table testtable(f1 string, f2 string, f3 array<string>) row format delimited fields terminated by ',' collection items terminated by ',';

数据

a,b,c,d
1,sdf,2323,sdaf
1,sdf,34,wer
1,sdf,223,daf
1,sdf,233,af

当我使用以下查询将数据加载到表中时,它会成功加载数据,但结果不正确。它没有加载数组中的最后两个字段,只加载了一个字段。结果如下:

load data inpath 'data/file.txt' into table testtable;

结果

hive> select * from testtable;                                                                                                            
OK
a       b       ["c"]
1       sdf     ["2323"]
1       sdf     ["34"]
1       sdf     ["223"]
1       sdf     ["233"]

所以问题是如何加载具有相同集合分隔符的数组字段中的数据?我的源文件将始终包含相同的分隔符。

【问题讨论】:

    标签: arrays hive


    【解决方案1】:

    Hive 将所有分隔符解释为字段分隔符,因此将您的输入视为 3 4 列。由于您已将表定义为具有 3 列,因此它只是忽略了第四列。我认为您需要将数据读入一个临时的 4 列表,然后从中构建您想要的表:

    create table temptesttable(f1 string, f2 string, f3 string, f4 string) 
    row format delimited fields terminated by ',';
    
    load data inpath 'data/file.txt' into table temptesttable;
    
    create table testtable as select f1, f2, array(f3, f4) as f3 from temptesttable;
    

    【讨论】:

      猜你喜欢
      • 2020-03-29
      • 1970-01-01
      • 2012-02-19
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2020-12-02
      相关资源
      最近更新 更多