首页 > 其他分享 >hive 求各直播间同时在线最大人数

hive 求各直播间同时在线最大人数

时间:2022-11-19 11:47:53浏览次数:37  
标签:同时在线 12 06 14 hive 2021 time 直播间 id

需求描述

需求有以下数据

create table temp_online_data
(
    room_id    varchar(2) comment '直播间ID',
    anchor_id  varchar(5) comment '播主ID',
    start_time varchar(20) comment '上播时间',
    end_time   varchar(20) comment '下播时间'
) row format delimited fields terminated by ','
    stored as orc
    tblproperties ("orc.compress" = "snappy");
insert into temp_online_data(room_id, anchor_id, start_time, end_time)
values ('0', '1004', '2021-06-14 13:15:12', '2021-06-14 20:12:12'),
       ('0', '1002', '2021-06-14 15:12:12', '2021-06-14 16:12:12'),
       ('0', '1005', '2021-06-14 15:18:12', '2021-06-14 20:12:12'),
       ('0', '1001', '2021-06-14 20:12:12', '2021-06-14 23:12:12'),
       ('0', '1006', '2021-06-14 21:12:12', '2021-06-14 23:15:12'),
       ('0', '1007', '2021-06-14 22:12:12', '2021-06-14 23:10:12'),
       ('1', '1001', '2021-06-14 12:12:12', '2021-06-14 18:12:12'),
       ('1', '1003', '2021-06-14 13:12:12', '2021-06-14 16:12:12'),
       ('1', '1004', '2021-06-14 13:15:12', '2021-06-14 20:12:12'),
       ('1', '1002', '2021-06-14 15:12:12', '2021-06-14 16:12:12');

现在要求出每个直播间同时在线的播主人数;

需求实现

1、将数据分类,在开始数据后添加 +1 表示有主播上线,在结束时间添加 -1,表示主播下线

select room_id, anchor_id, start_time action_time, 1 user_num
from temp_online_data
union all
select room_id, anchor_id, end_time action_time, -1 user_num
from temp_online_data;
0    1004    2021-06-14 13:15:12    1
0    1002    2021-06-14 15:12:12    1
0    1005    2021-06-14 15:18:12    1
0    1001    2021-06-14 20:12:12    1
0    1006    2021-06-14 21:12:12    1
0    1007    2021-06-14 22:12:12    1
1    1001    2021-06-14 12:12:12    1
1    1003    2021-06-14 13:12:12    1
1    1004    2021-06-14 13:15:12    1
1    1002    2021-06-14 15:12:12    1
0    1004    2021-06-14 20:12:12    -1
0    1002    2021-06-14 16:12:12    -1
0    1005    2021-06-14 20:12:12    -1
0    1001    2021-06-14 23:12:12    -1
0    1006    2021-06-14 23:15:12    -1
0    1007    2021-06-14 23:10:12    -1
1    1001    2021-06-14 18:12:12    -1
1    1003    2021-06-14 16:12:12    -1
1    1004    2021-06-14 20:12:12    -1
1    1002    2021-06-14 16:12:12    -1

2、按照时间 计算每个直播间累加人数

select room_id, anchor_id, action_time, sum(user_num) over (partition by room_id order by action_time) user_nums
from (select room_id, anchor_id, start_time action_time, 1 user_num
      from temp_online_data
      union all
      select room_id, anchor_id, end_time action_time, -1 user_num
      from temp_online_data) y;
1    1001    2021-06-14 12:12:12    1
1    1003    2021-06-14 13:12:12    2
1    1004    2021-06-14 13:15:12    3
1    1002    2021-06-14 15:12:12    4
1    1003    2021-06-14 16:12:12    2
1    1002    2021-06-14 16:12:12    2
1    1001    2021-06-14 18:12:12    1
1    1004    2021-06-14 20:12:12    0
0    1004    2021-06-14 13:15:12    1
0    1002    2021-06-14 15:12:12    2
0    1005    2021-06-14 15:18:12    3
0    1002    2021-06-14 16:12:12    2
0    1001    2021-06-14 20:12:12    1
0    1004    2021-06-14 20:12:12    1
0    1005    2021-06-14 20:12:12    1
0    1006    2021-06-14 21:12:12    2
0    1007    2021-06-14 22:12:12    3
0    1007    2021-06-14 23:10:12    2
0    1001    2021-06-14 23:12:12    1
0    1006    2021-06-14 23:15:12    0

3、分组求最大值

select room_id, max(user_nums) user_nums_max
from (select room_id, anchor_id, action_time, sum(user_num) over (partition by room_id order by action_time) user_nums
      from (select room_id, anchor_id, start_time action_time, 1 user_num
            from temp_online_data
            union all
            select room_id, anchor_id, end_time action_time, -1 user_num
            from temp_online_data) y) y
group by room_id;
1    4
0    3

标签:同时在线,12,06,14,hive,2021,time,直播间,id
From: https://www.cnblogs.com/wdh01/p/16905746.html

相关文章

  • Hive2Mysql
    #!/bin/bashset-ebin=`dirname"$0"`bin=`cd$bin;pwd`functionusage(){echoecho"Usage:-f:必选,执行文件的路径文件内容:-h必选,mysqlhost-P......
  • python连接hive
    安装"""pipinstallpyhs2等待这个模块安装完成之后不要关闭命令行,接着在新的一行去执行命令。此时这个命令的作用是开启hive服务,否则python程序无法成功连接,命令如下:......
  • 聊聊Hive数据血缘——从Atlas没有列级血缘的Bug讲起
    前几天,Datahub提供了最新的字段级别数据血缘功能,很多朋友迫不及待想对比一下Datahub的字段级血缘与Atlas的区别。这个时候问题来了,在Atlas收集Hive血缘的时候,由于部分版本......
  • (转)hive中NULL值问题
    原文:https://blog.csdn.net/jiguanglong/article/details/106427078问题描述源端数据oracle数据库,通过cdm迁移工具将数据迁移到目标端hive。在oracle中的NULL值迁移到hive......
  • 【Spark】java.lang.NoSuchMethodException: org.apache.hadoop.hive.ql.metadata.Hiv
    2/11/1419:02:23ERROR[main]SparkUncaughtExceptionHandler:UncaughtexceptioninthreadThread[main,5,main]java.lang.NoSuchMethodException:org.apache.hado......
  • Hive查询的18种方式,你都学会了吗?
    持之以恒,贵在坚持,每天进步一点点!前言        大家一定对Hive不陌生吧!Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查......
  • Hive的简单介绍与使用
    hive入门1.Hive概述1.1什么是HiveHive:由Facebook开源用于解决海量结构化日志的数据统计工具。Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张......
  • Hive职位岗位数据分析实战
    Hive职位岗位数据分析实战文章目录​​Hive职位岗位数据分析实战​​​​创建数据库​​​​使用创建的数据库​​​​创建表​​​​在hdfs上加载数据​​​​如果不在hdfs......
  • Hive 练习题
    准备数据createtablegulivideo_ori(videoIdstring,uploaderstring,ageint,categoryarray<string>,lengthint,viewsint,ratefloat,ratingsint......
  • Hive3源码总结2
    大数据技术之Hive源码2接上文2.4HQL生成AST(抽象语法树)2.5对AST进一步解析 接下来的步骤包括:1)将AST转换为QueryBlock进一步转换为OperatorTree;2)对OperatorTree进行逻辑优......