hive中的两个字符串(str)切片函数:substr()和split()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive中的两个字符串(str)切片函数:substr()和split()相关的知识,希望对你有一定的参考价值。

参考技术A 该种切片方法有别于python中,永远是向右切片。

Hive:map字段存储和取用 ( str_to_map函数 )

str_to_map(字符串参数, 分隔符1, 分隔符2)

使用两个分隔符将文本拆分为键值对。

分隔符1将文本分成K-V对,分隔符2分割每个K-V对。对于分隔符1默认分隔符是 ‘,‘,对于分隔符2默认分隔符是 ‘=‘

 

例子:

1. 创建map字段

DROP TABLE IF EXISTS tmp.tmp_str_to_map;
CREATE TABLE IF NOT EXISTS tmp.tmp_str_to_map
(
ocolumn string comment ‘原始字段‘, 
rcolumn map<string,string> comment ‘map字段‘
);

  


2. concat + str_to_map函数

用concat + & 取表字段拼接成map类型

insert overwrite table tmp.tmp_str_to_map
SELECT 
concat(‘&crowd:‘, m0.id,‘&clicker:‘,m0.dui_leader,‘&sen:‘,m0.application_type) ocolumn,
str_to_map(concat(‘column1:‘, m0.id,‘&column2:‘,m0.dui_leader,‘&column3:‘,m0.application_type), ‘&‘, ‘:‘) rcolumn
FROM tmp.tmp_adhoc_detail_20180927 m0
limit 1
;

  

3. 取用map里的字段,用[""]即可

select 
rcolumn,
rcolumn["column1"] column1
from tmp.tmp_str_to_map;

 

4. 也可以直接转换取用,而不需要存储字段

SELECT 
  m0.id column1,
  str_to_map(concat(‘column1:‘, m0.id,‘&column2:‘,m0.dui_leader,‘&column3:‘,m0.application_type), ‘&‘, ‘:‘)["column1"] column1_1
from tmp.tmp_adhoc_detail_20180927 m0 
limit 1

  

5. 结果:

rcolumn
column1
{"column1":"1","column2":"李某某","column3":"创新班"} 1


以上是关于hive中的两个字符串(str)切片函数:substr()和split()的主要内容,如果未能解决你的问题,请参考以下文章

R语言str_sub函数从字符串中提取或替换子字符串(substring):str_sub函数指定起始位置和终止位置抽取子字符str_sub函数指定起始位置和终止位置替换子字符串

Python中的split()函数的用法

Hive:map字段存储和取用 ( str_to_map函数 )

Python中的split()函数的使用方法

Python中的split()函数的使用方法

python的字符串列表字典和函数