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

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”,例如:共同好友、互相认识、共同使用等。遇到这类关键字的时候,往往可以采用自关联的方式解决。(笛卡尔积:一对多;去重取一)


http://www.kler.cn/news/233000.html

相关文章:

  • 【计算机网络基础篇】学习笔记系列之二《游览器输入URL后发生了什么?》
  • 语义分割任务的准确率计算:基于PyTorch实现
  • vscode +git +gitee 文件管理
  • archlinux 使用 electron-ssr 代理 socks5
  • mybatis-plus的批量修改源码遇到的问题
  • Lua metatable metamethod
  • 网络游戏租用价格表,一年、1个月收费明细表
  • 按键扫描16Hz-单片机通用模板
  • Docker-CE 国内源国内镜像
  • div 2_div 3_ div 4_刷题刷题刷题
  • Linux线程 分离和同步与互斥 条件变量
  • 华为 Huawei 交换机 黑洞MAC地址的作用和配置示例
  • JMM(Java内存模型)
  • 系统架构24 - 软件架构设计(3)
  • 已解决org.springframework.aop.AopInvocationException异常的正确解决方法,亲测有效!!!
  • Python中的嵌套字典访问与操作详解
  • VR全景技术可以应用在哪些行业,VR全景技术有哪些优势
  • 无心剑汉英双语诗《龙年大吉》
  • Docker概述
  • 《MySQL 简易速速上手小册》第4章:数据安全性管理(2024 最新版)
  • LabVIEW热电偶自动校准系统
  • FastDFS安装并整合Openresty
  • 【SpringBoot】JWT令牌
  • 【正式】今年第一篇CSDN(纯技术教学)
  • python29-Python的运算符之in运算符
  • Redis实现秒杀
  • SpringCloud-Ribbon实现负载均衡
  • Linux操作系统基础(六):Linux常见命令(一)
  • Python进阶:标准库
  • PySpark(三)RDD持久化、共享变量、Spark内核制度,Spark Shuffle、Spark执行流程