在hive中,如果a不为null,如何选择a,否则选择ba和b都是json列中的字段

crcmnpdw  于 2021-06-24  发布在  Hive
关注(0)|答案(5)|浏览(536)

我有一个配置单元表,其中有两列。第一列命名为user\ id,第二列命名为event,其中数据采用json格式。该表的示例如下所示:

user_id, event
1231321, {"ts":1554254647,"items":[{"id":12342,"label1":null,"lable2":a},{"id":2,"label1":"c","lable2":"d"}]}
2131232, {"ts":1524254647,"items":[{"id":12433,"label1":null,"lable2":null},{"id":2,"label1":"c","lable2":"d"}]}

如何编写查询以获取用户标识和标签(如果标签1可用,则为标签1,否则为标签2)。使用上述示例,查询结果将是:

user_id, lables
1231321, [c,a]
2131232, [c,d]
zengzsys

zengzsys1#

--+--+
|测试数据|
+

mxg2im7a

mxg2im7a2#

sql应该是
当get\u json\u object(json\u column,$.items.label1[0]')为空时选择case,然后get\u json\u object(json\u column,$.items.label2[0]')否则get\u json\u object(json\u column,$.items.label1[0]')作为测试数据结束;

select case when  get_json_object('{"ts":1554254647,"items":[{"id":12342,"label1":null,"lable2":"a"},{"id":2,"label1":"c","lable2":"d"}]}','$.items.label1\[0]') is null then get_json_object('{"ts":1554254647,"items":[{"id":12342,"label1":null,"lable2":"a"},{"id":2,"label1":"c","lable2":"d"}]}','$.items.lable2\[0]') else   get_json_object('{"ts":1554254647,"items":[{"id":12342,"label1":null,"lable2":"a"},{"id":2,"label1":"c","lable2":"d"}]}','$.items.label1\[0]')  end as test_data;
0tdrvxhp

0tdrvxhp4#

我不知道json文件是如何工作的。但是,coalesce或nvl是可以满足您需求的功能。

相关问题