一、Hive分区
(一)、分区概念:
为什么要创建分区:单个表数据量越来越大的时候,在Hive Select查询中一般会扫描整个表
内容,会消耗很多时间做没必要的工作。有时候只需要扫描表中关心的一部分数据,因此建表
时引入了partition概念。
(1)、Hive的分区和mysql的分区差异:mysql分区是将表中的字段拿来直接作为分区字段,
而hive的分区则是分区字段不在表中。
(2)、怎么分区:根据业务分区,(完全看业务场景)选取id、年、月、日、男女性别、年龄段
或者是能平均将数据分到不同文件中最好,分区不好将直接导致查询结果延迟。
(3)、分区细节:
1、一个表可以拥有一个或者多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下。
2、表和列名不区分大小写。
3、分区是以字段的形式在表结构中存在,通过describe table命令可以查看到字段存在(算是
一个伪列),但是该字段不存放实际的数据内容,仅仅是分区的表示。
4、分区有一级、二级、三级和多级分区:
5、创建动态分区、静态分区、混合分区:
动态分区:可以动态加载数据
静态分区:可以静态加入数据
混合分区:动态和静态结合加入数据

hive的分区则是分区字段不在表中。***********************

二、分区案例
案例1:使用hive的分区表对1608c班学生信息按性别存储;
create table if not exists part_1608c(
sno int,
sname string,
sage int,
saddress string
)
partitioned by(sex string)
row format delimited fields terminated by',';

说明;创建分区表:
partitioned by(sex string) 设置分区字段,并且分区字段不在表中

vi part_1608c_nan.txt
10001,laowu,18,daxing
10002,laowang,48,fanshan
10003,laozhang,8,daxing
10004,laoxu,18,daxing

vi part_1608c_nv.txt
10005,xiaowu,28,daxing
10006,xiaowang,18,fanshan
10007,xiaozhang,18,daxing
10008,xiaoxu,18,daxing

*************LOAD DATA 方式加载数据到分区**********************

对分区表数据的导入方式:
load data local inpath '/opt/data/part_1608c_nan.txt' into table part_1608c partition(sex='nan');
load data local inpath '/opt/data/part_1608c_nv.txt' into table part_1608c partition(sex='nv');

查看表的分区:
show partitions part_1608c;

添加分区:
alter table part_1608c add partition(sex='bunan');
或者
alter table part_1608c add partition(sex='bunv') partition(sex='bunanbunv');
重命名分区:
alter table part_1608c partition(sex='bunanbunv') rename to partition(sex='nannv');
删除分区:
alter table part_1608c drop partition(sex='bunv');
alter table part_1608c drop partition(sex='bunan');
alter table part_1608c drop partition(sex='nannv');

分区表的查询:
说明:对于分区表,在严格模式下查询分区表时必须使用where带上分区字段和分区值!
set hive.mapred.mode=strict;
select * from part_1608c where sex='nan';

案例2:使用hive的分区表对1608c班学生信息按性别存储;
创建一个普通表:
create table if not exists tb_students(
sno int,
sname string,
sage int,
saddress string,
sex string
)
row format delimited fields terminated by',';

vi tb_students.txt
10001,laowu,18,daxing,nan
10002,laowang,48,fanshan,nv
10003,laozhang,8,daxing,nan
10004,laoxu,18,daxing,nv
10005,xiaowu,28,daxing,nan
10006,xiaowang,18,fanshan,nv
10007,xiaozhang,18,daxing,nv
10008,xiaoxu,18,daxing,nan

load data local inpath '/opt/data/tb_students.txt' into table tb_students;

创建一个分区表:
create table if not exists part_1608c2(
sno int,
sname string,
sage int,
saddress string
)
partitioned by(sex string)
row format delimited fields terminated by',';

***************INSERT INTO 方式添加数据到分区表***************
insert into table part_1608c2 partition(sex='nan')
select sno,sname,sage,saddress from tb_students where sex='nan';

insert into table part_1608c2 partition(sex='nv')
select sno,sname,sage,saddress from tb_students where sex='nv';

总结:像上面两种方式加载数据到分区的方式加静态分区
静态分区:指定分区数量和字段值(sex='nan'、sex='nv')
静态分区的使用场景:当数据的分区字段数量和分区值确定,并且分区数量比较少时使用静态分区!

动态分区案例
案例2:将学生信息按年龄分区
创建一个普通表:
create table if not exists tb_students2(
sno int,
sname string,
saddress string,
sex string,
sage int
)
row format delimited fields terminated by',';

