代码改变世界

泰涨知识 | 10分钟快速入门Hive之基本操作篇 - 教程

2026-01-28 19:30  tlnshuju  阅读(0)  评论(0)    收藏  举报

一、DDL操作(数据定义语言)

DDL操作(信息定义语言)包括:Create、Alter、Show、Drop等。

1. create database- 创建新数据库

2. alter database - 修改数据库

3. drop database - 删除数据库

4. create table - 创建新表

5. alter table - 变更(改变)数据库表

6. drop table - 删除表

7. create index - 创建索引(搜索键)

8. drop index - 删除索引

9. show table - 查看表

二、DML操作(数据操作语言)

1. load data - 加载材料

①insert into - 插入内容

②insert overwrite - 覆盖数据(insert ... values从Hive 0.14开始可用)

2. update table - 更新表(update在Hive 0.14开始可用,并且只能在拥护ACID的表上执行)

3. delete from table where id = 1; - 删除表中ID等于1的数据(delete在Hive 0.14开始可用,并且只能在支持ACID的表上执行)

4. merge - 合并(MERGE在Hive 2.2开始可用,并且只能在支持ACID的表上执行)

注意:频繁的update和delete操作已经违背了Hive的初衷,不到万不得已的情况,最好运用增量添加的方式。

三、Hive数据仓库操作

1. 创建数据仓库:

create database test;

2. 查看数据仓库:

show databases;

3. 创建数据仓库:

create database if not exists test;

4. 查看数据仓库信息以及路径:

describe database test;

5. 删除名为f的数据仓库:

drop database if not exists test;

四、Hive数据表操作

1. 查看表:

show tables;

2. 创建一个名为cat的内部表,有两个字段为cat_id和cat_name,字符类型为string:

create table cat(cat_id string,cat_name string);

3.创建一个外部表,表名为cat2,有两个字段为cat_id和cat_name,字符类型为string。

create external table if not exists cat2(cat_id string,cat_name string);

4. 修改cat表的表结构。对cat表添加两个字段group_id和cat_code:

alter table cat add columns(group_id string,cat_code string);

5. 使用desc命令查看一下加完字段后的cat表结构:

desc cat;

6. 修改cat2表的表名。把cat2表重命名为cat3:

alter table cat2 rename to cat3;

7. 删除名为cat3的表并查看:

drop table cat3;

show tables;

8. 创建与已知表相同结构的表,创建一个与cat表结构相同的表,名为cat4,这里要用到like关键字:

create table cat4 like cat;

9. 创建完成并查看结果:

show tables;

五、Hive中数据的4种导入

1、从本地文件系统中导入数据到Hive表

①在Hive中创建一个cat_group表,具备group_id和group_name两个字段,字符类型为string,以“\t”为分隔符,并查看结果:

create table cat_group(group_id string,group_name string) row format delimited fields terminated by '\t' stored as textfile;

show tables;

②将Linux本地/home/hadoop/data/hive2目录下的cat_group文件导入到Hive中的cat_group表中:

load data local inpath '/data/hive2/cat_group' into table cat_group;

③通过select语句查看cat_group表中是否成功导入材料,由于数据量大,使用limit关键字限制输出10条记录:

select * from cat_group limit 10;

2、将HDFS上的数据导入到Hive中

①另外开启一个运行窗口,在HDFS上创建/hive2目录:

hdfs dfs -mkdir /hive2

②将本地/data/hive2/下的cat_group表上传到HDFS的/hive2上,并查看是否创建成功:

hdfs dfs -put /home/hadoop/cat_group /hive2

hdfs dfs -ls /hive2

③在Hive中创建名为cat_group1的表,创表语句如下:

create table cat_group1(group_id string,group_name string) row format delimited fields terminated by '\t' stored as textfile;

④将HDFS下/myhive2中的表cat_group导入到Hive中的cat_group1表中 ,并查看结果:

load data inpath '/hive2/cat_group' into table cat_group1;

select * from cat_group1 limit 10;

提示:HDFS中数据导入到Hive中与本地素材导入到hive中的区别是load data后少了local。

3、从别的表中查询出相应的数据并导入到Hive中

①在Hive中创建一个名为cat_group2的表:

create table cat_group2(group_id string,group_name string) row format delimited fields terminated by '\t' stored as textfile;

②用下面两种方式将cat_group1表中的数据导入到cat_group2表中:

insert into table cat_group2 select * from cat_group1;

insert overwrite table cat_group2 select * from cat_group1;

