一、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. c++内存优化:二级间接索引模式内存池

    /********************************************************* 在一些不确定内存总占用量的情形下,频繁的使用new申请内存,再通过链表 进行索引似 ...

  2. https证书申请流程和简介

    HTTPS证书是什么 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安 ...

  3. 漫谈PHP代码规范

    前言 虽说PHP是世界上最好的语言,但是写出来的PHP代码却往往不是最美观的.究其原因,可能正式因为PHP简单易上手,适合快速迭代的特性,导致了我们沉浸在迅速完成需求迭代的窃喜中,却忘记了规范性.忽略 ...

  4. 关于ARM内核与架构的解释

    本文摘自某论坛某位大神的一段回复,经典至极,copy来己用! 只要你玩过ARM内核的芯片,那么关于内核和架构,我想应该或多或少的困惑过你,看了下面的介绍,你应该会清楚很多! 好比你盖房子,刚开始因为水 ...

  5. 【Java并发】详解 AbstractQueuedSynchronizer

    前言 队列同步器 AbstractQueuedSynchronizer(以下简称 AQS),是用来构建锁或者其他同步组件的基础框架.它使用一个 int 成员变量来表示同步状态,通过 CAS 操作对同步 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十一)数据层优化-druid监控及慢sql记录

    本文提要 前文也提到过druid不仅仅是一个连接池技术,因此在将整合druid到项目中后,这一篇文章将去介绍druid的其他特性和功能,作为一个辅助工具帮助提升项目的性能,本文的重点就是两个字:监控. ...

  7. javascript中类式继承和原型式继承的实现方法和区别

    在所有面向对象的编程中,继承是一个重要的话题.一般说来,在设计类的时候,我们希望能减少重复性的代码,并且尽量弱化对象间的耦合(让一个类继承另一个类可能会导致二者产生强耦合).关于“解耦”是程序设计中另 ...

  8. servlet研究学习总结--OutputStream和PrintWriter的区别

    当用户和浏览器其进行交互时,会给服务器发送http请求,Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.requ ...

  9. hibernate 插入数据时让数据库默认值生效

    用hibernate做数据库插入操作时,在数据库端已经设置了对应列的默认值,但插入的数据一直为null.查找资料发现,原来是hibernate的配置项在作怪. Hibernate允许我们在映射文件里控 ...

  10. 使用Express开发个人网站(一)

    Express,基于Node.js平台,快速.开放.极简的 web 开发框架. Node的出现,让js有了运行在服务器端的可能,基于此的Express,可以快速,简单的搭建起一个服务器与个人网站. 安 ...