095-BigData-23Hive分区及DML操作

上一篇:094-BigData-22Hive数据类型及操作

一、Hive分区

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

1、分区表基本操作

1)引入分区表(需要根据日期对日志进行管理)
/user/hive/warehouse/log_partition/20180702/20180702.log
/user/hive/warehouse/log_partition/20180703/20180703.log
/user/hive/warehouse/log_partition/20180704/20180704.log
2)创建分区表语法

hive (default)> create table dept_partition(
               deptno int, dname string, loc string
               )
               partitioned by (month string)
               row format delimited fields terminated by '\t';

3)加载数据到分区表中
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201907');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201908');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201909');
4)查询分区表中数据
单分区查询
hive (default)> select * from dept_partition where month='201907';
多分区联合查询
hive (default)> select * from dept_partition where month='201908'
union
select * from dept_partition where month='201907'
union
select * from dept_partition where month='201909';

_u3.deptno _u3.dname _u3.loc _u3.month
10 ACCOUNTING NEW YORK 201807
10 ACCOUNTING NEW YORK 201808
10 ACCOUNTING NEW YORK 201809
20 RESEARCH DALLAS 201807
20 RESEARCH DALLAS 201808
20 RESEARCH DALLAS 201809
30 SALES CHICAGO 201807
30 SALES CHICAGO 201808
30 SALES CHICAGO 201809
40 OPERATIONS BOSTON 201807
40 OPERATIONS BOSTON 201808
40 OPERATIONS BOSTON 201809
5)增加分区
创建单个分区
hive (default)> alter table dept_partition add partition(month='201806') ;
同时创建多个分区
hive (default)> alter table dept_partition add partition(month='201805') partition(month='201804');
注:增加多个分区之间用空格" "隔开,删除多个分区用","隔开
6)删除分区
删除单个分区
hive (default)> alter table dept_partition drop partition (month='201804');
同时删除多个分区
hive (default)> alter table dept_partition drop partition (month='201805'), partition (month='201806');
7)查看分区表有多少分区
hive>show partitions dept_partition;
8)查看分区表结构
hive>desc formatted dept_partition;

# Partition Information          
# col_name              data_type               comment             
month                   string    

2 、分区表注意事项

1)创建二级分区表

hive (default)> create table dept_partition2(
               deptno int, dname string, loc string
               )
               partitioned by (month string, day string)
               row format delimited fields terminated by '\t';

2)正常的加载数据
(1)加载数据到二级分区表中
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition2 partition(month='201809', day='13');
(2)查询分区数据
hive (default)> select * from dept_partition2 where month='201809' and day='13';
3)把数据直接上传到分区目录上,让分区表和数据产生关联的两种方式
(1)方式一:上传数据后修复
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=12;
查询数据(查询不到刚上传的数据)
hive (default)> select * from dept_partition2 where month='201809' and day='12';
执行修复命令
hive>msck repair table dept_partition2;
再次查询数据
hive (default)> select * from dept_partition2 where month='201809' and day='12';
(2)方式二:上传数据后添加分区
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=11;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=11;
执行添加分区
hive (default)> alter table dept_partition2 add partition(month='201809', day='11');
查询数据
hive (default)> select * from dept_partition2 where month='201809' and day='11';
(3)方式三:上传数据后load数据到分区
创建目录
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=10;
上传数据
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201809',day='10');
查询数据
hive (default)> select * from dept_partition2 where month='201809' and day='10';

五、DML数据操作

续上一篇,多以是五~

5.1 数据导入

5.1.1 向表中装载数据(Load)
1)语法
hive>load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加载数据
(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表名
(7)partition:表示上传到指定分区
2)实操案例
(0)创建一张表
hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';
(1)加载本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;
(2)加载HDFS文件到hive中
上传文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加载HDFS上数据
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' into table default.student;
(3)加载数据覆盖表中已有的数据
上传文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加载数据覆盖表中已有的数据
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' overwrite into table default.student;
注:load hdfs的数据相当于mv文件到另一个目录中,原目录文件消失

5.1.2 通过查询语句向表中插入数据(Insert)
1)创建一张分区表
hive (default)> create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2)基本插入数据

hive (default)> insert into table  student partition(month='201907') values(1,'wangwu');

3)基本模式插入(根据单张表查询结果)
hive (default)> insert overwrite table student partition(month='201908')
select id, name from student where month='201907';
4)多插入模式(根据多张表查询结果)
hive (default)> from student
insert overwrite table student partition(month='201909')
select id, name where month='201907'
insert overwrite table student partition(month='201910')
select id, name where month='201908';

5.1.3 查询语句中创建表并加载数据(As Select)
详见4.5.1章创建表。
根据查询结果创建表(查询的结果会添加到新创建的表中)

create table if not exists student3
as select id, name from student;
这种方式不能创建外部表。
 external
CREATE-TABLE-AS-SELECT cannot create external table

5.1.4 创建表时通过Location指定加载数据路径
1)创建表,并指定在hdfs上的位置
hive (default)> create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2)上传数据到hdfs上
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;
3)查询数据
hive (default)> select * from student5;
5.1.5 Import数据到指定Hive表中
注意:先用export导出后(导出的数据目录里面附带有元数据),再import数据导入。同在HDFS上是Copy级操作

hive (default)> export table default.student to '/user/hive/warehouse/export/student';
hive (default)>create table student5(
           >                id int, name string
           >                )
           >                partitioned by (month string)
           >                row format delimited fields terminated by '\t';
hive (default)> import table student5 from '/user/hive/warehouse/export/student';
5.2 数据导出

5.2.1 Insert导出
1)将查询的结果导出到本地,数据之间无间隔

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
            select * from student;

2)将查询的结果格式化导出到本地,数据之间"\t"间隔

hive (default)> insert overwrite local directory '/root/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'             select * from student;

3)将查询的结果导出到HDFS上(没有local)

hive (default)> insert overwrite directory '/user/AncientMing/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
             select * from student;

注:虽然同是HDFS,但不是copy操作

5.2.2 Hadoop命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201809/000000_0  /opt/module/datas/export/student3.txt;

5.2.3 Hive Shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file(自己创建))

[AncientMing@bigdata111hive]$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

5.2.4 Export导出到HDFS上

hive (default)> export table default.student to '/user/hive/warehouse/export/student';
5.3 清除表中数据(Truncate)

注意:Truncate只能删除管理表,不能删除外部表中数据

hive (default)> truncate table student;

下一篇:096-BigData-24Hive查询排序分桶

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容