文章目录

  • 绪论
  • 创建静态分区表
    • 1、准备数据
    • 2、建表语句
    • 3、添加分区,加载数据
    • 4、进入网页(50070)查看
  • 创建动态分区表
    • 1、准备数据
    • 2、建表语句
      • 1、首先创建普通表
      • 2、加载数据
      • 3、创建动态分区表
      • 4、设置动态分区
      • 5、使用insert ... select 往动态分区插入数据
      • 6、查看分区
    • 3、进入50070查看某一个分区下内容
  • 建立分桶表
    • 建表语句
    • 打开分桶,插入数据
    • 进入50070查看

绪论

1、动态分区与静态分区的区别在于静态分区是手动指定,即通过alter table … 语句添加,而动态分区是基于查询参数的位置去推断出分区名称,即通过执行insert … select 语句建立分区
2、分区和分桶的区别分区是是对每一个数据建立父级目录的文件夹,而分桶是将数据分为多个块,不会建立父级文件夹
3、接下来我会通过建立静态分区表,动态分区表,分桶表来比较它们的不同点

创建静态分区表

1、准备数据

employee.txt内容如下

Michael|Montreal,Toronto|Male,30|DB:80|Product:DeveloperLead
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead

2、建表语句

create table employee_partition2(
name string,
work_place array<string>,
sex_age struct<sex:string,age:int>,
skills_score map<string,int>,
depart_title map<string,array<string>>)
partitioned by (year string,month string)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
stored as textfile;

3、添加分区,加载数据

alter table  employee_partition2  add partition(year='2020',month='12');
load data local inpath '/opt/hive/hiveData/employee.txt' into table employee_partition2 partition(year='2020',month='12');

4、进入网页(50070)查看



创建动态分区表

1、准备数据

employee_hr.txt内容如下

Matias McGrirl|1|945-639-8596|2011-11-24
Gabriela Feldheim|2|706-232-4166|2017-12-16
Billy O'Driscoll|3|660-841-7326|2017-02-17
Kevina Rawet|4|955-643-0317|2012-01-05
Patty Entreis|5|571-792-2285|2013-06-11
Claudetta Sanderson|6|350-766-4559|2016-11-04
Bentley Oddie|7|446-519-0975|2016-05-02
Theressa Dowker|8|864-330-9976|2012-09-26
Jenica Belcham|9|347-248-4379|2011-05-02
Reube Preskett|10|918-740-2357|2015-03-26
Mary Skeldon|11|361-159-8710|2016-03-09
Ethelred Divisek|12|995-145-7392|2016-10-18
Field McGraith|13|149-133-9607|2015-10-06
Andeee Wiskar|14|315-207-5431|2012-05-10
Lloyd Nayshe|15|366-495-5398|2014-06-28
Mike Luipold|16|692-803-9373|2011-05-14
Tallie Swaine|17|570-709-6561|2011-08-06
Worth Ledbetter|18|905-586-2348|2012-09-25
Reine Leyborne|19|322-644-5798|2015-01-05
Norby Bellson|20|736-881-5785|2012-12-31
Nellie Jewar|21|551-505-3957|2017-06-18
Hoebart Deeth|22|780-240-0213|2011-09-19
Shel Haddrill|23|623-169-5495|2014-02-04
Christalle Cervantes|24|275-309-7794|2017-01-01
Dorita Miche|25|476-242-9769|2014-10-26
Conny Bowmen|26|398-181-4961|2011-10-21
Sabra O' Donohoe|27|327-773-8515|2015-01-28
Rahal Ashbe|28|561-777-0202|2012-12-13
Tye Greenstreet|29|499-510-1700|2012-01-17
Gordy Cristoforetti|30|955-110-7073|2015-10-09
Marsha Sharkey|31|221-696-5744|2017-01-29
Corbie Cruden|32|979-583-4252|2011-08-20
Anya Easen|33|428-602-5117|2011-08-16
Clea Brereton|34|909-198-4992|2018-01-08
Kimberley Pinnijar|35|608-177-4402|2015-06-03
Wilma Mackriell|36|637-304-3580|2012-06-23
Mitzi Gorman|37|134-675-2460|2017-07-16
Ashlin Rennick|38|816-635-9974|2014-04-20
Whitaker Shedd|39|614-792-6663|2016-05-19
Mandi Stronack|40|753-688-2327|2016-04-24
Niki Driffield|41|225-867-0712|2014-02-15
Regine Agirre|42|784-395-9982|2017-05-01
Evelina Craddy|43|274-850-6569|2017-06-14
Yasmin Ubsdall|44|679-739-9660|2012-03-10
Vivianna Shoreman|45|873-271-7100|2014-09-06
Chance Murra|46|248-160-3759|2017-12-31
Ferdy Adriano|47|735-447-2642|2013-11-11
Nikolos Tichner|48|869-871-9057|2014-02-15
Doro Rushman|49|861-337-3364|2011-08-27
Lela Hinzer|50|147-386-3735|2011-06-03
Hoyt Winspar|51|120-561-6266|2016-05-05
Vicki Rimington|52|257-204-8227|2014-11-21
Louis Dalwood|53|735-885-8087|2014-02-17
Joseph Zohrer|54|178-152-4726|2015-11-04
Kennett Senussi|55|182-904-2652|2017-05-20
Letta Musk|56|534-353-2038|2013-11-04
Giulietta Glentz|57|761-390-2806|2011-09-08
Wright Frostdyke|58|932-838-9710|2015-07-15
Bat Hannay|59|404-841-2981|2015-04-04
Devlen Hutsby|60|830-520-6401|2015-07-12
Lynnea Bembrigg|61|408-264-4116|2013-02-24
Udall Nelle|62|485-420-4327|2011-07-01
Kyle Matheson|63|153-149-2140|2011-07-03
Jarid Sprowell|64|848-408-9569|2017-11-08
Jeanie Griffitt|65|442-599-1231|2018-03-09
Joana Sleith|66|264-979-0388|2017-02-13
Doris Ilyushkin|67|877-472-3918|2015-08-03
Michaelina Rennels|68|949-522-9333|2012-07-05
Onofredo Butchard|69|392-833-3926|2017-11-05
Beatrice Amis|70|963-487-6585|2015-01-24
Joyan O'Hanlon|71|952-969-7279|2017-09-22
Mikaela Cardoo|72|960-275-3958|2015-01-24
Lori Dale|73|530-116-2773|2017-07-05
Stevena Roloff|74|241-314-8328|2015-12-21
Fayth Carayol|75|907-502-3752|2012-12-04
Carita Bruun|76|117-771-8056|2017-05-31
Darnell Hardwell|77|718-247-8505|2012-05-09
Jonathon Grealy|78|136-515-3637|2014-03-29
Laurice Rosini|79|352-594-3238|2017-02-15
Emelia Auten|80|311-899-1782|2014-09-10
Trace Fontelles|81|414-607-8366|2016-03-09
Hope Sket|82|461-595-7667|2017-09-30
Cilka Heijne|83|772-704-7366|2011-08-27
Maurise Gallico|84|546-158-7983|2011-12-21
Casey Greenfield|85|204-108-7707|2012-03-18
Wes Jaffrey|86|848-465-5131|2016-02-14
Jilly Eisikowitz|87|431-355-2777|2017-02-18
Auguste Kobel|88|562-494-1360|2012-02-29
Zackariah Pietrusiak|89|810-738-9846|2012-02-25
Pearline Marcq|90|200-835-9497|2016-02-10
Sayre Osbaldeston|91|340-132-2361|2011-11-30
Floyd Cano|92|133-768-6535|2016-02-27
Ciro Arendt|93|792-967-0588|2015-11-07
Auguste Kares|94|230-184-3438|2014-03-13
Skipp Spurden|95|747-133-1382|2012-03-15
Alyssa Prydden|96|963-170-0545|2014-11-07
Orlando Pallatina|97|354-125-1208|2012-07-12
Zoe Adacot|98|704-987-0702|2015-09-29
Blaine Fawdry|99|477-109-9014|2012-07-14
Cleon Haresnape|100|625-338-3965|2014-12-04