(insert overwrite 会覆盖数据)

③导入搞定后,用select语句查询cat_group2表:

select * from cat_group2 limit 10;

4、在创建表的时候从别的表中查询出相应材料

并插入到所创建的表中

①Hive中创建表cat_group3并直接从cat_group2中获得数据。

create table cat_group3 as select * from cat_group2;

②创建并导入达成,用select语句查询实验结果:

select * from cat_group3 limit 10;

六、三种常见的数据导出方式

1、导出到本地文件系统

①在Linux本地新建/data/hive2/out目录。

mkdir -p /data/hive2/out

②将Hive中的cat_group表导出到本地文件系统/data/hive2/out中。

(注意:途径和导入数据到Hive不一样,不能用insert into来将数据导出。)

insert overwrite local directory '/data/hive2/out' select * from cat_group;

③导出完成后,在Linux本地切换到/data/hive2/out目录,通过cat命令查询导出文件的内容:

cd /home/hadoop/data/hive2/out

ls

cat 000000_0

通过④通过上图能够看到导出的资料,字段之间没有分割开,因而我们使用下面的方式,将输出字段以“\t”键分割:

insert overwrite local directory '/data/hive2/out' select group_id,concat('\t',group_name) from cat_group;

借助cat命令查询/data/hive2/out目录下的导出文件:

cd /home/hadoop/data/hive2/out/

cat 000000_0

2、Hive中内容导出到HDFS中

①在HDFS上创建/myhive2/out目录:

hdfs dfs -mkdir -p /hive2/out

②将Hive中的表cat_group中的数据导入到HDFS的/myhive2/out目录里:

insert overwrite directory '/myhive2/out' select group_id,concat('\t',group_name) from cat_group;

③导入完成后,在HDFS上的/myhive2/out目录下查看结果:

hdfs dfs -ls /myhive2/out

3、导出到Hive的另一个表中

①将Hive中表cat_group中的资料导入到cat_group4中(两表字段及字符类型相同)。

②在Hive中创建一个表cat_group4,有group_id和group_name两个字段,字符类型为string,以‘\t’为分隔符:

create table cat_group4(group_id string,group_name string) row format delimited fields terminated by '\t' stored as textfile;

③将cat_group中的内容导入到cat_group4中:

insert into table cat_group4 select * from cat_group;

④导入做完后,查看cat_group4表中数据:

select * from cat_group4 limit 10;

七、Hive分区表的操作

说,在表的数据文件中实际并不保存分区列的信息与数据。就是Hive中创建分区表没有什么复杂的分区类型(范围分区、列表分区、hash 分区,混合分区等)。分区列也不是表中的一个实际的字段,而是一个或者多个伪列。意思

1. 创建表分区,在Hive中创建一个分区表goods,包含goods_id和goods_status两个字段,字符类型为string,分区为cat_id,字符类型为string,以“\t“为分隔符。

create table goods(goods_id string,goods_status string) partitioned by (cat_id string)

row format delimited fields terminated by '\t';

2. 查看表goods表结构:

desc goods;

3. 向分区表插入素材,将本地/data/hive2下的表goods中数据,插入到分区表goods中。

①在Hive中创建一个非分区表goods_1表,用于存储本地/data/hive2下的表goods中数据。

create table goods_1(goods_id string,goods_status string,cat_id string)

row format delimited fields terminated by '\t';

②将本地/data/hive2下的表goods中数据导入到Hive中的goods_1表中:

load data local inpath '/data/hive2/goods' into table goods_1;

③将表goods_1中的素材导入到分区表goods中:

insert into table goods partition(cat_id='52052') select goods_id,goods_status from goods_1 where cat_id='52052';

④插入内容完成后,用select语句查看实验结果:

select * from goods limit 10;

⑤查看表goods中的分区:

show partitions goods;

⑥修改表分区,将分区表goods中的分区列cat_id=52052改为cat_id=52051,并查看修改后的分区名:

alter table goods partition(cat_id=52052) rename to partition(cat_id=52051);

show partitions goods;

4. 删除表分区:

①在删除goods分区表之前,先将goods表备份出一个goods_2表:

create table goods_2(goods_id string,goods_status string) partitioned by (cat_id string)

row format delimited fields terminated by '\t';

insert into table goods_2 partition(cat_id='52052') select goods_id,goods_status from goods_1 where cat_id='52052';

②删除goods表中的cat_id分区:

alter table goods drop if exists partition (cat_id='52051');

八、Hive桶的操作

