当前位置:首页 >> 编程语言 >> 【Python大数据笔记_day07_hive中的分区表、分桶表以及一些特殊类型】,a390

【Python大数据笔记_day07_hive中的分区表、分桶表以及一些特殊类型】,a390

0evadmin 编程语言 1
文件名:【Python大数据笔记_day07_hive中的分区表、分桶表以及一些特殊类型】,a390 【Python大数据笔记_day07_hive中的分区表、分桶表以及一些特殊类型】 分区表

 分区表的特点/好处:需要产生分区目录,查询的时候使用分区字段筛选数据,避免全表扫描从而提升查询效率

效率上注意:如果分区表在查询的时候呀没有使用分区字段去筛选数据,效率不变

分区字段名注意:分区字段名不能和原有的字段名重复,因为分区字段名要作为字段拼接到表后

 一级分区

创建分区表:create [external] table [if not exists] 表名(字段名 字段类型 , 字段名 字段类型 , ... )partitioned by (分区字段名 分区字段类型)... ; 

自动生成分区目录并插入数据: load data [local] inpath '文件路径' into table 分区表名 partition (分区字段名='值');

注意: 如果加local后面文件路径应该是linux本地路径,如果没有加那么就是hdfs文件路径

-- 创建库使用库create database hive3;use hive3;-- 演示分区表-- 1.一级分区表-- 建表create table one_part_order(oid string,name string,price double,num int)partitioned by (year string)row format delimitedfields terminated by ' ';-- 加载数据-- 先在hdfs的source目录下准备好订单相关数据文件-- 使用load加载数据到分区表中load data inpath '/source/order202251.txt' into table one_part_order partition (year=2022);load data inpath '/source/order2023415.txt' into table one_part_order partition (year='2023');load data inpath '/source/order202351.txt' into table one_part_order partition (year='2023');load data inpath '/source/order202352.txt' into table one_part_order partition (year='2023');-- 验证数据select * from one_part_order limit 20;/*分区表特点去hdfs验证分区表的本质就是分目录存储各个小文件通过查询发现分区字段最终效果作为一个字段拼接到表最后*/-- 分区表的好处:避免全表扫描,提升查询效率select * from one_part_order where year='2022';-- 注意: 如果查询的时候条件不是分区字段,效率不会改变select * from one_part_order where price=20;  多级分区

创建分区表: create [external] table [if not exists] 表名(字段名 字段类型 , 字段名 字段类型 , ... )partitioned by (一级分区字段名 分区字段类型, 二级分区字段名 分区字段类型 , ...) ; 

自动生成分区目录并插入数据: load data [local] inpath '文件路径' into table 分区表名 partition (一级分区字段名='值',二级分区字段名='值' , ...);

注意: 如果加local后面文件路径应该是linux本地路径,如果没有加那么就是hdfs文件路径

-- 2.多级分区表-- 创建表create table multi_part_order(oid string,name string,price float,num int)partitioned by (year string,month string,day string)row format delimitedfields terminated by ' ';-- 加载数据-- 思考数据文件在哪里?如果想从hdfs加载,怎么操作?上传到hdfs指定位置load data inpath '/source/order202251.txt' into table multi_part_order partition (year=2022,month=05,day=01);load data inpath '/source/order202351.txt' into table multi_part_order partition (year=2023,month=05,day=01);load data inpath '/source/order202352.txt' into table multi_part_order partition (year=2023,month=05,day=02);load data inpath '/source/order2023415.txt' into table multi_part_order partition (year=2023,month=04,day=15);-- 验证数据select * from multi_part_order;-- 分区表的好处:避免全表扫描,提升查询效率-- 需求: 统计2023年商品总销售额select sum(price*num) from multi_part_order where year='2023'; -- 提升效率-- 需求: 统计2023年5月份商品总销售额select sum(price*num) from multi_part_order where year='2023'and month='5'; -- 提升效率-- 需求: 统计2023年5月1日的商品总销售额select sum(price*num) from multi_part_order where year='2023'and month='5' and day='1'; -- 提升效率  分区操作