2、建表语句

1、首先创建普通表

create table employee_hr(
name string,
id int,
num string,
date string)
row format delimited
fields terminated by '|'
stored as textfile;

2、加载数据

load data local inpath '/opt/hive/hiveData/employee_hr.txt' into table employee_hr;

3、创建动态分区表

create external table if not exists employee_hr_partition(
name string,
id int,
num string,
date string)
partitioned by (year string,month string)
stored as textfile;

4、设置动态分区

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

5、使用insert … select 往动态分区插入数据

insert into table employee_hr_partition partition(year,month) select name,id,num,date,year(date),month(date) from employee_hr;

6、查看分区

+---------------------+
|      partition      |
+---------------------+
| year=2011/month=10  |
| year=2011/month=11  |
| year=2011/month=12  |
| year=2011/month=5   |
| year=2011/month=6   |
| year=2011/month=7   |
| year=2011/month=8   |
| year=2011/month=9   |
| year=2012/month=1   |
| year=2012/month=12  |
| year=2012/month=2   |
| year=2012/month=3   |
| year=2012/month=5   |
| year=2012/month=6   |
| year=2012/month=7   |
| year=2012/month=9   |
| year=2013/month=11  |
| year=2013/month=2   |
| year=2013/month=6   |
| year=2014/month=10  |
| year=2014/month=11  |
| year=2014/month=12  |
| year=2014/month=2   |
| year=2014/month=3   |
| year=2014/month=4   |
| year=2014/month=6   |
| year=2014/month=9   |
| year=2015/month=1   |
| year=2015/month=10  |
| year=2015/month=11  |
| year=2015/month=12  |
| year=2015/month=3   |
| year=2015/month=4   |
| year=2015/month=6   |
| year=2015/month=7   |
| year=2015/month=8   |
| year=2015/month=9   |
| year=2016/month=10  |
| year=2016/month=11  |
| year=2016/month=2   |
| year=2016/month=3   |
| year=2016/month=4   |
| year=2016/month=5   |
| year=2017/month=1   |
| year=2017/month=11  |
| year=2017/month=12  |
| year=2017/month=2   |
| year=2017/month=5   |
| year=2017/month=6   |
| year=2017/month=7   |
| year=2017/month=9   |
| year=2018/month=1   |
| year=2018/month=3   |
+---------------------+

