hive学习笔记之四:分区表

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
partition(city=‘shenzhen’);

  1. 再执行一次导入操作,命令如下,city的值从前面的shenzhen改为guangzhou:

load data

local inpath ‘/home/hadoop/temp/202010/25/009.txt’

into table t9

partition(city=‘guangzhou’);

  1. 查询数据,可见一共四条数据,city共有两个值:

hive> select * from t9;

OK

t9.name t9.age t9.city

tom 11 guangzhou

jerry 12 guangzhou

tom 11 shenzhen

jerry 12 shenzhen

Time taken: 0.104 seconds, Fetched: 4 row(s)

  1. 前面曾提到分区实际上是不同的子目录,来看一下是不是如此,如下图,红框是t9的文件目录,下面有两个子目录city=guangzhou和city=shenzhen:

在这里插入图片描述

  1. 查看子目录里面文件的内容,可见每条记录只有name和age两个字段:

[hadoop@node0 bin]$ ./hadoop fs -ls /user/hive/warehouse/t9/city=guangzhou

Found 1 items

-rwxr-xr-x 3 hadoop supergroup 16 2020-10-31 16:47 /user/hive/warehouse/t9/city=guangzhou/009.txt

[hadoop@node0 bin]$ ./hadoop fs -cat /user/hive/warehouse/t9/city=guangzhou/009.txt

tom,11

jerry,12

[hadoop@node0 bin]$

以上就是以单个字段做静态分区的实践,接下来尝试多字段分区;

静态分区(多字段分区)

  1. 新建名为t10的表,有两个分区字段:province和city,建表语句:

create table t10 (name string, age int)

partitioned by (province string, city string)

row format delimited

fields terminated by ‘,’;

  1. 上述建表语句中,分区字段province写在了city前面,这就意味着第一级子目录是province值,每个province子目录,再按照city值建立二级子目录,图示如下:

在这里插入图片描述

  1. 第一次导入,province=‘shanxi’, city=‘xian’:

load data

local inpath ‘/home/hadoop/temp/202010/25/009.txt’

into table t10

partition(province=‘shanxi’, city=‘xian’);

  1. 第二次导入,province=‘shanxi’, city=‘xian’:

load data

local inpath ‘/home/hadoop/temp/202010/25/009.txt’

into table t10

partition(province=‘shanxi’, city=‘hanzhong’);

  1. 第三次导入,province=‘guangdong’, city=‘guangzhou’:

load data

local inpath ‘/home/hadoop/temp/202010/25/009.txt’

into table t10

partition(province=‘guangdong’, city=‘guangzhou’);

  1. 第四次导入,province=‘guangdong’, city=‘shenzhen’:

load data

local inpath ‘/home/hadoop/temp/202010/25/009.txt’

into table t10

partition(province=‘guangdong’, city=‘shenzhen’);

  1. 全部数据如下:

hive> select * from t10;

OK

t10.name t10.age t10.province t10.city

tom 11 guangdong guangzhou

jerry 12 guangdong guangzhou

tom 11 guangdong shenzhen

jerry 12 guangdong shenzhen

tom 11 shanxi hanzhong

jerry 12 shanxi hanzhong

tom 11 shanxi xian

jerry 12 shanxi xian

Time taken: 0.129 seconds, Fetched: 8 row(s)

  1. 查看hdfs文件夹,如下图,一级目录是province字段的值:

在这里插入图片描述

  1. 打开一个一级目录,如下图,可见二级目录是city的值:

在这里插入图片描述

  1. 查看数据:

[hadoop@node0 bin]$ ./hadoop fs -cat /user/hive/warehouse/t10/province=shanxi/city=hanzhong/009.txt

tom,11

jerry,12

  1. 以上就是静态分区的基本操作,可见静态分区有个不便之处:新增数据的时候要针对每一个分区单独使用load命令去操作,这时候使用动态分区来解决这个麻烦;

动态分区

  1. 动态分区的特点就是不用指定分区目录,由hive自己选择;

  2. 执行以下命令开启动态分区功能:

set hive.exec.dynamic.partition=true

  1. 名为hive.exec.dynamic.partition.mode的属性,默认值是strict,意思是不允许分区列全部是动态的,这里改为nostrict以取消此禁制,允许全部分区都是动态分区:

set hive.exec.dynamic.partition.mode=nostrict;

  1. 建一个外部表,名为t11,只有四个字段:

create external table t11 (name string, age int, province string, city string)

row format delimited

fields terminated by ‘,’

location ‘/data/external_t11’;

  1. 创建名为011.txt的文件,内容如下:

tom,11,guangdong,guangzhou

jerry,12,guangdong,shenzhen

tony,13,shanxi,xian

john,14,shanxi,hanzhong

  1. 将011.txt中的四条记录载入表t11:

load data

local inpath ‘/home/hadoop/temp/202010/25/011.txt’

into table t11;

  1. 接下来要,先创建动态分区表t12,再把t11表的数据添加到t12中;

  2. t12的建表语句如下,按照province+city分区:

create table t12 (name string, age int)

partitioned by (province string, city string)

row format delimited

fields terminated by ‘,’;

最后

image.png

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
,先创建动态分区表t12,再把t11表的数据添加到t12中;

  1. t12的建表语句如下,按照province+city分区:

create table t12 (name string, age int)

partitioned by (province string, city string)

row format delimited

fields terminated by ‘,’;

最后

[外链图片转存中…(img-NQXkFJRM-1714427456107)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值