添加分区: alter table 分区表名 add partition (分区字段名='值' , ...);

删除分区: alter table 分区表名 drop partition (分区字段名='值' , ...);

修改分区名: alter table 分区表名 partition (分区字段名='旧值' , ...) rename to partition (分区字段名='新值' , ...);

查看所有分区: show partitons 分区表名;

同步/修复分区: msck repair table 分区表名;

-- 分区操作-- 注意: 先确定有一级分区和多级分区表,如果没有先创建再做分区操作select * from one_part_order limit 20;select * from multi_part_order limit 20;-- 添加分区(本质在hdfs上创建分区目录)alter table one_part_order add partition (year=2024);alter table multi_part_order add partition (year=2024,month=5,day=1);-- 修改分区(本质在hdfs上修改分区目录名)alter table one_part_order partition (year=2024) rename to partition (year=2030);alter table multi_part_order partition (year=2024,month=5,day=1) rename to partition (year=2030,month=6,day=10);-- 查看所有分区show partitions one_part_order;show partitions multi_part_order;-- 删除分区alter table multi_part_order drop partition (year=2030,month=6,day=10);alter table multi_part_order drop partition (year=2023,month=5,day=2);alter table multi_part_order drop partition (year=2023,month=5);alter table multi_part_order drop partition (year=2023,month=4);alter table multi_part_order drop partition (year=2022);-- 如果在hdfs上创建符合分区目录格式的文件夹,可以使用msck repair修复-- 举例:手动创建一个year=2033目录msck repair table one_part_order;msck repair table multi_part_order;-- 修复后再次查看所有分区show partitions one_part_order;show partitions multi_part_order;  Hadoop_hive文档

hive文档: https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties hdfs文档: https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml yarn文档: https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-common/yarn-default.xml mr文档: https://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/mapred-default.xml

 分桶表

 分桶表特点/好处:需要产生分桶文件,查询的时候特定操作上提升效率(过滤,join,分组以及抽样)

效率上注意:如果分桶表在查询数据的时候没有使用分桶字段去筛选,效率不变

分桶字段名注意:分桶字段名必须是原有的字段名,因为分桶需要根据对应的字段值取余数把余数相同的数据放到一个桶文件中

重要参数

-- 默认开启,hive2.x版本已经被移除 set hive.enforce.bucketing; -- 查看未定义因为已经被移除 set hive.enforce.bucketing=true; -- 修改

-- 查看reduce数量 -- 参数优先级: set方式 > hive文档 > hadoop文档 set mapreduce.job.reduces; -- 查看默认-1,代表自动根据桶数量匹配reduce数量 set mapreduce.job.reduces=3; -- 设置参数

 基础分桶表

创建基础分桶表:   create [external] table [if not exists] 表名(     字段名 字段类型  ) clustered by (分桶字段名)  into 桶数量 buckets ;

-- 1.创建基础分桶表,要求分3个桶create table course_base (cid int,cname string,sname string)clustered by(cid) into 3 bucketsrow format delimited fields terminated by '\t';-- 2.load方式加载数据-- 前提: 已经上传course.txt文件到hdfs的/source目录下load data inpath '/source/course.txt' into table course_base;-- 3.查询数据,观察结果select * from course_base; 分桶表排序

创建基础分桶表,然后桶内排序:    create [external] table [if not exists] 表名(     字段名 字段类型  ) clustered by (分桶字段名)  sorted by(排序字段名 asc|desc)   # 注意:asc升序(默认) desc降序 into 桶数量 buckets ;

-- 1.创建基础分桶表,要求分3个桶,桶内根据cid降序create table course_sort (cid int,cname string,sname string)clustered by(cid) sorted by (cid desc) into 3 bucketsrow format delimited fields terminated by '\t';-- 2.加载数据-- 还是使用/source/course.txt数据文件load data inpath '/source/course.txt' into table course_sort;-- 3.查询数据,观察结果select * from course_sort; 练习

