【问题标题】:Hive: How to test and find for null map entries?Hive:如何测试和查找空映射条目?
【发布时间】:2012-10-12 11:28:05
【问题描述】:

如果在 Hive 表中使用 map 类型,如何测试空条目(键存在,但值为空)?

有表:

 test1 (id string, m map<string, string>)

我有一些看起来像这样的条目:

id1 {"b":"B","c":null} 
id2 {"b":"B"}

如果我运行查询:

select * from test1 where m["c"] is null;

我会取回两行,因为表达式每次都计算为真。

如何在 key 存在和 value 为 null 之间进行测试?

【问题讨论】:

  • 是的,我添加了自己的答案,但正在寻找反馈和/或其他解决方案

标签: hive


【解决方案1】:

我想出了两个解决方案

要查找映射实际包含特定键且其为空的行:

select * from test1 where array_contains(map_keys(m),'c') and m["c"] is null;

查找值为空的任何键:

select id,k from test1 LATERAL VIEW explode(m) et as k,v where v is null;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多