当前位置: 首页 > 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 数据分析

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

 完整代码如下:

select
   t3.uid_1, t3.uid_2
from (select
          t1.ip,
          t1.uid as uid_1,
          t2.uid as uid_2
      from (select uid, ip from log group by uid, ip) t1
               join
               (select uid, ip from log group by uid, ip) t2
      where t1.ip = t2.ip
        and t1.uid < t2.uid) t3
group by t3.uid_1, t3.uid_2
having count(ip) >= 3;

代码分析:

step1: 获取自关联的结果集

select
    t1.ip,
    t1.uid as uid_1,
    t2.uid as uid_2
from (select uid, ip from log group by uid, ip) t1
 join(select uid, ip from log group by uid, ip) t2
 on t1.ip = t2.ip;

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

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

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

select
   t3.uid_1, t3.uid_2
from (select
          t1.ip,
          t1.uid as uid_1,
          t2.uid as uid_2
      from (select uid, ip from log group by uid, ip) t1
               join
               (select uid, ip from log group by uid, ip) t2
      where t1.ip = t2.ip
        and t1.uid < t2.uid) t3
group by t3.uid_1, t3.uid_2
having count(ip) >= 3;

3 小结

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

相关文章:

  • CVE-2022-25487 漏洞复现
  • 排序算法---计数排序
  • 安全基础~通用漏洞5
  • 【Spring2】从重构HelloWorld看Spring的作用
  • 面试经典150题——最小覆盖子串(困难)
  • 微信小程序(四十一)wechat-http的使用
  • Java解决下降路径最小和
  • C++数据结构与算法——双指针法
  • BatchNorm介绍:卷积神经网络中的BN
  • 鸿蒙系统优缺点,能否作为开发者选择
  • MySQL 基础知识(八)之用户权限管理
  • C#,21根火柴棍问题(21 Matchticks Problem)的算法与源代码
  • Netty Review - NioEventLoopGroup源码解析
  • 《CSS 简易速速上手小册》第4章:视觉美学(2024 最新版)
  • uniapp 开发一个密码管理app
  • Python四级考试笔记
  • 【Linux】vim的基本操作与配置(下)
  • html从零开始9:javaScript简介,语句、标识符,变量,JavaScript引入到文件【搬代码】
  • 高级前端面试题及详解
  • 打印最小公倍数
  • 向总书记汇报具身智能发展的“稚辉君”:从期待到兴奋再到备受鼓舞
  • 香港警务处高级助理处长叶云龙升任警务处副处长(行动)
  • 广州海关原党委委员、副关长刘小威被开除党籍
  • 上海浦东单价超10万楼盘228套房源开盘当天售罄,4月已有三个新盘“日光”
  • 财政部下达农业生产防灾救灾资金3.76亿元,支持黄淮海等地抗旱保春播
  • 在上海生活8年,13岁英国女孩把城市记忆写进歌里