如何在 hiveql 中的地图类型上使用 LIKE 运算符?
How can I use the LIKE operator on a map type in hiveql?
我想 select 两列 students
:
id_test int
number map<string,string>
我尝试使用 LIKE 运算符执行以下命令:
SELECT id_test ,number FROM students WHERE id_test = 123456 AND number LIKE %MOBILE%;
并得到这个错误:
FAILED: SemanticException [Error 10014]: Line 1:82 Wrong arguments ''%MOBILE%'': No
matching method for class org.apache.hadoop.hive.ql.udf.UDFLike with
(map<string,string>, string). Possible choices: _FUNC_(string, string)
复制代码:
CREATE TABLE students(id_test INT, number MAP<STRING, STRING>) ROW FORMAT DELIMITED FIELDS TERMINATED by
'|' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':';
INSERT INTO TABLE students SELECT 123434, map('MOBILE','918-555-1162') FROM existingtable LIMIT 1;
INSERT INTO TABLE students SELECT 245678, map('WORK','806-555-4722') FROM existingtable LIMIT 1;
展开地图,然后您可以使用 LIKE 过滤键。如果您希望每个 id_test, number
获得单行,即使有许多键满足 LIKE 条件,请使用 GROUP BY
或 DISTINCT
.
演示:
with students as (--one record has many MOBILE* keys in the map
SELECT 123434 id_test , map('MOBILE','918-555-1162', 'OFFICE', '123456', 'MOBILE2', '5678') number union all
SELECT 245678, map('WORK','806-555-4722')
)
select s.id_test, s.number
from students s
lateral view explode(number) n as key,value
where n.key like '%MOBILE%'
group by s.id_test, s.number
结果:
123434 {"MOBILE":"918-555-1162","MOBILE2":"5678","OFFICE":"123456"}
如果您确切地知道密钥 'MOBILE' 那么最好像这样过滤:where number['MOBILE'] is not null
,不要爆炸。
select *
from students
where concat_ws(' ',map_keys(number)) like '%MOBILE%'
下面贴在这里只是为了让你看看与 LIKE 一起使用的表达式是怎样的:
select concat_ws(' ',map_keys(number))
from students
+-----------------------+
| _c0 |
+-----------------------+
| MOBILE MOBILE2 OFFICE |
| WORK |
+-----------------------+
我想 select 两列 students
:
id_test int
number map<string,string>
我尝试使用 LIKE 运算符执行以下命令:
SELECT id_test ,number FROM students WHERE id_test = 123456 AND number LIKE %MOBILE%;
并得到这个错误:
FAILED: SemanticException [Error 10014]: Line 1:82 Wrong arguments ''%MOBILE%'': No
matching method for class org.apache.hadoop.hive.ql.udf.UDFLike with
(map<string,string>, string). Possible choices: _FUNC_(string, string)
复制代码:
CREATE TABLE students(id_test INT, number MAP<STRING, STRING>) ROW FORMAT DELIMITED FIELDS TERMINATED by
'|' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':';
INSERT INTO TABLE students SELECT 123434, map('MOBILE','918-555-1162') FROM existingtable LIMIT 1;
INSERT INTO TABLE students SELECT 245678, map('WORK','806-555-4722') FROM existingtable LIMIT 1;
展开地图,然后您可以使用 LIKE 过滤键。如果您希望每个 id_test, number
获得单行,即使有许多键满足 LIKE 条件,请使用 GROUP BY
或 DISTINCT
.
演示:
with students as (--one record has many MOBILE* keys in the map
SELECT 123434 id_test , map('MOBILE','918-555-1162', 'OFFICE', '123456', 'MOBILE2', '5678') number union all
SELECT 245678, map('WORK','806-555-4722')
)
select s.id_test, s.number
from students s
lateral view explode(number) n as key,value
where n.key like '%MOBILE%'
group by s.id_test, s.number
结果:
123434 {"MOBILE":"918-555-1162","MOBILE2":"5678","OFFICE":"123456"}
如果您确切地知道密钥 'MOBILE' 那么最好像这样过滤:where number['MOBILE'] is not null
,不要爆炸。
select *
from students
where concat_ws(' ',map_keys(number)) like '%MOBILE%'
下面贴在这里只是为了让你看看与 LIKE 一起使用的表达式是怎样的:
select concat_ws(' ',map_keys(number))
from students
+-----------------------+
| _c0 |
+-----------------------+
| MOBILE MOBILE2 OFFICE |
| WORK |
+-----------------------+