3、进入50070查看某一个分区下内容



建立分桶表

用的还是employee_hr.txt

建表语句

create table if not exists hr_bucket(
name string,
id int,
num string,
date string)
cluster by (id) into 4 buckets
row format delimited
fields terminated by '|';

打开分桶,插入数据

set hive.enforce.bucketing=true;
insert into table hr_bucket select * from employee_hr;

进入50070查看

hive静态分区,动态分区,分桶区别相关推荐

  1. hive静态与动态分区理解

    需要建立一个备份带分区的数据表,拷贝时若采用静态分区方式需要写 N 行语句,因此可以使用动态分区,节省大量时间. Hive 的分区方式:由于 Hive 实际是存储在 HDFS 上的抽象,Hive 的一 ...

  2. mysql 分桶_使用hive脚本一键动态分区、分区又分桶以及脚本步骤详细详解(亲测成功)...

    一.动态分区以及从linux本地文件映射到hive表中. partitioned.sql脚本中的创造数据: mkdir /mytemp cd mytemp vi stu_score.txt 1,zha ...

  3. hive:默认允许动态分区个数为100,超出抛出异常:

    hive:默认允许动态分区个数为100,超出抛出异常: 参考文章: (1)hive:默认允许动态分区个数为100,超出抛出异常: (2)https://www.cnblogs.com/yy3b2007 ...

  4. Hive分区(静态分区+动态分区)

    原文地址: https://www.cnblogs.com/yongjian/archive/2017/03/29/6640951.html   Hive分区的概念与传统关系型数据库分区不同.   传 ...

  5. Hive之——Hive分区(静态分区+动态分区)

    Hive分区的概念与传统关系型数据库分区不同. 传统数据库的分区方式:就oracle而言,分区独立存在于段里,里面存储真实的数据,在数据进行插入的时候自动分配分区. Hive的分区方式:由于Hive实 ...

  6. hive静态分区,动态分区 -夜幕思年华

    脚本数据 person.txt 夜幕1,china 夜幕2,china 夜幕3,china 夜幕4,china 年华1,India 年华2,India 年华3,India 年华4,India - 静态 ...

  7. 82.Hive SQL插入动态分区的异常分析

    82.1 问题情况 INSERT - SELECT语句向Parquet或者ORC格式的表中插入数据时,启用了动态分区,无法正常执行 Hive客户端: Task with the most failur ...

  8. hive实现全自动动态分区

    我们在业务中往往会遇到一种情况就是: 现有的业务已经有很多的数据堆积,并且需要根据现有的数据以分区的形式来建立数据仓库,这样的话就需要将表中的一个字段作为分区字段,以这个字段的值作为分区的依据. 那么 ...

  9. Hive入门(三)分桶

    1 什么是分桶 上一篇说到了分区,分区中的数据可以被进一步拆分成桶,bucket.不同于分区对列直接进行拆分,桶往往使用列的哈希值进行数据采样.在分区数量过于庞大以至于可能导致文件系统崩溃时,建议使用 ...

最新文章

  1. 干货|深度学习之过拟合和正则化
  2. ios调用restful接口_Postman调用https异常解决
  3. 51CTO 新人报道
  4. 云堆栈三大服务模式解析
  5. rxjava 被观察者_RxJava:从未来到可观察
  6. TensorFlow10-多层神经网络建模,存储和载入
  7. Makefile模板的改进
  8. windows鼠标响应相关知识
  9. kubernetes Ingress是什么
  10. hao643.com劫持(IE和Chrome等被修改快捷方式跳转到hao123.com)
  11. QT Creator 创建qrc文件和使用资源文件
  12. 优秀课件笔记之WINDOWS XP 操作系统
  13. 计算机专业技术职务评审表怎么填,专业技术职务怎么填
  14. 利用python爬取租房信息网_Python3爬虫实战:以爬取小猪短租租房信息为例
  15. spring cloud 复杂类型 FeignClient 避免linkedHashMap
  16. SPSS多选题数据如何录入与分析?——SPSS多重响应分析的详细讲解
  17. 利用pandas读取Excel文件数据
  18. html5 原生插件,前端必备插件之纯原生JS的瀑布流插件Macy.js
  19. linux原理与应用 武汉大学,Linux原理与应用 郑鹏 武汉大学出版社 9787307060876
  20. 干货!12个程序员证书​,含金量超高

热门文章

  1. Duplicate Entry
  2. SEO 搜索引擎优化(Google 排名)
  3. linux硬盘和分区,Linux硬盘和分区
  4. 在vscode里配置ESLint和Prettier
  5. 月结1 - 开关会计期间[OB52]
  6. 【python与数据分析】Matplotlib数据可视化
  7. OECD富裕经济体系通胀飙升至 25 年高位-全球薪资停滞不前
  8. 深度学习:人工智能的核心
  9. 如何把get请求改成post
  10. linux磁盘配额步骤详细步骤,Linux磁盘配额命令及步骤.doc