如何将Mysql查询转换为Hive

问题描述

我有这张桌子:

for (var i = data.length-1; i >= 0; i--) {
    if (!data[i]) {
        data.splice(i,1);
    }
}

我使用此查询选择最频繁的IP地址:

CREATE TABLE ip_logs (
  `ip_address` VARCHAR(11),`start_date` VARCHAR(11),`end_date` VARCHAR(11),`loc_id` INTEGER
);

INSERT INTO ip_logs
  (`ip_address`,`start_date`,`end_date`,`loc_id`)
VALUES
 ('120.0.53.21','2020-01-03','2020-01-09','5'),('198.5.273.2','2020-01-10','2020-01-14','4'),('100.36.33.1','2020-02-01','2020-02-02',('198.0.47.33','2020-02-22','2020-02-24','2'),('122.8.0.11','2020-02-25','2020-02-30','2020-03-10','2020-03-17','2020-03-18','2020-03-23','2020-03-04','2020-03-09','3'),('106.25.12.2','2020-03-24','2020-03-30','1');

这在MysqL8.0中有效。但是,这在Hive中也不起作用。我收到此错误

select ( select ip_address from ip_logs t2 where t2.loc_id = t1.loc_id group by ip_address order by count(*) desc limit 1) from ip_logs t1 group by loc_id

预期输出为:

cannot recognize input near 'select' 'ip_address' 'from' in expression specification

解决方法

您可以尝试使用k <= n窗口功能

row_number()