hive函数str_to_map

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

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

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

例子:

1. 创建map字段

1

2

3

4

5

6

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字段'

);

  

--------------------------------------------------------------------

如果数据很想kv 直接清洗下数据转成map:

str_to_map(regexp_replace(params,'[\"|\{|\}]',''),'&',':') as params,   ---数据是用& 分割,kv以:分割

--------------------------------------------------------------------


2. concat + str_to_map函数

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

1

2

3

4

5

6

7

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里的字段,用[""]即可

1

2

3

4

select

rcolumn,

rcolumn["column1"] column1

from tmp.tmp_str_to_map;

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

1

2

3

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 <br>limit 1

  

5. 结果:

rcolumn

1

<span style="font-size: 14px"><strong>column1</strong></span>

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

猜你喜欢

转载自blog.csdn.net/m0_38102941/article/details/88561229