zeppelin 行转列、开窗函数、动态分区表

企业实训课第七节
  • 一键启动所有命令(脚本)
cd /export/  #进入到此文件夹下
ll
mkdir onekey  # 创建一个文件夹
cd onekey/
vim onekey-start.sh  # 进入到编辑模式,编辑内容

### context
# 启动HDFS集群
echo "启动Hadoop集群..."
/export/server/hadoop-3.1.4/sbin/start-all.sh

# 启动Hive MetaStore 服务器
echo "启动Hive MetaStore服务器..."
nohup /export/server/hive/bin/hive --service metastore > ./metastore.log 2>&1 &
sleep 5 

# 启动Hive hiveserver2 服务器
nohup /export/server/hive/bin/hive --service hiveserver2 > ./hiveserver2.log 2>&1 &
sleep 5

# 启动Zeppelin服务器
echo "启动Zeppelin服务器..."
/export/server/zeppelin-0.8.2-bin-all/bin/zeppelin-daemon.sh start


#一键关闭
vim onekey-stop.sh

## content
/export/server/zeppelin-0.8.2-bin-all/bin/zeppelin-daemon.sh stop
jps -lm | grep -i 'server.HiveServer2' | awk '{print $1}' | xargs kill -s 9
jps -lm | grep -i 'metastore.HiveMetaStore' | awk '{print $1}' | xargs kill -s 9
/export/server/hadoop-3.1.4/sbin/stop-all.sh
  • 赋予777权限
chmod -R 777 onekey/
  • hive内置函数
    在这里插入图片描述
1、实操理解hive内置函数
行转列操作笔记

以下操作均在zeppelin操作

# 创建表
create table emp( deptno int, ename string ) row format delimited fields terminated by '\t'
试验

在这里插入图片描述

  • 课堂练习题
    将上图行转列,以|分割;
select  deptno, concat_ws('|' , collect_set(ename)) from emp group by deptno;
2、开窗函数

在这里插入图片描述

实操理解开窗函数
  • 创建表
create table user_access (
 user_id string, 
 createtime string, --day 
 pv int 
) 
row format DELIMITED FIELDS TERMINATED BY ','

在这里1片描述

  • 实现分组排名
    开窗函数:实际上就是生成一个新的字段(新的窗口),来根据我们的逻辑和规则显示;
    关键字:| 函数 * over(partition by xxxx [order by ]) -> 组成开窗函数
rank() dense_rank()  row_number() 
Q:查看哪一个用户的pv量最多?
select user_id,createtime,pv,
rank() over(partition by user_id order by pv desc) as rn1,
dense_rank() over(partition by user_id order by pv desc) as rn2,
row_number() over(partition by user_id order by pv desc) as rn3
from user_access

在这里插入图片描述

hive内置函数-开窗函数
  • sum avg min max
    在这里插入图片描述
    在这里插入图片描述
分区表
  • 动态分区-——分区表数据加载
    【动态分区】指分区的字段值是基于查询结果(参数位置)自动推断出来的。
    【核心语法】insert+select;
  • 启用hive动态分区,需要在hive绘画中设置两个参数。
#是否开启动态分区功能
set hive.exec.dynamic.partition=true;
#指定动态分区模式,分为nonstick非严格模式和strict严格模式。
#strict严格模式要求至少有一个分区为静态分区。
set hive.exec.dynamic.partition.mode=nonstrict;
实操动态分区表
  • 创建分区表
create table t_all_hero(
   id int,
   name string,
   hp_max int,
   mp_max int,
   attack_max int,
   defense_max int,
   attack_range string,
   role_main string,
   role_assist string
)
row format delimited
fields terminated by "\t";

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

create table t_all_hero_part(
   id int,
   name string,
   hp_max int,
   mp_max int,
   attack_max int,
   defense_max int,
   attack_range string,
   role_main string,
   role_assist string
) partitioned by (role string)--注意哦 这里是分区字段
row format delimited
fields terminated by "\t";
静态分区表-手动加载

在这里插入图片描述
静态加载

load data local inpath '/export/data/hivedata/archer.txt' into table t_all_hero_part partition(role='sheshou');
load data local inpath '/export/data/hivedata/assassin.txt' into table t_all_hero_part partition(role='cike');
load data local inpath '/export/data/hivedata/mage.txt' into table t_all_hero_part partition(role='fashi');
load data local inpath '/export/data/hivedata/support.txt' into table t_all_hero_part partition(role='fuzhu');
load data local inpath '/export/data/hivedata/tank.txt' into table t_all_hero_part partition(role='tanke');
load data local inpath '/export/data/hivedata/warrior.txt' into table t_all_hero_part partition(role='zhanshi');

把6个文件上传到mobx里,之后开beeline

../server/hive/bin/beeline
一键启动脚本

在这里插入图片描述

———————————————————

动态分区
  • 创建动态分区表
--创建一张新的分区表 t_all_hero_part_dynamic
create table t_all_hero_part_dynamic(
    id int,
    name string,
    hp_max int,
    mp_max int,
    attack_max int,
    defense_max int,
    attack_range string,
    role_main string,
    role_assist string
) partitioned by (role string)
row format delimited
fields terminated by "\t";

创建一张新的分区表,执行动态分区插入;

动态分区插入时,分区值是根据查询返回字段位置自动推断的。

核心语句——动态分区
--执行动态分区插入
insert into table t_all_hero_part_dynamic partition(role) select tmp.*,tmp.role_main from t_all_hero tmp;

在这里插入图片描述

分桶表

在这里插入图片描述

在这里插入图片描述
语法
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值