博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HIVE-分区表详解以及实例
阅读量:4621 次
发布时间:2019-06-09

本文共 2580 字,大约阅读时间需要 8 分钟。

Hive中的分区表示什么,我们先看操作,然后再来体会

创建一个分区表,分区的单位时dt和国家名

hive> create table logs(ts bigint,line string)    > partitioned by (dt String,country string);

接下来我们创建要给分区

hive> load data local inpath '/root/hive/partitions/file1' into table logs    > partition (dt='2001-01-01',country='GB');

上面语句的效果是在hdfs系统上建立了一个层级目录

  -logs

    -dt=2001-01-01

    -country=GB

我们继续执行下面语句,先看一下什么效果

hive>  load data local inpath '/root/hive/partitions/file2' into table logs    > partition (dt='2001-01-01',country='GB');Loading data to table default.logs partition (dt=2001-01-01, country=GB)OKTime taken: 1.379 secondshive>  load data local inpath '/root/hive/partitions/file3' into table logs    > partition (dt='2001-01-01',country='US');Loading data to table default.logs partition (dt=2001-01-01, country=US)OKTime taken: 1.307 secondshive>  load data local inpath '/root/hive/partitions/file4' into table logs    > partition (dt='2001-01-02',country='GB');Loading data to table default.logs partition (dt=2001-01-02, country=GB)OKTime taken: 1.253 secondshive>  load data local inpath '/root/hive/partitions/file5' into table logs    > partition (dt='2001-01-02',country='US');Loading data to table default.logs partition (dt=2001-01-02, country=US)OKTime taken: 1.07 secondshive>  load data local inpath '/root/hive/partitions/file6' into table logs    > partition (dt='2001-01-02',country='US');Loading data to table default.logs partition (dt=2001-01-02, country=US)OKTime taken: 1.227 seconds

我们到HDFS上查看,发现建立了下面层级目录

/user/hive/warehouse/logs

├── dt=2001-01-01/
│ ├── country=GB/
│ │ ├── file1
│ │ └── file2
│ └── country=US/
│ └── file3
└── dt=2001-01-02/
├── country=GB/
│ └── file4
└── country=US/
├── file5
└── file6

 

总结:分区表的意思,其实想明白了就很简单。就是在系统上建立文件夹,把分类数据放在不同文件夹下面,加快查询速度。

关键点1:partitioned by (dt String,country string); 创建表格时,指明了这是一个分区表。将建立双层目录,第一次目录的名字和第二层目录名字规则

PARTITIONED BY子句中定义列,是表中正式的列,成为分区列。但是数据文件中并没有这些值,仅代表目录。

 

关键点2: partition (dt='2001-01-01',country='GB'); 上传数据时,把数据分别上传到不同分区中。也就是分别放在不同的子目录下。

理解分区就是文件夹分而治之,查询的时候可以当作列名来显示查询的范围。

 

 查看分区结构

hive> show partitions logs;OKdt=2001-01-01/country=GBdt=2001-01-01/country=USdt=2001-01-02/country=GBdt=2001-01-02/country=US

 

查询数据

条件限定了country='GB'目录所以只有file1,2,4的内容输出

hive> select ts,dt,line     > from logs    > where country='GB';OK1    2001-01-01    Log line 12    2001-01-01    Log line 24    2001-01-02    Log line 4

现在只查看dt=2001-01-02目录下country=US的文件夹下的数据

hive> select ts,dt,line > from logs> where dt='2001-01-02' > and country='US';OK5    2001-01-02    Log line 56    2001-01-02    Log line 6

 

转载于:https://www.cnblogs.com/sellsa/p/11242850.html

你可能感兴趣的文章
Author Agreement
查看>>
OO第一次博客作业
查看>>
netty4 HTTPclient 可添加参数
查看>>
FusionChart实现柱状图、饼状图的动态数据显示
查看>>
检查结果
查看>>
并行执行任务 Stat-Job
查看>>
Kail Linux渗透测试培训手册3第二章信息采集
查看>>
HDU 1010 Tempter of the Bone heuristic 修剪
查看>>
Django报错:__init__() missing 1 required positional argument: 'on_delete'
查看>>
2017秋软工 —— 本周PSP
查看>>
MongoDB执行计划分析详解
查看>>
盒子居中
查看>>
日志查找排序统计
查看>>
新个人博客
查看>>
goroute应用-模拟远程调用RPC
查看>>
Redis入门
查看>>
js对象的深度克隆
查看>>
java通过反射了解集合泛型的本质
查看>>
plsql程序中循环语句的使用
查看>>
毛巾行业生产管理软件系统 淮安七夕软件有限公司
查看>>