【博学谷学习记录】超强总结,用心分享| Hive的表操作

1.Hive表操作1-内部表和外部表

1.1内部表

1、内部表是私有表,一旦给表加载数据之后,内部表认为这份数据就是他独占的,表一旦删除,表数据文件会跟着全部删除,如果在应用中,数据是部门内部的,或者个人的,则表可以设置为内部表,不会对其他人造成影响。
2、外部表创建语法: create  table 表

use myhive;
-- 1、创建内部表-使用默认分隔符:'\001'
create table stu(id int, name string);
-- 加载数据
insert into stu values (1,'zs');
insert into stu values (2,'ls');
select * from stu;

-- 2、创建内部表-使用指定分隔符: ','

create table stu2(id int, name string)
row format delimited fields terminated by ',';
insert into stu2 values (1,'zs');
insert into stu2 values (2,'ls');

-- 开启本地模式
set hive.stats.column.autogather=false;
set hive.exec.mode.local.auto=true;  --开启本地mr


-- 3、通过复制表结构来建表
create table stu3 as select * from stu2;  -- 即复制表结构,又复制数据
create table stu4 like stu2;   -- 仅复制表结构

-- 4、查看表的元数据信息
 desc   stu2;          -- 查看字段信息(简单)
 desc formatted stu2;  -- 查看详细的元数据信息


-- 5、删除表
-- 内部表删除,将表数据和元数据全部删除
drop table stu2;
select * from stu;

-- 6、给表加载数据(最正式的) - 本地 -复制

create table stux(id int, name string)
row format delimited fields terminated by '\t';


-- 从本地加载--复制
load data local inpath '/export/data/hivedatas/1.txt' into table stux;
select * from stux;


-- 6、给表加载数据(最正式的) - HDFS - 剪切
create table stuy(id int, name string)
row format delimited fields terminated by '\t';

load data  inpath '/input/hivedatas/1.txt' into table stuy;

select * from stuy;

1.2外部表

1、外部表是公有表,一旦给表加载数据之后,外部表认为这份数据大家的,表一旦删除,表数据文件不会删除,只删除表和文件之间的映射关系,如果在应用中,数据是各部门共享,则可以设置为外部表,你的表只是对文件有访问权。
2、外部表创建语法: create external table 表...


-- 1、创建外部表
create external table teacher
(
    tid   string,
    tname string
) row format delimited fields terminated by '\t';

create external table student
(
    sid    string,
    sname  string,
    sbirth string,
    ssex   string
) row format delimited fields terminated by '\t';

-- 加载数据
load data local inpath '/export/data/hivedatas/student.txt' into table student;
load data local inpath '/export/data/hivedatas/teacher.txt' into table teacher;

select * from student;
select * from teacher;


-- 删除表,只删除元数据,不会删除表数据
drop table teacher;

2. 分区表

1、分区表就是对一个表的文件数据进行分类管理,表现形式就是有很多的文件夹(dt=2019-02-27)
2、分区表的作用是以后查询时,我们可以手动指定对应分区的数据,避免全表扫描,提高查询效率
3、专业的介绍
所谓的分区表,指的就是将数据按照表中的某一个字段进行统一归类,并存储在表中的不同的位置,也就是说,一个分区就是一类,这一类的数据对应到hdfs存储上就是对应一个目录。当我们需要进行处理的时候,可以通过分区进行过滤,从而只取部分数据,而没必要取全部数据进行过滤,从而提升数据的处理效率。且分区表是可以分层级创建。
select * from 表 where dt = '2019-03-13'

4、分区表的关键字是Partition,这里的分区是MR中的分区没有关系
5、分区表可以有内部分区表,也可以有外部分区表
6、什么时候表数据不用分区:  
   1)几乎在实际应用中所有的表数据都要分区
   2)如果你的数据量很小,而且数据很单一,此时可以不用分区

2.1静态分区

----------------------单级分区----------------------------------
-- 1、创建单分区表
create table score
(
    sid    string,
    cid    string,
    sscore int
)
partitioned by (dt string)  -- 这个dt是分区字段和表字段没有关系,理论上可以随便写
row format delimited fields terminated by '\t';

-- 2、给分区表加载数据
-- 第一件事:在HDFS的表目录下创建文件夹:dt=2022-10-13  第二件事:将score.txt复制到该文件夹下
load data local inpath '/export/data/hivedatas/score.txt' into table score partition (dt='2022-10-13');

select * from score;

-- 再添加一个分区
load data local inpath '/export/data/hivedatas/score2.txt' into table score partition (dt='2022-10-14');
select * from score;


-- 3、查询数据

-- 查找dt=2022-10-13分区数据
select * from score where dt='2022-10-13';

-- 查找dt=2022-10-14分区数据
select * from score where dt='2022-10-14';

desc score; -- 查看哪个是分区列


