如何在hive表中拆分字符串

时间:2017-11-07 17:58:20

标签: hive

我有一张表emp,看起来像这样:

+----------+----+
|customerID|name|
+----------+----+
|         1| Ram|
|         2| Raj|
+----------+----+

我想拆分每条记录的字符串,我想在Hive中输出如下:

+----------+----+---+
|customerID|name|req|
+----------+----+---+
|         1| Ram|  R|
|         1| Ram|  a|
|         1| Ram|  m|
|         1| Ram|   |
|         2| Raj|  R|
|         2| Raj|  a|
|         2| Raj|  j|
|         2| Raj|   |
+----------+----+---+

1 个答案:

答案 0 :(得分:0)

您必须在hive中将name列转换为数组类型,然后展开以获得所需的结果。 例如:

+----------+----+
|customerID|name|
+----------+----+
|         1| Ram|
|         2| Raj|
+----------+----+

select *,explode(split(name,'')) as req from emp

+----------+----+---+
|customerID|name|req|
+----------+----+---+
|         1| Ram|  R|
|         1| Ram|  a|
|         1| Ram|  m|
|         1| Ram|   |
|         2| Raj|  R|
|         2| Raj|  a|
|         2| Raj|  j|
|         2| Raj|   |
+----------+----+---+

删除空格。

select customerID,req  from (select *,explode(split(name,'')) as req from emp) where req != ''

+----------+---+
|customerID|req|
+----------+---+
|         1|  R|
|         1|  a|
|         1|  m|
|         2|  R|
|         2|  a|
|         2|  j|
+----------+---+