vi tb_students2.txt
10001,laowu,daxing,nan,18
10002,laowang,fanshan,nv,48
10003,laozhang,daxing,nan,8
10004,laoxu,daxing,nv,18
10005,xiaowu,daxing,nan,28
10006,xiaowang,fanshan,nv,18
10007,xiaozhang,daxing,nv,18
10008,xiaoxu,daxing,nan,18

load data local inpath '/opt/data/tb_students2.txt' into table tb_students2;

创建分区表:分区依据是年龄
create table if not exists part_students2(
sno int,
sname string,
saddress string,
sex string
)
partitioned by(sage int)
row format delimited fields terminated by',';

动态分区;使用动态方式实现按年龄分区
动态分区时只能以结果集的方式将数据动态分区到分区表:

要能使用动态分区,必须打开动态分区模式,并且设置分区模式为非严格模式!
1.打开动态分区模式:
set hive.exec.dynamic.partition=true;
2.设置分区模式为非严格模式
set hive.exec.dynamic.partition.mode=nonstrict;

insert into table part_students2 partition(sage)
select sno,sname,saddress,sex,sage from tb_students2;

总结:像上面插入分区表数据的方式是动态分区
动态分区:在插入数据时,不确定分区数量并且分区数量不是特别大的时候可以使用动态分区
动态分区,在插入数据的时分区字段的值是不确定的!

**************混合分区****************
案例3:将用户信息按国家和城市分区
创建用户信息表:
create table if not exists users(
ucard int,
uname string,
contry string,
city string
)
row format delimited fields terminated by'\t';

加载数据:
load data local inpath '/opt/data/city.txt' into table users;

创建二级分区表:
create table if not exists part_users(
ucard bigint,
uname string
)
partitioned by(contry string,city string)
row format delimited fields terminated by'\t';

混合分区:有静态分区字段和动态分区字段混合
insert into table part_users partition(contry="USA",city)
select ucard,uname,city from users where contry='USA';

insert into table part_users partition(contry="CH",city)
select ucard,uname,city from users where contry='CH';

insert into table part_users partition(contry="UK",city)
select ucard,uname,city from users where contry='UK';

混合分区注意;主分区字段必须是静态字段、辅助分区可以是动态。

静态分区:
动态分区:
混合分区:

案例4: 数据如果已经落地在hdfs系统的目录下,如果创建hive表管理已经落地的数据!
模拟落地数据:
mkdir /opt/data/source
cd /opt/data/source
[root@Hadoop001 source]# mkdir 2016/01/01 -p
[root@Hadoop001 source]# mkdir 2016/01/02 -p
[root@Hadoop001 source]# mkdir 2016/01/03 -p
[root@Hadoop001 source]# mkdir 2016/01/04 -p
[root@Hadoop001 source]# mkdir 2016/02/04 -p
[root@Hadoop001 source]# mkdir 2016/02/03 -p
[root@Hadoop001 source]# mkdir 2016/02/02 -p
[root@Hadoop001 source]# mkdir 2016/02/01 -p
[root@Hadoop001 source]# mkdir 2016/03/01 -p
[root@Hadoop001 source]# mkdir 2016/02/01 -p
[root@Hadoop001 source]# mkdir 2016/03/02 -p
[root@Hadoop001 source]# mkdir 2016/03/03 -p
[root@Hadoop001 source]# mkdir 2016/03/04 -p

[root@Hadoop001 data]# cp flow.log ./source/2016/01/01/
[root@Hadoop001 data]# cp flow.log ./source/2016/01/02/
[root@Hadoop001 data]# cp flow.log ./source/2016/01/03/
[root@Hadoop001 data]# cp flow.log ./source/2016/03/01/
[root@Hadoop001 data]# cp flow.log ./source/2016/02/01/

将模拟数据上传到hidfs系统:

hadoop fs -put ./source /

