HiveQL:合并数组列

时间:2018-12-28 12:48:30

标签: sql hive hiveql

我有一个表,其中的列具有数组,如何编写HiveQL合并数组列?

| id | colA             | colB            | colC 
+----+------------------+-----------------+------------------    
| 1  | ["john", "james"]| ["peter"]       | ["sam","peter"]
| 2  | ["jane"]         | ["doug"]        | ["mary","peter"]
| 3  | ["jan", "james"] | ["peter","mary"]| ["sam","peter"]

编写查询以显示如下数据:

| id | newcol
+----+------------------------------------------------------    
| 1  | ["john", "james", "peter", "sam","peter"]
| 2  | ["jane", "doug", "mary","peter"]
| 3  | ["jan", "james", "peter","mary","sam","peter"]

3 个答案:

答案 0 :(得分:1)

如果要从多个字符串数组中创建一个新的字符串数组,可以使用以下内容(请注意,您需要检查colB和colC是否为空,以避免产生多余的逗号):

split (
concat (

concat_ws(',',colA),
if(size(colB)>0, concat(',', concat_ws(',',colB)), ''    ),
if(size(colC)>0, concat(',', concat_ws(',',colC)), ''    )


),',')

答案 1 :(得分:1)

hive> select split(concat_ws(',',array("john", "james"), array('peter'), array("sam","peter")), ",") as a;
OK
["john","james","peter","sam","peter"]

根据您的情况。

select split(concat_ws(',',ColA, ColB, ColC), ",") as a;

答案 2 :(得分:0)

类似的东西:

Concat or ||

SELECT id, colA||','||colB||','||colC AS newcol FROM myTable

OR

SELECT id, Concat(colA,',',colB,',',colC,) AS newcol FROM myTable