hive基础命令和经典题总结

1.建库:
create database mybd;
create database if not exists mydb;
create database if not exists mydb location ‘a’

2.查询数据库:
查询库列表:show databases;
查询库详细信息:desc database [extended] mydb

3.删除数据库:
drop database mydb;
drop database if exists mydb;
drop database if exists mydb [restrict cascade];

4.进入我们要操作的数据库/切换库
use mydb;

5.查看数据库里面的表
show tables;
show tables in mydb;

6.添加表
1)创建内部表(Managered table)
create table mingxing( id int, name string, sex string) row format delimited fields terminated by ‘,’
2)创建外部表(External table)
create external table mingxing (id int, name string)
row format delimited fields terminated by ‘,’ location
‘/hive/qyl/hivedata’
3)创建分区表
create table mingxing_ptn(id int, name string) partitioned by (city string) row format delimited fields terminated bu ‘,’
注意:分区字段不能是表中声明的字段,必须是一个新字段。
4)创建分桶表
create table mingxing_bck(id int, name string,age int) clustered by(id) sorted by(age desc) into 4 buckets row format delimited fields terminated by ‘,’
注意:clustered里面的字段必须是表字段中出现的字段,分桶字段和排序字段可以不一样

7.删除表
drop table mingxing;
drop table if exists mingxing;

8.对表进行重命名
alter table mingxing rename to student;

9.对表的字段进行操作(add,drop,change,replace)
增加字段:
alter table mingxing add columns(province string);
修改字段:
alter table mingxing change newage string;
alter table mingxing change newage string after id;
alter table mingxing change age newage string first;
替换字段:
alter table mingxing replace columns(id int, name string) 替换所有字段

10.对表中的分区进行操作
增加分区:
alter table mingxing_ptn add partition(city=‘beijing’)
删除分区:
alter table mingxing_ptn drop partition(city=“beijg”)

11.查询显示命令
查看库: show databases;
查看表: show tables;
查看建表完整语法:show create table mingxing
查看内置函数库:show functions
查看函数的详细手册:desc function extended concat;
查看分区: show partitions mingxing
查看表的字段: desc mingxing
查看表的详细信息: desc extended mingxing
查看表格式化之后的详细信息:desc formatted mingxing

扫描二维码关注公众号,回复: 4621874 查看本文章

12.load方式导入数据
导入本地相对路径的数据:
load data local inpath ‘./student.txt’ into table mi;
load data local inpath './student.txt’overwrite into table student(覆盖导入)
导入本地绝对路径数据:
load data local inpath “/home/qyl/hivedata/student” into table mingxing;
导入HDFS上的数据:
load data inpath ‘/student’ into table student;
注意:
1.导入HDFS上的数据到hive表,表示截切,移动
2.导入本地数据,相当于复制或者上传

13、利用insert关键字往表中插入数据
单条数据插入:
insert into table mingxing values (001,‘huangbo’,‘male’,50,‘MA’);
单重插入模式: insert … select …
insert into table student select id,name,sex,age,department from mingxing;
注意:查询出的字段必须是student表中存在的字段

多重插入模式:
from mingxing
insert into table student1 select id,name,sex,age
insert into table student2 select id,department;

静态分区插入:
需要手动的创建分区
alter table student add partition (city=“zhengzhou”)
load data local inpath ‘/root/hivedata/student.txt’ into table student partition(city=‘zhengzhou’);

动态分区插入:
打开动态分区的开关:set hive.exec.dynamic.partition = true;
设置动态分区插入模式:set hive.exec.dynamic.partition.mode = nonstrict
create table student(name string, department string) partitioned by (id int) …
insert into table student partition(id) select name,department,id from mingxing2;
student表字段:name,department, 分区字段是id
查询字段是:name,department,id,分区字段
注意:动态分区插入的分区字段必须是查询语句当中出现的字段中的最后一个

CTAS(create table ... as select ...)(直接把查询出来的结果存储到新建的一张表里)
create table student as select id,name,age,department from mingxing;
注意:自动新建的表中的字段和查询语句出现的字段的名称,类型,注释一模一样
限制:
1、不能创建外部表
2、不能创建分区表
3、不能创建分桶表

分桶插入:
创建分桶表:
create table mingxing(id int, name string, sex string, age int, department string)
clustered by(id) sorted by(age desc) into 4 buckets
row format delimited fields terminated by ‘,’;

