Hive DML ,分区表

1.Hive构建在Hadoop之上的数据仓库
sql ==> Hive ==> MapReduce
但是有些简单基本的hive不调用mapreduce,就是不带分组

2.分组函数:出现在select中的字段,要么出现在group by子句中,要么出现在聚合函数中。

3.count(1) and count(字段)
两者的主要区别是
(1) count(1) 会统计表中的所有的记录数,包含字段为null 的记录。
(2) count(字段) 会统计该字段在表中出现的次数,忽略字段为null 的情况。即不统计字段为null 的记录。

4.(case when then else end ) 类似if-else,返回一列then
的结果.
union all 堆叠

5.看hive里有哪些函数

hive (default)> show functions;

desc function extended xxx 查看函数功能

转换某个字段的类型,如果转换失败,返回值就是null

cast(value as TYPE)

截取一段字符串,开始位置,截取长度

substr(str,pos,len)

返回以.分割的连接

concat_ws('.','www','asd') 返回www.asd

返回长度,字符串数字都可以

length()

把数组分隔为多行

explode()

拆分,以a,d两种分割符

split('asd.sdf','[a,d]')

用.分割的话要

hive (default)> select split('asd.asd','\\.');
OK
["asd","asd" ]

6.用hive函数完成一个wordcount

数据
asd,dsa,asd
asd,das

create table ruoze_wc(
sentence string
);

select word, count(1) as c
from
(
select explode(split(sentence,",")) as word from ruoze_wc
) t group by word
order by c desc;

split之后成了
[‘asd’,‘dsa’,‘asd’]
[‘asd’,‘das’]

explode后变成5行1列的形式

7.创建和数组相关的表

1,doudou,化学:物理:数学:语文
2,dasheng,化学:数学:生物:生理:卫生
3,rachel,化学:语文:英语:体育:生物

create table ruoze_student(
id int,
name string,
subjects array<string>  数组里装string
)row format delimited fields terminated by ','
COLLECTION ITEMS TERMINATED BY ':';   数组集合用:分割



load data local inpath '/home/hadoop/data/student.txt' into table ruoze_student;

hive (default)> select * from ruoze_student;
OK
1	doudou	["化学","物理","数学","语文"]
2	dasheng	["化学","数学","生物","生理","卫生"]
3	rachel	["化学","语文","英语","体育","生物"]

8.分区表

分区表:一个表按照某些字段进行分区
解决问题:全盘扫描慢,分区定位扫描快

create table order_partition(
orderNumber string,
event_time string
)PARTITIONED BY(event_month string)   按照event_month分区
row format delimited fields terminated by '\t';

指定分区加载,数据表会多个分区列

load data local inpath '/home/hadoop/data/order.txt' into table order_partition PARTITION (event_month='2014-05');

如果报错,key太长,需要修改字符集,在mysql里改

use ruoze_d5;
alter table PARTITIONS convert to character set latin1;
alter table PARTITION_KEYS convert to character set latin1;

手动hdfs dfs 创建partitions分区,会找不到元数据,需要
MSCK REPAIR 分区表 ,这要刷所有分区,性能低,不用。

增加分区的办法:

alter table order_partition add partition(event_month='2014-07');

查看一个表的分区:

show partitions order_partition;

查看如何创建的表

show create table xxx;

9.多级分区表

create table order_mulit_partition(
orderNumber string,
event_time string
)PARTITIONED BY(event_month string, step string)
row format delimited fields terminated by '\t';

load data local inpath '/home/hadoop/data/order.txt' into table order_mulit_partition PARTITION (event_month='2014-05',step='1');

10.动态分区

需求,按照deptno字段写进分区表里

CREATE TABLE `ruoze_emp_partition`(
  `empno` int, 
  `ename` string, 
  `job` string, 
  `mgr` int, 
  `hiredate` string, 
  `sal` double, 
  `comm` double)
partitioned by(`deptno` int)
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY '\t';

静态导入

insert into table ruoze_emp_partition PARTITION(deptno=10)
select empno,ename,job,mgr,hiredate,sal,comm from ruoze_emp where deptno=10;

假如有1000个deptno,岂不是要写1000个导入

动态导入

分区字段deptno要写在最后,1句解决。

insert overwrite table ruoze_emp_partition PARTITION(deptno)
select empno,ename,job,mgr,hiredate,sal,comm,deptno from ruoze_emp;

启动动态分区功能

hive> set hive.exec.dynamic.partition=true;

猜你喜欢

转载自blog.csdn.net/qq_36459386/article/details/84024142