创建外部分区表,并且location指向数据目录:
CREATE external TABLE IF NOT EXISTS part_flow(
id string,
phonenumber bigint,
mac string,
ip string,
url string,
tiele string,
colum1 string,
colum2 string,
colum3 string,
upflow int,
downflow int
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by'\t'
location '/source';

给源数据添加分区:
alter table part_flow add partition(year=2016,month=01,day=01)
location 'hdfs:///source/2016/01/01';

alter table part_flow add partition(year=2016,month=03,day=01)
location 'hdfs:///source/2016/03/01/';

alter table part_flow add partition(year=2016,month=01,day=03)
location 'hdfs:///source/2016/01/03/';

Hive的分区操作~~~~~~的更多相关文章

  1. hive 表分区操作

    hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念    hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...

  2. hive表分区操作

    1.修复表分区命令 msck repair table table_name; 2.添加表分区操作 alter table table_name add partition(month_id='201 ...

  3. Hive 基本语法操练(三):分区操作和桶操作

    (一)分区操作 Hive 的分区通过在创建表时启动 PARTITION BY 实现,用来分区的维度并不是实际数据的某一列,具体分区的标志是由插入内容时给定的.当要查询某一分区的内容时可以采用 WHER ...

  4. hive 分区操作记录

    创建分区: alter table table_name add partition (dt='20150423') location '/data/text/20150423';

  5. Hive基本语法操练

    建表规则如下: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment ...

  6. Hive sql 语法解读

    一. 创建表 在官方的wiki里,example是这种: Sql代码   CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name d ...

  7. Hive 桶的分区

    (一).桶的概念: 对于每一个表(table)或者分区, Hive可以进一步组织成桶(没有分区能分桶吗?),也就是说桶是更为细粒度的数据范围划分.Hive也是 针对某一列进行桶的组织.Hive采用对列 ...

  8. 大数据开发主战场hive (企业hive应用)

    hive在大数据套件中占很的地位,分享下个人经验. 1.在hive日常开发中,我们首先面对的就是hive的表和库,因此我要先了解库,表的命名规范和原则 如 dwd_whct_xmxx_m 第1部分为表 ...

  9. Hive学习之路 (一)Hive初识

    Hive 简介 什么是Hive 1.Hive 由 Facebook 实现并开源 2.是基于 Hadoop 的一个数据仓库工具 3.可以将结构化的数据映射为一张数据库表 4.并提供 HQL(Hive S ...

随机推荐

  1. iOS开发寻找最近公共view

    新技能 #pragma mark --寻找最近公共view + (NSArray *)superViews:(UIView *)view{ if (view==nil) { return @[]; } ...

  2. 01.PHP5.x编译详解

    ##PHP5.5编译安装 ``` wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum i ...

  3. ArcGIS API for JavaScript 4.2学习笔记[20] 使用参数查询要素(油井和地震关系)

    这个例子相当复杂.我先简单说说这个例子是干啥的. 在UI上,提供了一个下拉框.两个滑动杆,以确定三个参数,使用这三个参数进行空间查询.这个例子就颇带空间查询的意思了. 这个例子是干嘛的呢?第一个参数是 ...

  4. 用Xamarin + VS 编写Android程序体验及其与Android Studio的比较

    昨天看了微软2016Build大会,Xamarin免费了.恩,5亿美刀的家伙,哈哈,我也要体验一下..... 1. 首先在Xamarin官网下载安向导:https://www.xamarin.com/ ...

  5. (iOS)关于UITableView设置contentsize(原创)

    由于UITableView是继承自UIScrollView的,所以他是可以设置contentsize的. 但是,我在试验的过程中,初始化UITableView实例后,直接设置它的contentsize ...

  6. Jenkins: 使用groovy + job-dsl 创建并触发job

    Jenkins: 使用groovy + job-dsl 创建并触发job 背景: 我们的 Automation 测试脚本需要在10个不同语言的机器上跑,本地化测试产品. 我们用Jenkins启动测试执 ...

  7. kNN算法个人理解

    新手,有问题的地方请大家指教 训练集的数据有属性和标签 同类即同标签的数据在属性值方面一定具有某种相似的地方,用距离来描述这种相似的程度 k=1或则较小值的话,分类对于特殊数据或者是噪点就会异常敏感, ...

  8. 树型权限管理插件:jQuery Tree Multiselect详细使用指南

    1.认识jQuery Tree Multiselect 这个插件允许用户以树型的形式来呈现列表复选框的选择.多用于权限管理中用于分配不同的权限.使用文档,请参考:     https://github ...

  9. Javascript使用克隆的原型模式

    ECMAScript 5中提供了Object.create()方法. 使用这个方法很容易克隆一个一模一样的对象. var animal=function(){ this.blood=100; this ...

  10. systemtap原理及使用

    SystemTap的架构 SystemTap用于检查运行的内核的两种方法是 Kprobes和 返回探针.但是理解任何内核的最关键要素是内核的映射,它提供符号信息(比如函数.变量以及它们的地址).有了内 ...