一直课程表course.txt数据文件,要求建表,根据sname分桶,然后桶内再根据cid升序排序,观察结果

注意事项

数据倾斜问题:分桶字段值如果大量重复,相同的会分到同一个桶内,导致数据倾斜

-- 1.创建基础分桶表,要求分3个桶,桶内根据cid降序create table course_test (cid int,cname string,sname string)clustered by(sname) sorted by (cid) into 3 bucketsrow format delimited fields terminated by '\t';-- 2.加载数据-- 还是使用/source/course.txt数据文件load data inpath '/source/course.txt' into table course_test;-- 3.查询数据,观察结果select * from course_test; 分桶原理

分桶原理:

如果是数值类型分桶字段:直接使用数值对桶数量取模

如果是字符串类型分桶字段:底层会使用hash算法计算出一个数字然后再对桶数量取模

Hash:Hash是一种数据加密算法,其原理我们不去详细讨论,我们只需要知道其主要特征:同样的值被hash加密后的结果是一致的

举例:字符串'binzi'被hash后的结果是93742710(仅作为示意),那么无论计算多少次,字符串“binzi”的结果都会是93742710。 计算余数: hash('binzi')%3==0   注意: 同样的数据得到的结果一致,如’binzi’ hash取模结果是0,无论计算多少次,它的取模结果都是0

 分区表和分桶表的区别

分区表     创建表的时候使用关键字: partition by (分区字段名 分区字段类型)     分区字段名注意事项: 是一个新的字段,需要指定类型,且不能和其他字段重名     分区表好处: 使用分区字段作为条件的时候,底层直接找到对应的分区目录,能够避免全表扫描,提升查询效率     分区表最直接的效果: 在hfds表目录下,分成多个分区目录(year=xxxx,month=xx,day=xx)     不建议直接上传文件在hdfs表根路径下: 分区表直接不能识别对应文件中数据,因为分区表会找分区目录下的数据文件     使用load方式加载hdfs中文件: 本质是移动文件到对应分区目录下

分桶表     创建表的时候使用关键字: clustered by (分桶字段名) into 桶数量 buckets     分桶字段名注意事项: 是指定一个已存在的字段,不需要指定类型     分桶表好处: 使用分桶字段做抽样等特定操作的时候,也能提升性能效率     分桶表最直接的效果: 在hdfs表目录或者分区目录下,分成多个分桶文件(000000_0,000001_0,000002_0...)     不建议直接上传文件在hdfs表根路径下: 分桶表可以识别对应文件中数据,但是并没有分桶效果,也是不建议的     使用load方式加载hdfs中文件: 本质是复制数据到各个分桶文件中

 复杂类型 hive的SerDe机制

其中ROW FORMAT是语法关键字,DELIMITED和SERDE二选其一。本次我们主要学习DELIMITED关键字相关知识点 如果使用delimited: 表示底层默认使用的Serde类:LazySimpleSerDe类来处理数据。 如果使用serde:表示指定其他的Serde类来处理数据,支持用户自定义SerDe类。

Hive默认的序列化类: LazySimpleSerDe 包含4种子语法,分别用于指定字段之间、集合元素之间、map映射 kv之间、换行的分隔符号。 在建表的时候可以根据数据的类型特点灵活搭配使用。 COLLECTION ITEMS TERMINATED BY '分隔符' : 指定集合类型(array)/结构类型(struct)元素的分隔符 MAP KEYS TERMINATED BY '分隔符' : 表示映射类型(map)键值对之间用的分隔

 复杂类型

复杂类型建表格式:  ... [row format delimited] # hive的serde机制     [fields terminated by '字段分隔符'] # 自定义字段分隔符固定格式     [collection ITEMS terminated by '集合分隔符'] # 自定义array同类型集合和struct不同类型集合     [map KEYS terminated by '键值对分隔符'] # 自定义map映射kv类型     [lines terminated by '\n'] # # 默认即可 ...;

hive复杂类型:   array  struct  map

