首页 > 解决方案 > 我们如何在 Hadoop 中截断空格后的文本?

问题描述

我有一列说column_1,它的值是:

abc 12edf
hbnm 847
47sf hg41

我需要如下输出:

abc
hbnm
47sf

PS:我对数据库有只读访问权限

标签: sqlstringhadoophivehiveql

解决方案


用于regexp_extract(col,'^(.*?)\\s',1)提取正则表达式中空格(第 1 组)之前字符串开头的所有内容。

'^(.*?)\\s'方法:

^- 字符串锚点的开头 (.*?)- 任意字符任意次数 \\s- 空格

Demo:
with your_table as (--use your_table instead of this
select stack (3,
'abc  12edf',
'hbnm 847',
'47sf hg41'
) as str
)

select regexp_extract (str,'^(.*?)\\s',1) as result_str 
  from your_table s

结果:

abc
hbnm
47sf

另一种可能的解决方案是使用split

select split (str,' ')[0] as result_str

还有一种使用instr+的解决方案substr

select substr(str,1,instr(str,' ')-1)

推荐阅读