2021年大数据Hive:手把手教你如何吃透Hive数据库和表操作(学会秒变数仓大佬)

Posted Lansonli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021年大数据Hive:手把手教你如何吃透Hive数据库和表操作(学会秒变数仓大佬)相关的知识,希望对你有一定的参考价值。

全网最详细的Hive文章系列,强烈建议收藏加关注!

后面更新文章都会列出历史文章目录,帮助大家回顾知识重点。

目录

系列历史文章

前言

Hive数据库和表操作

一、数据库操作

1、创建数据库

2、创建数据库并指定hdfs存储位置

3、查看数据库详细信息

4、删除数据库

二、数据库表操作

1、创建数据库表语法

2、Hive建表时候的字段类型

3、内部表操作

4、外部表操作

5、复杂类型操作

6、分区表

7、分桶表

8、修改表

9、hive表中加载数据

10、hive表中的数据导出


系列历史文章

2021年大数据Hive(二):Hive的三种安装模式和MySQL搭配使用

2021年大数据Hive(一):​​​​​​​Hive基本概念

 

前言

 2021年全网最详细的大数据笔记,轻松带你从入门到精通,该栏目每天更新,汇总知识分享

 

Hive数据库和表操作

 

一、数据库操作

 

1、创建数据库

create database if not exists myhive;

use  myhive;

说明:hive的表存放位置模式是由hive-site.xml当中的一个属性指定的

<name>hive.metastore.warehouse.dir</name>

<value>/user/hive/warehouse</value>

 

2、创建数据库并指定hdfs存储位置

create database myhive2 location '/myhive2';

 

3、查看数据库详细信息

查看数据库基本信息

desc  database  myhive;

 

 

4、删除数据库

删除一个空数据库,如果数据库下面有数据表,那么就会报错

drop  database  myhive;

强制删除数据库,包含数据库下面的表一起删除

drop  database  myhive2  cascade; 

 

二、数据库表操作

 