array类型: 又叫数组类型,存储同类型的单数据的集合      建表指定类型:  array<数据类型>      取值: 字段名[索引]   注意: 索引从0开始      获取长度: size(字段名)      判断是否包含某个数据: array_contains(字段名,某数据)

struct类型: 又叫结构类型,可以存储不同类型单数据的集合      建表指定类型: struct<子字段名1:数据类型1, 子字段名2:数据类型2 , ...>      取值: 字段名.子字段名n      map类型: 又叫映射类型,存储键值对数据的映射(根据key找value)     建表指定类型: map<key类型,value类型>     取值: 字段名[key]     获取长度: size(字段名)     获取所有key: map_keys()     获取所有value: map_values()

 srray示例

需求:已知data_for_array_type.txt文件,存储了学生以及居住过的城市信息,要求建hive表把对应的数据存储起来

-- 演示使用简单类型映射数据-- 创建表create table test_array1(name string,location string)row format delimitedfields terminated by '\t';-- 加载数据load data inpath '/source/data_for_array_type.txt' into table test_array1;-- 验证数据select * from test_array1;-- 演示使用array类型映射数据-- 创建表create table test_array2(name string,location array<string>)row format delimitedfields terminated by '\t'collection items terminated by ',';-- 加载数据load data inpath '/source/data_for_array_type.txt' into table test_array2;-- 验证数据select * from test_array2;-- 需求: 查询zhangsan的地址有几个?select size(location) from test_array2 where name = 'zhangsan';-- 需求: 查询zhangsan的第二个地址?select location[1] from test_array2 where name = 'zhangsan';-- 需求: 查询zhangsan是否在tianjin住过?select array_contains(location,'tianjin') from test_array2 where name = 'zhangsan'; struct示例

 需求: 已知data_for_struct_type.txt文件存储了用户姓名和年龄基本信息,要求建hive表把对应的数据存储起来

-- 演示使用简单类型映射数据-- 创建表create table test_struct1(id int,info string)row format delimitedfields terminated by '#';-- 加载数据(前提hdfs必须有对应文件)load data inpath '/source/data_for_struct_type.txt' into table test_struct1;-- 验证数据select * from test_struct1;-- 演示struct类型映射数据-- 创建表create table test_struct2(id int,info struct<name:string,age:int>)row format delimitedfields terminated by '#'collection items terminated by ':';-- 加载数据(前提hdfs必须有对应文件)load data inpath '/source/data_for_struct_type.txt' into table test_struct2;-- 验证数据select * from test_struct2;-- 需求: 获取所有的姓名select info.name from test_struct2;-- 需求: 获取所有的年龄select info.age from test_struct2;  map示例

 需求: 已知data_for_map_type.txt文件存储了每个学生详细的家庭信息,要求建hive表把对应数据存储起来

-- 演示简单类型映射数据-- 创建表create table test_map1(id int,name string,info string,age int)row format delimitedfields terminated by ',';-- 加载数据(前提hdfs有对应数据文件)load data inpath '/source/data_for_map_type.txt' into table test_map1;-- 验证数据select * from test_map1;-- 演示map类型的应用-- 创建表create table test_map2(id int,name string,info map<string,string>,age int)row format delimitedfields terminated by ','collection items terminated by '#'map keys terminated by ':';-- 加载数据(前提hdfs有对应数据文件)load data inpath '/source/data_for_map_type.txt' into table test_map2;-- 验证数据select * from test_map2;-- 需求: 查看所有人的father,mother信息select name,info['father'] as father ,info['mother'] as mother from test_map2;-- 需求: 查看所有人的家庭相关角色select name,map_keys(info) from test_map2;-- 需求: 查看所有人的家庭相关姓名select name,map_values(info) from test_map2;-- 需求: 查看所有人的家庭相关人员个数select name,size(info) as cnt from test_map2;-- 需求: 查看马大云是否包含brother角色select name,array_contains(map_keys(info),'brother') from test_map2 where name = '马大云';
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接