hive窗口函数练习题

有作业问题可私信小刘,欢迎探讨

一、第一套练习

需求:
1、求用户明细并统计每天的用户总数
2、计算从第一天到现在的所有 score 大于80分的用户总数
3、计算每个用户到当前日期分数大于80的天数

test_window.txt数据:

20191020,11111,85
20191020,22222,83
20191020,33333,86
20191021,11111,87
20191021,22222,65
20191021,33333,98
20191022,11111,67
20191022,22222,34
20191022,33333,88
20191023,11111,99
20191023,22222,33

建表:

0: jdbc:hive2://hadoop:11240> create table test_window(logday string,userid string,score int)
. . . . . . . . . . . . . . > row format delimited
. . . . . . . . . . . . . . > fields terminated by ',';
0: jdbc:hive2://hadoop:11240> load data local inpath '/home/xiaokang/hivedata/test_window.txt'
. . . . . . . . . . . . . . > into table test_window;
0: jdbc:hive2://hadoop:11240> select * from test_window;
+---------------------+---------------------+--------------------+
| test_window.logday  | test_window.userid  | test_window.score  |
+---------------------+---------------------+--------------------+
| 20191020            | 11111               | 85                 |
| 20191020            | 22222               | 83                 |
| 20191020            | 33333               | 86                 |
| 20191021            | 11111               | 87                 |
| 20191021            | 22222               | 65                 |
| 20191021            | 33333               | 98                 |
| 20191022            | 11111               | 67                 |
| 20191022            | 22222               | 34                 |
| 20191022            | 33333               | 88                 |
| 20191023            | 11111               | 99                 |
| 20191023            | 22222               | 33                 |
+---------------------+---------------------+--------------------+

1、求用户明细并统计每天的用户总数

0: jdbc:hive2://hadoop:11240> select *,count()over(partition by logday)as day_total from test_window;
+---------------------+---------------------+--------------------+------------+
| test_window.logday  | test_window.userid  | test_window.score  | day_total  |
+---------------------+---------------------+--------------------+------------+
| 20191020            | 33333               | 86                 | 3          |
| 20191020            | 22222               | 83                 | 3          |
| 20191020            | 11111               | 85                 | 3          |
| 20191021            | 33333               | 98                 | 3          |
| 20191021            | 22222               | 65                 | 3          |
| 20191021            | 11111               | 87                 | 3          |
| 20191022            | 33333               | 88                 | 3          |
| 20191022            | 22222               | 34                 | 3          |
| 20191022            | 11111               | 67                 | 3          |
| 20191023            | 22222               | 33                 | 2          |
| 20191023            | 11111               | 99                 | 2          |
+---------------------+---------------------+--------------------+------------+

2、计算从第一天到现在的所有 score 大于80分的用户总数

0: jdbc:hive2://hadoop:11240> select *,count()over(order by logday rows between unbounded preceding and current row) as total from test_window where score>80;
+---------------------+---------------------+--------------------+--------+
| test_window.logday  | test_window.userid  | test_window.score  | total  |
+---------------------+---------------------+--------------------+--------+
| 20191020            | 33333               | 86                 | 1      |
| 20191020            | 22222               | 83                 | 2      |
| 20191020            | 11111               | 85                 | 3      |
| 20191021            | 33333               | 98                 | 4      |
| 20191021            | 11111               | 87                 | 5      |
| 20191022            | 33333               | 88                 | 6      |
| 20191023            | 11111               | 99                 | 7      |
+---------------------+---------------------+--------------------+--------+

3、计算每个用户到当前日期分数大于80的天数

0: jdbc:hive2://hadoop:11240> select *,count()over(partition by userid order by logday rows between unbounded preceding and current row) as total
. . . . . . . . . . . . . . > from test_window where score>80 order by logday,userid;
+---------------------+---------------------+--------------------+--------+
| test_window.logday  | test_window.userid  | test_window.score  | total  |
+--------
1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下 如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘新鲜事儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值