栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive分区

Hive分区

目录

一、分区的概念及作用

二、如何实现分区表

1、分区表的简单创建及简单使用(增删查改)

(1)建立分区表

(2)增加一个分区:

(3) 删除一个分区:

(4)查看某个表的所有分区

(5)往分区中插入数据:

2、 查询某个分区的数据:

3、 Hive动态分区

(1)开启Hive的动态分区支持

(2)建立原始表并加载数据sql

(3)建立分区表并加载数据sql

(4)使用动态分区插入数据

5、多级分区


一、分区的概念及作用

概念:分区表实际上是在表的目录下在以分区命名,建子目录

作用:进行分区裁剪,避免全表扫描,减少MapReduce处理的数据量,提高效率

一般在公司的hive中,所有的表基本上都是分区表,通常按日期分区、地域分区

分区表在使用的时候记得加上分区字段

 分区也不是越多越好,一般不超过3级,根据实际业务衡量

二、如何实现分区表

1、分区表的简单创建及简单使用(增删查改)

(1)建立分区表

create external table students_pt1

(

    id bigint,

    name string,

    age int,

    gender string,

    clazz string

)

PARTITIonED BY(pt string)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

(2)增加一个分区:

alter table students_pt1 add partition(pt='20210904')

(3) 删除一个分区:

alter table students_pt drop partition(pt='20210904');

(4)查看某个表的所有分区

方式一:show partitions students_pt; // 推荐这种方式(直接从元数据中获取分区信息)

方式二:select distinct pt from students_pt; // 不推荐

(5)往分区中插入数据:

方式一:insert into table students_pt partition(pt='20210902') select * from students;

方式二:load data local inpath '/usr/local/soft/data/students.txt' into table students_pt partition(pt='20210902');

2、 查询某个分区的数据:

(1) 全表扫描,不推荐,效率低

select count(*) from students_pt;

(2)使用where条件进行分区裁剪,避免了全表扫描,效率高

select count(*) from students_pt where pt='20210101';

(3) 也可以在where条件中使用非等值判断

select count(*) from students_pt where pt<='20210112' and pt>='20210110';

3、 Hive动态分区

动态分区:根据数据中某几列的不同的取值 划分 不同的分区

有的时候我们原始表中的数据里面包含了 ''日期字段 dt'',我们需要根据dt中不同的日期,分为不同的分区,将原始表改造成分区表。

hive默认不开启动态分区

(1)开启Hive的动态分区支持

①表示开启动态分区

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

(不使用)表示动态分区模式:strict(需要配合静态分区一起使用)、nostrict(不建议使用)

# strict: insert into table students_pt partition(dt='anhui',pt) select ......,pt from students;

②强烈建议使用这个来设置分区模式

hive> set hive.exec.dynamic.partition.mode=nostrict;

③表示支持的最大的分区数量为1000,可以根据业务自己调整

hive> set hive.exec.max.dynamic.partitions.pernode=1000;

(2)建立原始表并加载数据sql

create table students_dt

(

    id bigint,

    name string,

    age int,

    gender string,

    clazz string,

    dt string

)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

向表中传入数据

load data local inpath '数据路径' into table students_dt;

(3)建立分区表并加载数据sql

create table students_dt_p

(

    id bigint,

    name string,

    age int,

    gender string,

    clazz string

)

PARTITIonED BY(dt string)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

(4)使用动态分区插入数据

// 分区字段需要放在 select 的最后,如果有多个分区字段 同理,它是按位置匹配,不是按名字匹配

insert into table students_dt_p partition(dt) select id,name,age,gender,clazz,dt from students_dt;

注意:下面这条语句会使用age作为分区字段,而不会使用student_dt中的dt作为分区字段

insert into table students_dt_p partition(dt) select id,name,age,gender,dt,age from students_dt;

5、多级分区

create table students_year_month

(

    id bigint,

    name string,

    age int,

    gender string,

    clazz string,

    year string,

    month string

)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

create table students_year_month_pt

(

    id bigint,

    name string,

    age int,

    gender string,

    clazz string

)

PARTITIonED BY(year string,month string)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

插入数据

insert into table students_year_month_pt partition(year,month) select id,name,age,gender,clazz,year,month from students_year_month;

 上单讲分区:https://developer.aliyun.com/article/81775

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/308092.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号