1、创建数据库表语法

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name

   [(col_name data_type [COMMENT col_comment], ...)] 

   [COMMENT table_comment] 

   [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 

   [CLUSTERED BY (col_name, col_name, ...) 

   [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] 

   [ROW FORMAT row_format] 

   [STORED AS file_format] 

   [LOCATION hdfs_path]

说明:

1、CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。

2、EXTERNAL 关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),Hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。

3、LIKE 允许用户复制现有的表结构,但是不复制数据。

4、ROW FORMAT DELIMITED 可用来指定行分隔符

5、STORED AS  SEQUENCEFILE|TEXTFILE|RCFILE 来指定该表数据的存储格式,hive中,表的默认存储格式为TextFile。

6、CLUSTERED BY  对于每一个表(table)进行分桶(MapReuce中的分区),桶是更为细粒度的数据范围划分。Hive也是 针对某一列进行桶的组织。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中。

7、LOCATION  指定表在HDFS上的存储位置。

 

2、Hive建表时候的字段类型

分类

类型

描述

字面量示例

原始类型

BOOLEAN

true/false

TRUE

 

TINYINT

1字节的有符号整数 -128~127

1Y

 

SMALLINT

2个字节的有符号整数,-32768~32767

1S

 

INT

4个字节的带符号整数(-2147483648~2147483647)

1

 

BIGINT

8字节带符号整数

1L

 

FLOAT

4字节单精度浮点数1.0

 

 

DOUBLE

8字节双精度浮点数

1.0

 

DEICIMAL

任意精度的带符号小数

1.0

 

STRING

字符串,变长

“a”,’b’

 

VARCHAR

变长字符串

“a”,’b’

 

CHAR

固定长度字符串

“a”,’b’

 

BINARY

字节数组

无法表示

 

TIMESTAMP

时间戳,毫秒值精度

122327493795

 

DATE

日期

‘2016-03-29’

 

Time

 时分秒

‘12:35:46’

 

DateTime

年月日 时分秒

 

复杂类型

ARRAY

有序的的同类型的集合

   ["beijing","shanghai","tianjin","hangzhou"]

MAP

key-value,key必须为原始类型,value可以任意类型

{"数学":80,"语文":89,"英语":95}

STRUCT

字段集合,类型可以不同

struct(‘1’,1,1.0)

 

3、内部表操作

未被external修饰的是内部表(managed table),内部表又称管理表,内部表数据存储的位置由hive.metastore.warehouse.dir参数决定(默认:/user/hive/warehouse),删除内部表会直接删除元数据(metadata)及存储数据,因此内部表不适合和其他工具共享数据。

 

1、hive建表初体验

create database myhive;

use myhive;

create table stu(id int,name string);

insert into stu values (1,"zhangsan");

select * from stu;

 

​​​​​​​2、创建表并指定字段之间的分隔符

create  table if not exists stu3(id int ,name string) row format delimited fields terminated by '\\t';

 

​​​​​​​3、根据查询结果创建表

create table stu3 as select * from stu2;

 

​​​​​​​4、根据已经存在的表结构创建表

create table stu4 like stu2;

 

​​​​​​​5、查询表的类型

desc formatted  stu2;

 

​​​​​​​​​​​​​​6、删除表

drop table stu2;

查看数据库和HDFS,发现删除内部表之后,所有的内容全部删除

 

4、外部表操作

在创建表的时候可以指定external关键字创建外部表,外部表对应的文件存储在location指定的hdfs目录下,向该目录添加新文件的同时,该表也会读取到该文件(当然文件格式必须跟表定义的一致)。

外部表因为是指定其他的hdfs路径的数据加载到表当中来,所以hive表会认为自己不完全独占这份数据,所以删除hive外部表的时候,数据仍然存放在hdfs当中,不会删掉。

 

1、数据装载载命令Load

Load命令用于将外部数据加载到Hive表中

语法:

load data [local] inpath '/export/data/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、操作案例

分别创建老师与学生表外部表,并向表中加载数据

源数据如下:

student.txt

01 赵雷 1990-01-01 男

02 钱电 1990-12-21 男

03 孙风 1990-05-20 男

04 李云 1990-08-06 男

05 周梅 1991-12-01 女

06 吴兰 1992-03-01 女

07 郑竹 1989-07-01 女

08 王菊 1990-01-20 女

teacher.txt​​​​​​​

01 张三

02 李四

03 王五

  • 创建老师表:
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/student.txt' overwrite  into table student;
  • 从hdfs文件系统向表中加载数据

其实就是一个移动文件的操作

需要提前将数据上传到hdfs文件系统,

hadoop fs -mkdir -p /hivedatas

cd /export/data/hivedatas

hadoop fs -put teacher.csv /hivedatas/

load data inpath '/hivedatas/teacher.csv' into table teacher;

注意,如果删掉teacher表,hdfs的数据仍然存在,并且重新创建表之后,表中就直接存在数据了,因为我们的student表使用的是外部表,drop table之后,表当中的数据依然保留在hdfs上面了

 

5、复杂类型操作

 

1、Array类型

Array是数组类型,Array中存放相同类型的数据

源数据:  

说明:name与locations之间制表符分隔,locations中元素之间逗号分隔

zhangsan   beijing,shanghai,tianjin,hangzhou

wangwu    changchun,chengdu,wuhan,beijin

建表语句

create external table hive_array(name string, work_locations array<string>)

row format delimited fields terminated by '\\t'

collection items terminated by  ',';

导入数据(从本地导入,同样支持从HDFS导入)

load data local inpath '/export/data/hivedatas/work_locations.txt' overwrite into table hive_array;

常用查询:

-- 查询所有数据

select * from hive_array;

-- 查询loction数组中第一个元素

select name, work_locations[0] location from hive_array;

-- 查询location数组中元素的个数

select name, size(work_locations) location from hive_array;

-- 查询location数组中包含tianjin的信息

select * from hive_array where array_contains(work_locations,'tianjin'); 

 

​​​​​​​6、分区表

分区不是独立的表模型,要和内部表或者外部表结合:

  内部分区表

  外部分区表

 

​​​​​​​​​​​​​​1、基本操作

在大数据中,最常用的一种思想就是分治,分区表实际就是对应hdfs文件系统上的的独立的文件夹,该文件夹下是该分区所有数据文件。

分区可以理解为分类,通过分类把不同类型的数据放到不同的目录下。

分类的标准就是分区字段,可以一个,也可以多个。

分区表的意义在于优化查询。查询时尽量利用分区字段。如果不使用分区字段,就会全部扫描。

在查询是通过where子句查询来指定所需的分区。

在hive中,分区就是分文件夹

创建分区表语法

create table score(sid string,cid string, sscore int) partitioned by (month string) row format delimited fields terminated by '\\t';

创建一个表带多个分区

create table score2 (sid string,cid string, sscore int) partitioned by (year string,month string,day string) row format delimited fields terminated by '\\t';

加载数据到分区表中

load data local inpath '/export/data/hivedatas/score.csv' into table score partition (month='202006');

加载数据到一个多分区的表中去

load data local inpath '/export/data/hivedatas/score.csv' into table score2 partition(year='2020',month='06',day='01');

多分区联合查询使用union  all来实现

select * from score where month = '202006' union all select * from score where month = '202007';

查看分区

show  partitions  score;

添加一个分区

alter table score add partition(month='202008');

同时添加多个分区

alter table score add partition(month='202009') partition(month = '202010');

注意:添加分区之后就可以在hdfs文件系统当中看到表下面多了一个文件夹

删除分区

alter table score drop partition(month = '202010');

 

​​​​​​​7、分桶表

分桶就是将数据划分到不同的文件,其实就是MapReduce的分区

 

​​​​​​​​​​​​​​1、基本操作

将数据按照指定的字段进行分成多个桶中去,说白了就是将数据按照字段进行划分,可以将数据按照字段划分到多个文件当中去

开启hive的桶表功能(如果执行该命令报错,表示这个版本的Hive已经自动开启了分桶功能,则直接进行下一步)

set hive.enforce.bucketing=true;

设置reduce的个数

set mapreduce.job.reduces=3; 

创建分桶表

create table course (cid string,c_name string,tid string) clustered by(cid) into 3 buckets row format delimited fields terminated by '\\t';

桶表的数据加载,由于桶表的数据加载通过hdfs  dfs  -put文件或者通过load  data均不好使,只能通过insert  overwrite

创建普通表,并通过insert  overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去

创建普通表:

create table course_common (cid string,c_name string,tid string) row format delimited fields terminated by '\\t';

普通表中加载数据

load data local inpath '/export/data/hivedatas/course.csv' into table course_common;

通过insert  overwrite给桶表中加载数据

insert overwrite table course select * from course_common cluster by(cid);

 

8、修改表

 

1、表重命名

基本语法:

alter  table  old_table_name  rename  to  new_table_name;
-- 把表score3修改成score4

alter table score3 rename to score4;

 

​​​​​​​2、增加/修改列信息

-- 1:查询表结构

desc score4;

-- 2:添加列

alter table score4 add columns (mycol string, mysco string);

-- 3:查询表结构

desc score4;

-- 4:更新列

alter table score4 change column mysco mysconew int;

-- 5:查询表结构

desc score4;

 

​​​​​​​​​​​​​​3、删除表

drop table score4;

 

​​​​​​​4、清空表数据

只能清空管理表,也就是内部表

truncate table score4;

 

9、hive表中加载数据

 

1、直接向分区表中插入数据

通过insert into方式加载数据

create table score3 like score;

insert into table score3 partition(month ='202007') values ('001','002',100);

通过查询方式加载数据

create table score4 like score;

insert overwrite table score4 partition(month = '202006') select sid,cid,sscore from score;

 

​​​​​​​2、通过查询插入数据

通过load方式加载数据

create table score5 like score;

load data local inpath '/export/data/hivedatas/score.csv' overwrite into table score5 partition(month='202006');
  • ​​​​​​​多插入模式

常用于实际生产环境当中,将一张表拆开成两部分或者多部分

给score表加载数据

load data local inpath '/export/data/hivedatas/score.csv' overwrite into table score partition(month='202006');

创建第一部分表:

create table score_first( sid string,cid  string) partitioned by (month string) row format delimited fields terminated by '\\t' ;

创建第二部分表:

create table score_second(cid string,sscore int) partitioned by (month string) row format delimited fields terminated by '\\t';

分别给第一部分与第二部分表加载数据

from score insert overwrite table score_first partition(month='202006') select sid,cid insert overwrite table score_second partition(month = '202006')  select cid,sscore;
  • ​​​​​​​查询语句中创建表并加载数据(as select

将查询的结果保存到一张表当中去

create table score5 as select * from score;
  • ​​​​​​​创建表时通过location指定加载数据路径

1、创建表,并指定在hdfs上的位置

create external table score6 (sid string,cid string,sscore int) row format delimited fields terminated by '\\t' location '/myscore6';

2、上传数据到hdfs上

hadoop fs -mkdir -p /myscore6

hadoop fs -put score.csv/myscore6;

3、查询数据

select * from score6;

 

​​​​​​​10、hive表中的数据导出

将hive表中的数据导出到其他任意目录,例如linux本地磁盘,例如hdfs,例如mysql等等

​​​​​​​​​​​​​​1、insert导出

1)将查询的结果导出到本地

insert overwrite local directory '/export/data/exporthive' select * from score;

2)将查询的结果格式化导出到本地

insert overwrite local directory '/export/data/exporthive' row format delimited fields terminated by '\\t' collection items terminated by '#' select * from student;

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

insert overwrite directory '/exporthive' row format delimited fields terminated by '\\t'  select * from score;

 

​​​​​​​​​​​​​​2、hive shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

bin/hive -e "select * from myhive.score;" > /export/data/exporthive/score.txt

 

​​​​​​​​​​​​​​3、export导出到HDFS上

export table score to '/export/exporthive/score';

 

​​​​​​​4、sqoop导出

由于篇幅有限,在项目实战的系列文章详细介绍


本博客大数据系列文章会一直每天更新,记得收藏加关注喔~ 

以上是关于2021年大数据Hive:手把手教你如何吃透Hive数据库和表操作(学会秒变数仓大佬)的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你快速吃透Flask web开发!仅分享3天!

2021年大数据Hive:Hive的表生成函数

2021年大数据Hive:Hive调优

2021年大数据Hive:​​​​​​​Hive基本概念

2021年大数据Hive:Hive综合案例!!!

2021年大数据Hive:Hive的数据压缩