如何在Hive shell终端中使用Split功能

时间:2018-02-08 15:23:02

标签: sql shell hive

下面的语句在SQL Workbench(hive)中完全正常:

select split(split('word1;with;some;meaning,;word2,;word3,;', ',;')[0],';')[0];

以上语句产生输出:word1

第1步:

select split('word1;with;some;meaning,;word2,;word3,;', ',;')[0];
output 1: `word1;with;some;meaning`

第2步:

select split(split('word1;with;some;meaning,;word2,;word3,;', ',;')[0],';')[0];

output 2: `word1`

如果我在蜂巢终端中尝试以上操作,我会收到以下错误: hive terminal error

3 个答案:

答案 0 :(得分:0)

您需要使用;屏蔽分号\\,因为分号会在hive CLI中终止字符串:

hive> select split(split('word1\\;with\\;some\\;meaning,\\;word2,\\;word3,\\;', ',\\;')[0],'\\;')[0];
OK
word1
Time taken: 0.276 seconds, Fetched: 1 row(s)

屏蔽它工作正常。 SQL工作台可能会为您工作。

Step1已修复:

 hive> select split('word1\\;with\\;some\\;meaning,\\;word2,\\;word3,\\;', '[,\\;]')[0];
OK
word1

此处'[,\\;]' - 表示, or ;作为分隔符

答案 1 :(得分:0)

如果您只对;终止的第一个令牌感兴趣,那么这可能会有所帮助

select regexp_extract("word1;with;some;meaning,;word2,;","(.*?);(.*)",1)

它适用于Hue但未在CLI上测试

答案 2 :(得分:0)

经过一番研究,我发现了这个解决方案

select split(split(word_list,',\073')[0],'\073')[1];

'\073' is equivalent to ';'.

这符合我的目的。谢谢大家!