在Hive中的数组内查询

问题描述

我看过this,但是它对我的数据无效。

我有以下数据:

 1,John,a@com;b@com2,32
 2,Jack,ab@com;c@com2,33

并通过以下方式将它们加载到配置单元中:

create table t7(id int,name string,email Array<string>,age int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION items terminated by ';'
STORED AS textfile;
Load data inpath '/user/maria_dev/7.txt' into table t7;

和选择输出

enter image description here

但是我无法在数组中搜索特定值

enter image description here

enter image description here

那么,我想念什么?

解决方法

您无法使用的原因是,第一个元素之前有空格,因此您必须使用trim

select * from t7 where trim(email[0]) like "%a@%";
CREATE TABLE `t7`(
  `id` int,`name` string,`email` array<string>,`age` int)
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  COLLECTION ITEMS TERMINATED BY '\;'

hive> select * from t7 where trim(email[0])="a@com";
OK
1        John   [" a@com","b@com2"]     32

hive> select * from t7 LATERAL VIEW explode(email) exploded_table as id_email where id_email like "%com2%";
OK
1        John   [" a@com","b@com2"]     32      b@com2
2        Jack   [" ab@com","c@com2"]    33      c@com2