当前位置: 首页 > news >正文

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;  -- 这里的动态分区是看最后的两个字段

相关文章:

  • 30 个 Python 技巧,加速你的数据分析处理速度
  • 学习unix网络编程第二章
  • 实验三.局域网的组建
  • 微服务14 Docker镜像仓库
  • Lambda详解 => {C#莱姆达表达式}
  • 6207. 统计定界子数组的数目(每日一难phase3-2)
  • java毕业设计家居体验平台的设计与实现Mybatis+系统+数据库+调试部署
  • SpringBoot测试配置属性与启动web环境
  • 11. SpringCloud Alibaba Seata
  • C++模板之——类模板详解及代码示例
  • Python推荐系统和深度学习教程
  • 基于Matlab使用雷达资源管理有效跟踪多个机动目标仿真(附源码)
  • 医院管理系统/医院药品管理系统
  • 项目中使用到的Spring注解及其作用
  • Postgresql源码(86)varchar的创建与插入分析
  • android 一些 utils
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • Less 日常用法
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • Markdown 语法简单说明
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Spring框架之我见(三)——IOC、AOP
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • Vue学习第二天
  • XML已死 ?
  • 聊聊directory traversal attack
  • 日剧·日综资源集合(建议收藏)
  • 小李飞刀:SQL题目刷起来!
  • 学习ES6 变量的解构赋值
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #include
  • #laravel 通过手动安装依赖PHPExcel#
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (笔试题)合法字符串
  • (二)PySpark3:SparkSQL编程
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (三)uboot源码分析
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .Net Redis的秒杀Dome和异步执行
  • .NET Standard 的管理策略
  • .net wcf memory gates checking failed
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .NET基础篇——反射的奥妙
  • .net快速开发框架源码分享
  • .NET上SQLite的连接
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [145] 二叉树的后序遍历 js