1桶的定义及用途

①桶的定义

Hive是一个基于Hadoop的数据仓库工具,它允许通过SQL-like查询语言(HQL)来处理存储在Hadoop文件系统(HDFS)中的大数据集。Hive中的“桶”(Bucketing)是一种数据分区和排序的方法,用于优化查询性能和提高并行处理能力。

通过当我们在Hive表上创建桶时,会指定一个字段或一组字段作为桶键。Hive会根据这些键的哈希值将材料分成固定数量的桶。每个桶都会被写入到单独的文件中,这样能够使得相同键值的材料落在同一个桶内。当执行JOIN操控时,假设JOIN条件涉及到桶键,Hive可能利用该特性来减少JOIN的计算量,因为只必须比较相同桶号的资料,从而提高效率。

桶的采用还有助于并行化管理,由于每个桶可以独立处理,尤其是在大规模内容集上,这可以显著加快查询速度。然而,应该注意的是,虽然桶化可以提高某些特定查询的性能,但它也会增加信息写入的复杂性和存储空间的需求。因此,合理选择桶的数量和桶键对于优化性能至关重要。

②桶的用途

优化JOIN操作:通过当两个表在JOIN执行中使用相同的桶键时,Hive能够并行处理每个桶之间的JOIN,而不是处理整个表。这显著减少了JOIN操作的时间,尤其是当JOIN涉及大数据集时。

数据采样:桶化可以用于创建表的代表性样本。通过选择特定数量的桶,你可以轻松地获取表的一小部分数据,这对于测试查询性能、进行数据分析或生成报表非常有用。

提高查询效率:通过桶化,Hive可能更有效地应用索引(若是已创建)和执行范围查询,因为数据已经按照桶键进行了预排序。

并行处理:每个桶都许可独立处理,这意味着在分布式环境中,不同的桶允许由集群的不同节点同时处理,从而提高处理速度。

数据分布均匀:桶化有助于确保素材在HDFS上的分布更加均匀,避免某些节点过载,提高整体系统的性能和稳定性。

简化数据处理:桶化还可以简化某些麻烦的统计运行,如计算分位数、百分比等,因为每个桶内的数据已经是有序的。

提示:尽管桶化有诸多好处,但使用时必须谨慎,因为它可能会增加存储开销,并且不适用于所有类型的查询。在决定是否使用桶以及如何设计桶策略时,应根据具体的应用场景和查询模式进行权衡。

③Hive还可以把表或分区,组织成桶。将表或分区组织成桶有以下几个目的:

为使取样更高效。在处理大规模的内容集时,在开发、测试阶段将所有的数据全部处理一遍可能不太现实,若是能在素材集的一小部分数据上试运行查询,会带来很多方便。

为了获得更好的查询处理效率。桶为表给出了额外的结构,Hive在处理某些查询时利用这个结构,能够有效地提高查询效率。

具体而言,连接两个在(包含连接列的)相同列上划分了桶的表,可以使用Map端连接(Map-side join)高效的搭建。比如JOIN操作。对于JOIN操作两个表有一个相同的列,如果对这两个表都进行了桶操作。那么将保存相同列值的桶进行JOIN操作就可以,可以大大较少JOIN的数据量。在建立桶之前,需要设置hive.enforce.bucketing属性为true,使得Hive能识别桶。

2、创建桶

①创建一个名为goods_t的表,涵盖两个字段goods_id和goods_status,字符类型都为string,按cat_id string做分区,按goods_status列聚类和goods_id列排序,划分成两个桶:

create table goods_t(goods_id string,goods_status string) partitioned by (cat_id string)

clustered by(goods_status) sorted by (goods_id) into 2 buckets;

②设置环境变量set hive.enforce.bucketing=ture

set hive.enforce.bucketing=true;

③向goods_t表中插入goods_2表中的数据:

from goods_2 insert overwrite table goods_t partition(cat_id='52063') select goods_id,goods_status;

查看结果:

select * from goods_t tablesample(bucket 1 out of 2 on goods_id);

tablesample 是抽样语句,语法如下:

tablesample(bucket x out of y)

y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了64份,当y=32时,抽取(64/32=)2个bucket的数据,当y=128时,抽取(64/128=)1/2个bucket的数据。

x表示从哪个bucket开始抽取。例如,table总bucket数为32,tablesample(bucket 3 out of 16),表示总共抽取(32/16=)2个bucket的数据,分别为第3个bucket和第(3+16=)19个bucket的数据。

END