当前位置: 首页 > news >正文

HiveSQL——共同使用ip的用户检测问题【自关联问题】

注:参考文章:

SQL 之共同使用ip用户检测问题【自关联问题】-HQL面试题48【拼多多面试题】_hive sql 自关联-CSDN博客文章浏览阅读810次。0 问题描述create table log( uid char(10), ip char(15), time timestamp);insert into log valuesinsert into log values('a', '124', '2019-08-07 12:0:0'),('a', '124', '2019-08-07 13:0:0'),('b', '124', '2019-08-08 12:0:0'),('c', '124', '2019-0._hive sql 自关联https://blog.csdn.net/godlovedaniel/article/details/119858751

0 问题描述

1 数据准备

create table log
(uid string,ip string,login_time string
)row format delimited
fields terminated by '\t';insert into log values
('a', '124', '2019-08-07 12:00:00'),
('a', '124', '2019-08-07 13:00:00'),
('b', '124', '2019-08-08 12:00:00'),
('c', '124', '2019-08-09 12:00:00'),
('a', '174', '2019-08-10 12:00:00'),
('b', '174', '2019-08-11 12:00:00'),
('a', '194', '2019-08-12 12:00:00'),
('b', '194', '2019-08-13 13:00:00'),
('c', '174', '2019-08-14 12:00:00'),
('c', '194', '2019-08-15 12:00:00');

2 数据分析

   共同使用问题,一般此类题型都需要一对多,该问题的解决核心逻辑是自关联

 完整代码如下:

selectt3.uid_1, t3.uid_2
from (selectt1.ip,t1.uid as uid_1,t2.uid as uid_2from (select uid, ip from log group by uid, ip) t1join(select uid, ip from log group by uid, ip) t2where t1.ip = t2.ipand t1.uid < t2.uid) t3
group by t3.uid_1, t3.uid_2
having count(ip) >= 3;

代码分析:

step1: 获取自关联的结果集

selectt1.ip,t1.uid as uid_1,t2.uid as uid_2
from (select uid, ip from log group by uid, ip) t1join(select uid, ip from log group by uid, ip) t2on t1.ip = t2.ip;

step2: 由于数据会两两出现,所以a,b和 b,a实际上是一样的,需要过滤掉这部分重复数据,只需要选出 t1.uid < t2.uid,即过滤掉a,b这组数据。hive中不支持不等连接,故使用where语句

selectt1.ip,t1.uid as uid_1,t2.uid as uid_2
from (select uid, ip from log group by uid, ip) t1join (select uid, ip from log group by uid, ip) t2where t1.ip = t2.ip and t1.uid < t2.uid;

step3:按照组合键分组,并过滤出符合条件的用户

selectt3.uid_1, t3.uid_2
from (selectt1.ip,t1.uid as uid_1,t2.uid as uid_2from (select uid, ip from log group by uid, ip) t1join(select uid, ip from log group by uid, ip) t2where t1.ip = t2.ipand t1.uid < t2.uid) t3
group by t3.uid_1, t3.uid_2
having count(ip) >= 3;

3 小结

    本案例题型属于:“共同xx”,例如:共同好友、互相认识、共同使用等。遇到这类关键字的时候,往往可以采用自关联的方式解决。(笛卡尔积:一对多;去重取一)

http://www.lryc.cn/news/298766.html

相关文章:

  • 猫头虎分享已解决Bug ‍ || 修改mongodb3.0副本集用户密码遇到 BeanDefinitionParsingException
  • 如何将ChatGPT升级到4.0版本?如何充值?
  • conda 相关命令
  • 探索现代Web前端开发框架:选择最适合你的工具
  • 记录一下,我使用stm32实现pwm波输入,以及对频率和占空比的计算,同时通过串口输出(实现-重要)
  • Spring Cloud使用ZooKeeper作为注册中心的示例
  • 【项目日记(九)】项目整体测试,优化以及缺陷分析
  • JavaScript 设计模式之外观模式
  • 一、基础数据结构——2.队列——3.双端队列和单调队列2
  • Stable Diffusion 模型下载:Samaritan 3d Cartoon(撒玛利亚人 3d 卡通)
  • 【软件工程导论】实验二——编制数据字典(数字化校园系统案例分析)
  • 耳机壳UV树脂制作私模定制耳塞适合什么样的人使用呢?
  • 第三百一十回
  • 海量数据处理商用短链接生成器平台 - 4
  • 基于CNN+LSTM深度学习网络的时间序列预测matlab仿真
  • 如何控制系统安全 或 控制流氓软件
  • 【Docker】Docker Container(容器)
  • Amazon CodeWhisperer 免费 AI 代码生成助手体验分享
  • Spring Cloud Gateway 网关路由
  • 【Spring学习】Spring Data Redis:RedisTemplate、Repository、Cache注解
  • C语言:内存函数
  • Go+:一种简单而强大的编程语言
  • 【开源】SpringBoot框架开发数字化社区网格管理系统
  • Lua可变参数函数
  • Nginx实战:3-日志按天分割
  • springmvc中的数据提交方式
  • unity2017 遇到visual studio 2017(社区版) 30日试用期到了
  • Netty应用(六) 之 异步 Channel
  • STM32CubeMx+MATLAB Simulink串口输出实验,UART/USART串口测试实验
  • 【51单片机】串口通信实验(包括波特率如何计算)