字符串映射转换配置单元 [英] String to Map Conversion Hive

查看:147
本文介绍了字符串映射转换配置单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张有四列的表。

C1    C2    C3    C4
--------------------
x1    y1    z1    d1
x2    y2    z2    d2

现在我想将其转换为具有键和值对的地图数据类型,并加载到单独的表中。

Now I want convert it into map data type having key and value pairs and load into separate table.

create table test
(
   level map<string,string>
)
row format delimited
COLLECTION ITEMS TERMINATED BY '&'
map keys terminated by '=';

现在我正在使用下面的sql加载数据。

Now I am using below sql to load data.

insert overwrite table test
select str_to_map(concat('level1=',c1,'&','level2=',c2,'&','level3=',c3,'&','level4=',c4) from input;

在表格上选择查询。

select * from test;
{"level1":"x1","level2":"y1","level3":"z1","level4":"d1=\\"}
{"level1":"x2","level2":"y2","level3":"z2","level4":"d2=\\"}

没有得到为什么我在最后一个值得到额外的= \ \。

I didn't get why I am getting extra "=\ \" in last value.

我仔细检查数据,但问题仍然存在。

I double check data but the issue persist.

你可以帮忙吗?

推荐答案

str_to_map(text,delimiter1,delimiter2) - 创建一个通过解析文本绘制地图
使用两个分隔符将文本分割成键值对,第一个分隔符分隔对,第二个分隔符执行键a nd值,如果只给出一个参数,则使用默认分隔符:','作为delimiter1,'='作为delimiter2。

str_to_map(text, delimiter1, delimiter2) - Creates a map by parsing text Split text into key-value pairs using two delimiters. The first delimiter seperates pairs, and the second delimiter sperates key and value. If only one parameter is given, default delimiters are used: ',' as delimiter1 and '=' as delimiter2.

您可以通过运行此信息获取此信息命令:

You can get this info by running this command:

describe function extended str_to_map

在您的语法中有两个错误:

In your syntax there are two errors:

insert overwrite table test
select str_to_map(concat('level1=',c1,'&','level2=',c2,'&','level3=',c3,'&','level4=',c4) from input;

首先是一个括号丢失。

其次是基本上没有错误,你没有给出分隔符,所以这个函数使用了分隔符的默认值,这就是为什么你得到','在您的结果中。

Second is, its not an error basically, you have not given the delimiters so the function is taking default values for delimiters, That's why your are getting ',' in your result.

要获取当前格式的输出,您应该尝试此查询:

To get the output in current format you should try this query:

insert overwrite table test
select str_to_map(concat('level1=',c1,'&','level2=',c2,'&','level3=',c3,'&','level4=',c4),'&','=') from input;

希望这有助于... !!!

Hope this helps...!!!

这篇关于字符串映射转换配置单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