插入数据:
insert into table mingxing select id,name,sex,age,department from mingxing2
distribute by id sort by age desc;
注意:查询语句中的分桶信息必须和分桶表中的信息一致

14.like关键字使用:复制表结构
create table student like mingxing;

15.利用inset导出数据到本地或者hdfs
单模式导出数据到本地:
insert overwrite local directory ‘/root/outputdata’ select id,name,sex,age,department from mingxing;

16.清空数据库表中的数据
truncate table mingxing2;

17.select 查询
order by:全局排序
sort by:局部排序
distribute by:指定分桶字段
cluster by :既分桶,也排序

18.join查询
限制:
支持等值连接,不支持非等值连接
支持and操作,不支持or
支持超过2个表的连接
经验:
当出现多个表进行连接是,最好把小表放置在前面!!
join分类:
inner join
left outer join
right outer join
full outer join
left semi join 它是in、exists的高效实现
select a.* from a left semi join b on a.id=b.id
等价于:
select a.* from a where a.id in (select b.id from b);

二、hive的高级语法
1.内置函数
日期函数 :
1. UNIX时间戳转日期函数: from_unixtime
2. 获取当前UNIX时间戳函数: unix_timestamp
3. 日期转UNIX时间戳函数: unix_timestamp
4. 指定格式日期转UNIX时间戳函数: unix_timestamp
字符串函数:
1. 字符ascii码函数:ascii
2. base64字符串
3. 字符串连接函数:concat
4. 带分隔符字符串连接函数:concat_ws
5. 数组转换成字符串的函数:concat_ws
6. 小数位格式化成字符串函数:format_number
7. 字符串截取函数:substr,substring

2.自定义函数
步骤:
1)自定义类继承UDF,定义evaluate()方法
2)将此类打成jar包导入到linux
3)在hive中 add jar ‘文件路径’
4)创建一个临时函数 create temporary function zk as “类的全限定名称”;
5)使用函数

3.hive的高阶操作
1.窗口函数:
场景1:求到这个月为止,累计的销售额或当月最大销售额等

 SELECT cookieid,createtime,pv,SUM(pv) OVER(PARTITION BY cookieid ORDER BY createtime) AS pv1 from cookie;

结果: –其他AVG,MIN,MAX,和SUM用法一样。
sum(pv)
cookie1 2015-04-10 1 1
cookie1 2015-04-11 5 6
cookie1 2015-04-12 7 13
cookie1 2015-04-13 3 16
cookie1 2015-04-14 2 18
cookie1 2015-04-15 4 22
cookie1 2015-04-16 4 26

场景2:统计一个cookie,pv数最多的前1/3天(ntile)
NTILE(n),用于将分组数据按照顺序切分成n片,返回当前切片值
如果切片不均匀,默认增加第一个切片的分布

SELECT cookieid, createtime, pv,
    NTILE(3) OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn 
FROM cookie2;

场景3:统计小于等于当前薪水的人数,所占总人数的比例
–CUME_DIST 小于等于当前值的行数/分组内总行数
SELECT dept, userid, sal,
CUME_DIST() OVER(ORDER BY sal) AS rn1, //小于当前值所占的比例
CUME_DIST() OVER(PARTITION BY dept ORDER BY sal) AS rn2 //分组后
FROM cookie3;
// 结果:
d1 user1 1000 0.2 0.3333333333333333
d1 user2 2000 0.4 0.6666666666666666
d1 user3 3000 0.6 1.0
d2 user4 4000 0.8 0.5
d2 user5 5000 1.0 1.0

2.hive中行转列,列转行的方法

列转行;case (字段) when (某个值) then (成立的时候的值) else (不成立的值) end [重命名字段])
例子:
张三 数学 89 1
张三 语文 80 1
张三 英语 70 1
李四 数学 90 2
李四 语文 70 2
李四 英语 80 2
1)select id,name,sum(shuxue),sum(yuwen),sum(english) from
(select id,name,
case course when “数学” then score else 0 end shuxue ,
case course when “语文” then score else 0 end yuwen,
case course when “英语” then score else 0 end english
from student) a group by id,name;
2)select id,name,concat_ws(’,’,collect_set(score))
> from student
> group by id,name;

行转列:炸裂函数(explode)
select id,name,allscore from student a
lateral view explode(split(score,",")) b as allscoure;

猜你喜欢

转载自blog.csdn.net/weixin_43823423/article/details/85060903