----------------------多级分区----------------------------------
-- 1、创建多级分区表
create table score2
(
    sid    string,
    cid    string,
    sscore int
)
partitioned by (year string, month string ,dt string)  -- 这个dt是分区字段和表字段没有关系,理论上可以随便写
row format delimited fields terminated by '\t';



-- 2、给分区表加载数据
-- 第一件事:在HDFS的表目录下创建三级文件夹:year=2022/month=10/dt=13 第二件事:将score.txt复制到该文件夹下
load data local inpath '/export/data/hivedatas/score.txt'
into table score2 partition (year='2022',month='10',dt='13');

select * from score2;

-- 再添加一个分区
load data local inpath '/export/data/hivedatas/score2.txt'
    into table score2 partition (year='2022',month='11',dt='13');
-- 再添加一个分区
load data local inpath '/export/data/hivedatas/score2.txt'
    into table score2 partition (year='2023',month='11',dt='13');

select * from score2;


-- 3、查询分区数据:查询 2022年 10月13号数据
select * from  score2 where year='2022' and month = '10' and dt = '13';


----------------------分区相关的SQL----------------------------------

show  partitions  score; -- 查看表所有分区情况
alter table score add partition(dt='2022-01-01');  -- 手动添加一个分区
alter table score drop partition(dt='2022-01-01'); -- 手动删除一个分区

2.2动态分区

单级分区


-- -----------------------单级分区:按照日进行分区---------------------------------
-- 1、开启动态分区
set hive.exec.dynamic.partition=true;  -- 开启动态分区
set hive.exec.dynamic.partition.mode=nonstrict;-- 设置为非严格格式


-- 2、模拟数据
/*
1	2022-01-01	zhangsan	80
2	2022-01-01	lisi	70
3	2022-01-01	wangwu	90
1	2022-01-02	zhangsan	90
2	2022-01-02	lisi	65
3	2022-01-02	wangwu	96
1	2022-01-03	zhangsan	91
2	2022-01-03	lisi	66
3	2022-01-03	wangwu	96
*/
-- 3、创建一个中间普通表(该表用来存入原始数据)
create table test1
(
    id       int,
    date_val string,
    name     string,
    score    int
)
row format delimited fields terminated by '\t';

-- 4、给普通表加载数据
load data local inpath '/export/data/hivedatas/partition.txt' into table test1;

-- 5、来创建最终的分区表
create table test2
(
    id    int,
    name  string,
    score int
)
partitioned by (dt string) -- 这个分区字段的名字随便写,它来决定HDFS上文件夹的名字:day=2022-01-01
row format delimited fields terminated by ',';

-- 6、查询普通表,将数据插入到分区表
insert overwrite table test2 partition (dt)
select id, name, score, date_val  from test1;

select * from test2;



-- -----------------------单级分区:按照月进行分区---------------------------------
1       2022-01-01      zhangsan        80
2       2022-01-01      lisi    70
3       2022-01-01      wangwu  90
1       2022-01-02      zhangsan        90
2       2022-01-02      lisi    65
3       2022-01-02      wangwu  96
1       2022-01-03      zhangsan        91
2       2022-01-03      lisi    66
3       2022-01-03      wangwu  96
1       2022-02-01      zhangsan        80
2       2022-02-01      lisi    70
3       2022-02-01      wangwu  90
1       2022-02-02      zhangsan        90
2       2022-02-02      lisi    65
3       2022-02-02      wangwu  96
1       2022-02-03      zhangsan        91
2       2022-02-03      lisi    66
3       2022-02-03      wangwu  96

load data local inpath '/export/data/hivedatas/partition2.txt' overwrite into table test1;

drop  table test2_1;
create table test2_1
(
    id    int,
    date_val string,
    name  string,
    score int
)
partitioned by (month string) -- 这个分区字段的名字随便写,它来决定HDFS上文件夹的名字:day=2022-01-01
row format delimited fields terminated by ',';


-- 6、查询普通表,将数据插入到分区表
insert overwrite table test2_1 partition (month)
select id, date_val,name, score, substring(date_val,1,7)   from test1;

动态分区

-- 1、创建普通表

drop table if exists test3;
create table test3
(
    id       int,
    date_val string,
    name     string,
    sex      string,
    score    int
)
    row format delimited fields terminated by '\t';
;


-- 2、给普通表加载数据
load data local inpath '/export/data/hivedatas/partition3.txt' overwrite into table test3;
select * from test3;

-- 3、创建最终的分区表
drop table test4;
create table test4
(
    id    int,
    name  string,
    score int
)
    partitioned by (xxx string, yyy string)
    row format delimited fields terminated by '\t'
;

-- 4、去普通表查询,将查询后的结果插入到最终的分区表

insert overwrite table test4
select id, name, score,date_val,sex from test3;  -- 这里的动态分区是看最后的两个字段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值