目录
分区:避免全部扫描:
格式:
create external table 表名
(
字段1 字段类型1,
字段2 字段类型2
...
)paartitioned by (分区字段 字段类型)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '分隔符'
STORED AS file_format
location 'HDFS路径';
注意:分区字段和普通字段没区别(不能重复)
添加分区:
alter table 表名 add partition(分区字段='值');
删除分区:
alter table 表名 drop partition(分区字段='值');
查看分区:
select DISTINCT 分区字段 from 表;
show partition 表;
插入数据:
load data local inpath '路径' into table 表名 partition(分组字段='值');
分区不存在自动创建
动态分区:
set hive.exec.dynamic.partition=true; //开启hive动态分区,
hive默认不支持动态分区
set hive.exec.dynamic.partition.mode=nostrict; //动态分区模式
动静结合
set hive.exec.max.dynamic.partitions.pernode=1000; //hive最大分区数
insert into 分区表 partition(dt) select id,name,age,dt from 表
清空表数据,不清楚表建表语句
truncate table 表名;
分桶:
开启分桶开关:set hive.enforce.buckting=true;
创建分桶表:
create table students_cluster
(
id bigint comment '学生id',
name string comment '学生姓名',
age int comment '学生年龄',
gender string comment '学生性别',
clazz string comment '学生班级'
) comment '学生信息表'
clustered by (clazz) into 6 buckets
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',’;
注意:分桶字段来源普通字段,分桶数量是明确的。
往分桶表里插入数据:
insert overwrite table student select id,name,age,gender,clazz from student;
Hive会自动根据bucket个数自动分配Reduce task的个数
Reduce个数与bucket个数一致
分桶加载数据:
load:不会触发分桶
insert into:自动触发分桶
注意:
分桶:不常用,因为对文件进行切分产生过多的小文件
分区:最多三级分区
Hive数据类型:
基本数据类型:
数值型:
TINYINT — 微整型,只占用1个字节,只能存储0-255的整数。
SMALLINT– 小整型,占用2个字节,存储范围–32768 到 32767。
INT– 整型,占用4个字节,存储范围-2147483648到2147483647。
BIGINT– 长整型,占用8个字节,存储范围-2^63到2^63-1。
布尔型BOOLEAN — TRUE/FALSE 浮点型FLOAT– 单精度浮点数。
DOUBLE– 双精度浮点数。 字符串型STRING– 不设定长度。
日期类型:
1,Timestamp 格式“YYYY-MM-DD HH:MM:SS.fffffffff”(9位小数位精度)
2,Date DATE值描述特定的年/月/日,格式为YYYY-MM-DD。
复杂数据类型: Structs,Maps,Arrays
窗口函数(开窗函数):用户分组中开窗
row_number()等
select * from (
select name,date_time,rownumber() over(partition by name order by cost desc) as rn
from window_t) a
where rn
一般用于分组中求TopN
dense_rank() 并列连续排名
rank() 并列不连续排名
拆分多行函数:
explode(ARRAY)
返回值:多行
select explode(array(1,2,3)) from 表
ok
1
2
3
create table laterv_tb(name string,weight array)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ',' ;
合并函数:
LATERAL VIEW
SELECT name,new_num FROM laterv_tb LATERAL VIEW explode(weight) num AS new_num;