一、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. window7使用svn(svn系列 客户端 知识二总结)

     ♣eclipse插件subclipse  ♣TortoiseSVN  ♣svn操作  ♣注意事项 使用svn可以在eclipse直接装subclipse或者在windows使用TortoiseSVN ...

  2. 一个web应用的诞生(10)--关注好友

    下面回到首页中,使用一个账户登录,你肯定已经注意到了这里的内容: 没错,现在都是写死的一些固定信息,其中分享数量很容易就可以获取,只需要修改首页模板: <p class="text-m ...

  3. MVC 5 + EF6 完整教程15 -- 使用DI进行解耦

    如果大家研究一些开源项目,会发现无处不在的DI(Dependency Injection依赖注入). 本篇文章将会详细讲述如何在MVC中使用Ninject实现DI 文章提纲 场景描述 & 问题 ...

  4. Visual Studio 2017无法加载Visual Studio 2015创建的SharePoint解决方案

    前几天安装了最新的Visual Studio 2017企业版,发现无法打开之前使用Visual Studio 2015创建的SharePoint 2016解决方案,提示"需要更新" ...

  5. 基于Spring Security 的JSaaS应用的权限管理

    1. 概述 权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源.资源包括访问的页面,访问的数据等,这在传统的应用系统中比较常见.本文介绍的则是基于Saas系统 ...

  6. es6基础系列一:let和const

    let 声明变量,可以说是具有作用域的var,用于声明变量,主要规则如下: 1 let声明的变量拥有块级作用域 { let i = 1; console.log(i); // 1 } console. ...

  7. 为JQuery EasyUI 表单组件增加“焦点切换”功能

    1.背景说明 在使用 JQuery  EasyUI 各表单组件时,实际客户端页面元素是由 JQuery EasyUI 生成的,元素的焦点切换,虽然 Tab 键可以正常用,但顺序控制属性 tabinde ...

  8. [Android]Gradle 插件 DiscardFilePlugin(class注入&清空类和方法)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6732128.html Android Gradle 插件 Di ...

  9. angular apply

    <div ng-controller="firstController"> {{date}} </div> <script> var first ...

  10. ubuntu 下安装Angular2-cli脚手架

    一.首先需要安装node,npm. 请到nodejs官网进行下载并跟据提示进行安装 版本号中间偶数为稳定版本建议下载 https://nodejs.org/en/ node相关操作 https://n ...