HIve中的分区表和分桶表

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

导入数据的四种方式:
1、将本地的数据导入到hive中
load data local inpath '/root/tes.txt' into table test.usr;
2、从hdfs集群导入数据
load data inpath 'hdfs://node01:9000/user/tes.txt' into table test.te;
这里的test.te指的是test数据库的te表
LOAD DATA命令,
可分为LOAD DATA LOCAL INPATH和LOAD DATA INPATH。两者的区别在于LOCAL导入的是本地文件而不加LOCAL的导入的是HDFS文件—相当于直接将文件进行相应的上传
3、insert into—内外部表,不适应于分区
4、将table1的数据添加到table2的表中

from table1
		insert into(overwrite) tables2
			select id ,name

Hive 分区partition(分成不同的文件目录进行存储)

静态分区:

==必须在表定义时指定对应的partition字段-----分区字段一定不能与表中字段重复==

a、单分区建表语句:

create table day_table (id int, content string) partitioned by (dt int);

上传数据:

load data local inpath '/root/tes.txt' into table test.usr partition (age=10);

单分区表,按天分区,在表结构中存在id,content,dt三列。
以dt为文件夹区分
粗细力度分区的时候要根据业务需求,提前进行相应的设定 年月日时分秒----为了减少每一个分区中的内容,提高计算效率

b、 双分区建表语句:
双分区表,按天和小时分区,在表结构中新增加了dt和hour两列。
先以dt为文件夹,再以hour子文件夹区分
create table hour(id int, content string) partitioned by (dt int, hour int);
增加分区
alter table hour add partition(dt=10,hour=40);
alert table tablename add partiton(dt=20,hour=40)
也就是说添加分区的时候不能直接添加,而是需要将原来的分区也要包含其中,完成相应的排序
删除分区
alter table tablename drop partition (sex='boy')
alert table tablename drop partiton(dt=20,hour=40)
注:删除分区的时候,会将所有存在的分区都删除

动态分区:

修改权限的方式:
1、conf/hive-site.xml
2、在hive内部使用set进行相应的设置
2.1、修改权限
set hive.exec.dynamic.partiton=true //开启动态分区
2.2、修改默认状态
set hive.exec.dynamic.partiton.mode=nostrict //默认strict至少有一个静态分区
3、hive启动的时候设置
hive --conf hive.exec.dynamic.partiton=true

创建分区表:

 create table psn22(
		 id int,
		 name string,
		 likes array<String>,
		 address map<string ,string>
		 )
		 partitioned by (age int ,sex string)
		 ROW FORMAT DELIMITED 
		FIELDS TERMINATED BY ’,’ 
		COLLECTION ITEMS TERMINATED BY ‘,’ 
		MAP KEYS TERMINATED BY ‘:’ 
		LINES TERMINATED BY ‘\t’ 

写入数据
from psn21 //已经存在的表格并且要有数据
insert overwrite table pas22 partiton (age,sex)
select * distribute by age,sex

分桶表

开启分桶

set hive.enforce.bucketing=true

创建桶

create table psnbucket1 (
	id int,
	name string,
	age int)
	clustered by(age) into 4 buckets   通过age字段创建4个桶        注意点by(字段)不空
	row format delimited   行格式分割
	fields terminated by ','    键值对使用冒号分隔
	lines terminated by '\t'   记录之间使用换行符分隔

加载数据
insert into table 分桶表 select id,name,age from 表名
抽样
select * from 表 tablesample(bucket 1 out of 4 on colimes)
1指的是第几个桶
4指的是桶的个数

常见错误问题

创建分桶只显示一个的原因
1.开启分桶
set hive.enforce.bucketing=true
2.hadoop开启时间过久,重新启动
3.创建的过程
建立临时表—导入数据-----临时表数据导入分通表
创建失败
Number of reduce tasks is set to 0 since there’s no reduce operator
Job running in-process (local Hadoop)
2019-06-20 11:38:49,107 Stage-1 map = 100%, reduce = 0%

创建成功

In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Job running in-process (local Hadoop)
2019-06-20 12:01:13,547 Stage-1 map = 100%,  reduce = 0%
2019-06-20 12:01:14,590 Stage-1 map = 100%,  reduce = 25%
2019-06-20 12:01:15,616 Stage-1 map = 100%,  reduce = 100%

这里可以看出reduce100%成功 就证明你分桶成功了。

猜你喜欢

转载自blog.csdn.net/power_k/article/details/93137028