Hive:过滤映射数组

huangapple 未分类评论45阅读模式
英文:

Hive : Filter array of maps

问题

我有一个Hive表列如下:

col1              	array<map<string,string>>

该列包含以下值:

[
    {'a':1,'b':2},
    {'a':1,'c':3},
    .
    .
    .
]

现在内部映射中的键可以每个项目都不同。我想筛选数组,只获取包含键b的项目。是否可以使用array_contains来实现这样的操作?我尝试过,但是出现了错误:

select col1 from mytab where size(col1) > 0 and array_contains(col1, 'b');
FAILED: SemanticException [Error 10016]: Line 1:196 Argument type mismatch ''b'':
"map<string,string>" expected at function ARRAY_CONTAINS, but "string" is found

这是显而易见的,但我还是尝试了一下。

更新

我尝试了以下操作:

select res from mytab LATERAL VIEW EXPLODE (col1) c as res where 
 size(col1) > 0 and res is not NULL and length(res) > 0;

对于这个操作,我得到了以下结果:

{}
{a=1, b=2}
{a=1, b=2}
{}

这是很好的,但是如何避免空的结果,即{}

更新 2

我成功地移除了空结果,如下:

select res from mytab LATERAL VIEW EXPLODE (col1) c as res where 
     size(col1) > 0 and res['b'] is not NULL and length(res['b']) > 0 res <> '{}';

最后一个问题是,如何将res从字符串转换为正确的映射?我想使用res中的键来添加一些筛选条件。我尝试过:

select str_to_map(res) as st from mytab LATERAL VIEW EXPLODE (col1) c as res where 
         size(col1) > 0 and res is not NULL and length(res) > 0 res <> '{}';

但我得到了:

{"{a=1, b=2}":null}

我应该如何获得正确的映射?我只想能够使用映射中的键进行筛选。目前,该映射被选中为字符串:

select res['b'] from mytab LATERAL VIEW EXPLODE (col1) c as res where 
     size(col1) > 0 and res['b'] is not NULL and length(res['b']) > 0 res['b'] <> '{}' and res['b']=1;

但是我得到了错误:

FAILED: SemanticException [Error 10033]: Line 1:237 [] not valid on non-collection types ''b'':
string
英文:

I have a hive table column as

col1              	array&lt;map&lt;string,string&gt;&gt;

this column contains values as

[
    {&#39;a&#39;:1,&#39;b&#39;:2},
    {&#39;a&#39;:1,&#39;c&#39;:3},
    .
    .
    .
]

now the keys in the inner map can vary per item. I want to filter the array to only get items where key b is present. Can such a thing be achieved using array_contains ? I tried but i got error

select col1 from mytab where size(col1) &gt; 0 and array_contains(col1, &#39;b&#39;);
FAILED: SemanticException [Error 10016]: Line 1:196 Argument type mismatch &#39;&#39;b&#39;&#39;: &quot;map&lt;string,string&gt;&quot; expected at function ARRAY_CONTAINS, but &quot;string&quot; is found

which is obvious but i tried anyway.

Update

i tried

select res from mytab LATERAL VIEW EXPLODE (col1) c as res where 
 size(col1) &gt; 0 and res is not NULL and length(res) &gt; 0;

for this i get

{}
{a=1, b=2}
{a=1, b=2}
{}

which is good but how can i avoid the empty results i.e. {} ?

Update 2

I was able to remove the empty results as

select res from mytab LATERAL VIEW EXPLODE (col1) c as res where 
     size(col1) &gt; 0 and res[&#39;b&#39;] is not NULL and length(res[&#39;b&#39;]) &gt; 0 res &lt;&gt; &#39;{}&#39;;

Last question is, how to convert res from string to a proper map ? i want to have some where condition using keys in res. I tried

select str_to_map(res) as st from mytab LATERAL VIEW EXPLODE (col1) c as res where 
         size(col1) &gt; 0 and res is not NULL and length(res) &gt; 0 res &lt;&gt; &#39;{}&#39;;

but i get

{&quot;{a=1, b=2}&quot;:null}

how can i get a proper map ? i just want to be able to filter using the keys of the map. right now this map is being selected as a string

select res[&#39;b&#39;] from mytab LATERAL VIEW EXPLODE (col1) c as res where 
     size(col1) &gt; 0 and res[&#39;b&#39;] is not NULL and length(res) &gt; 0 res[&#39;b&#39;] &lt;&gt; &#39;{}&#39; and res[&#39;b&#39;]=1;

but i get

FAILED: SemanticException [Error 10033]: Line 1:237 [] not valid on non-collection types ''b'': string

huangapple
  • 本文由 发表于 2020年4月10日 11:02:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61133390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定