美文网首页
Hive的一个面试题

Hive的一个面试题

作者: chailei | 来源:发表于2018-08-26 19:59 被阅读0次

需求:求出每个User截止当月总的流量

user date traffic
熊猫tv 2018-01-02 5
快手 2018-01-02 3
YY 2018-01-02 2
抖音 2018-01-02 15
熊猫tv 2018-01-03 5
快手 2018-01-03 3
YY 2018-01-03 2
抖音 2018-01-03 15
熊猫tv 2018-02-02 5
快手 2018-02-02 3
YY 2018-02-02 2
抖音 2018-02-02 15
熊猫tv 2018-02-03 5
快手 2018-02-03 3
YY 2018-02-03 2
抖音 2018-02-03 15
熊猫tv 2018-03-02 5
快手 2018-03-02 3
YY 2018-03-02 2
抖音 2018-03-02 15
熊猫tv 2018-03-03 5
快手 2018-03-03 3
YY 2018-03-03 2
抖音 2018-03-03 15

hive创建表:

create table user_traffic(user string,date string,traffic bigint) row format delimited fields terminated by '\t';

load data local inpath '/home/hadoop/data/user_traffic.txt' overwrite into table user_traffic;

先按user,月份分组求出每个月的traffic

select temp.user,temp.yearmonth,sum(temp.traffic) from(select 
user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic 
from user_traffic) temp group by temp.user,temp.yearmonth;
image.png

表自连接

select t1.*,t2.* from (select temp.user,temp.yearmonth,sum(temp.traffic) from
 (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic)
 temp group by temp.user,temp.yearmonth) t1,(select temp.user,temp.yearmonth,sum(temp.traffic) from
 (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by 
temp.user,temp.yearmonth) t2 where t1.yearmonth=t2.yearmonth and t1.user = t2.user and t1.yearmonth >= t2.yearmonth;
image.png

select t.user,t.yearmonth,sum(t.total) from (select t1.user,t1.yearmonth,t1.total from (select temp.user,temp.yearmonth,sum(temp.traffic) as total from (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by temp.user,temp.yearmonth) t1,(select temp.user,temp.yearmonth,sum(temp.traffic) as total from (select user,concat(split(date,'-')[0],split(date,'-')[1]) as yearmonth,traffic from user_traffic) temp group by temp.user,temp.yearmonth) t2 where t1.user = t2.user and t1.yearmonth >= t2.yearmonth) t group by t.user,t.yearmonth;

image.png

相关文章

  • 面试题汇总:Hive

    1.《大数据Hive 面试以及知识点》 2.《Hive学习之路 (十一)Hive的5个面试题》 3.《大数据工程师...

  • Hive的一个面试题

    需求:求出每个User截止当月总的流量 user date traffic熊猫t...

  • Hive面试题

    1. 什么是数据倾斜, 如何处理 什么是数据倾斜 大量相同的key被partition到一台机器, 造成这台机器...

  • hive面试题

    http://bigdatastudy.net/show.aspx?id=163&cid=14 https://b...

  • hive面试题

    1、hive是什么? 本质是将sql转换成mr程序。 2、hive的架构? 客户端:CLI(shell命令...

  • Hive面试题

    1、Hive表关联查询,如何解决数据倾斜的问题?(☆☆☆☆☆) 1)倾斜原因: map输出数据按key Hash的...

  • hive基础入门与环境的搭建

    一、初识Hive 1、Hive简介     (1) 什么是Hive? Hive是基于Hadoop的一个数据仓库可以...

  • 大数据常见面试题目

    每天在在技术群里沉水,搜刮些面试题目,留作备用~ 1.简述对大数据组件:Yarn,Spark,Hbase,Hive...

  • Hive经典面试题

    1.Hive表关联查询,如何解决数据倾斜的问题 1)倾斜原因: map输出数据按key Hash的分配到reduc...

  • Hive安装配置

    2. Hive 的基本概念 2.1. Hive 简介 什么是 Hive Hive是基于Hadoop的一个数据仓库工...

网友评论

      本文标题:Hive的一个面试题

      本文链接:https://www.haomeiwen.com/subject/kveoiftx.html