zuizui1204
1、concat_ws

它是一个特殊形式的 CONCAT()

concat_ws(分隔符,参数1,参数2.。。。。。。) as 字段

2、split

返回值为一个数组

a.基本用法:

例1:split(\'a,b,c,d\',\',\')

得到的结果:["a","b","c","d"]

b.截取字符串中的某个值:

当然,我们也可以指定取结果数组中的某一项

例2:split(\'a,b,c,d\',\',\')[0]

得到的结果:a

c.特殊字符的处理:

特殊分割符号

regex 为字符串匹配的参数,所以遇到特殊字符的时候需要做特殊的处理

例3:  "." 点

split(\'192.168.0.1\',\'.\')

得到的结果:[]

正确的写法:split(\'192.168.0.1\',\'\\.\')

得到的结果:["192","168","0","1"]

需要注意的是:

当然当split包含在 "" 之中时 需要加4个\

如 hive -e "....  split(\'192.168.0.1\',\'\\\\.\') ... "  不然得到的值是null

同样的 | 等特殊符号也需要做类似 处理。

指定多个分隔符时,多个分隔符使用 | 连接

测试:

drop table test;
create table if not exists test(
name string,
age string,
sex string
)row format delimited fields terminated by \',\'
lines terminated by \'\n\'
stored as textfile;
load data local inpath \'/usr/test.txt\' overwrite into table test;

栗子:想通过分割得到name中的li和i,指定;和-两个分隔符;注意分割之后得到的是一个数组array,下标从0开始

select split(a.name,\'-|;\')[0],split(a.name,\'-|;\')[2] from test a ;

 

分类:

技术点:

相关文章:

  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-08-22
  • 2021-05-01
猜你喜欢
  • 2021-11-13
  • 2021-10-23
  • 2021-11-13
  • 2021-05-04
  • 2021-06-26
  • 2022-02-10
  • 2021-08-14
相关资源
相似解决方案