Hive分区表

Hive分区表

1.分区表

1)分区表是什么

分区表其实就是对应一个文件夹,我们在Hive上创建的数据库,对应的其实是HDFS上的文件夹, 我们创建表有两种情况,第一中就是不指定表的存储位置,第二种是指定表的存储位置(这里我们默认使用db_hive数据库)

第一种,不指定存储位置,在安装hive的时候,我们可以找到hive-site.xml配置文件

 <!-- Hive默认在HDFS的工作目录 -->
    <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive</value>
    </property>

所以我们在不指定的情况下默认创建的库和表都放在/user/hive目录下面

例如我们在db_hive中创建了student表,实际上在HDFS的文件路径是**/user/hive/db_hive/student/表中数据**(数据是以文件的形式,可以有多个文件,hive会统计所有文件中的内容,但是必须保持每个文件的格式都和数据库中的一致)

第二种,我们在指定存储位置的情况下

create table student(
    id  int,
    name string,
    loc string 
)
location '/hive/student'

这种情况我们的数据就在**/hive/student**中(/hive/student/数据文件)

分区表其实就是在student下面多了一层文件夹,对数据文件进行分区,下面进行详细解释

--log文件是表数据,加入我们要根据日期做一张分区表  将三个日期的分别分为三个区
--student_20200401.log
--student_20200402.log
--student_20200403.log

--创建分区表
create table student(
    id int,
    name string,
    loc string
)
partitioned by(day string)
row format delimited fields terminated by '\t'

--将log文件分别加载到表
load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table student partition(day='20200401');

load data local inpath '/opt/module/hive/datas/dept_20200402.log' into table student partition(day='20200402');

load data local inpath '/opt/module/hive/datas/dept_20200403.log' into table student partition(day='20200403');

--此时的文件结构
/hive/student/day=20200401/student_20200401.log
/hive/student/day=20200402/student_20200402.log
/hive/student/day=20200403/student_20200403.log
在student路径下面又创建了三个子文件夹,我们称之为分区,实际上就相当于目录

2)分区表基本操作

--创建分区表和加载数据在上面有写

--查看分区表有多少分区
show partitions student;

--创建单个分区
alter table student add partition(day='20200404')
--删除单个分区
alter table dept_partition drop partition (day='20200404')

--同时创建多个分区,中间不能有逗号
alter table student add partition(day='20200405') partition(day='20200406') partition(day='20200407')
--同时删除多个分区 ,中间必须加逗号(奇葩)
alter table student drop partition(day='20200405'),partition(day='20200406'),partition(day='20200407')

--查看分区表结构
desc formatted student

3)二级分区基本操作

--创建二级分区表
 create table student(
     id int, 
     name string, 
     loc string
 )
partitioned by (day string, hour string)
row format delimited fields terminated by '\t';

--加载数据
load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table
student2 partition(day='20200401', hour='12');

--查看分区数据
select * from dept_partition2 where day='20200401' and hour='12';

4)把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

方式一 上传数据后修复
--上传数据
dfs -mkdir -p /user/hive/warehouse/db_hive.db/dept_partition2/day=20200401/hour=13;
dfs -put /opt/module/hive/datas/dept_20200401.log  /user/hive/warehouse/db_hive.db/dept_partition2/day=20200401/hour=13;

--此时我们查询数据是查询不到的,需要执行修复命令,修复完成后就可以查询数据
 msck repair table dept_partition2
方式二 上传数据后添加分区
--上传数据
dfs -mkdir -p /user/hive/warehouse/db_hive.db/dept_partition2/day=20200401/hour=13;
dfs -put /opt/module/hive/datas/dept_20200401.log  /user/hive/warehouse/db_hive.db/dept_partition2/day=20200401/hour=13;

alter table dept_partition2 add partition(day='20200401',hour='14');
方式三 创建文件夹后load数据到分区
--创建目录
 dfs -mkdir -p /user/hive/warehouse/db_hive.db/dept_partition2/day=20200401/hour=15;
 --上传数据
 load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table
 dept_partition2 partition(day='20200401',hour='15');


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值