1.HIVE的数据类型
1.1基本数据类型

注意:Hive中没有long类型,是bigint。
1.2复杂数据类型
1.3 hive默认分隔符
2.HIVE对库的操作
2.1创建库
(1)简单方式:
create database t1;
show databases;

(2)创建库的时候带注释。
create database if not exists t2 comment 'learning hive';

(3)创建带属性的库。
create database if not exists t3 with dbproperties('creator'='hadoop','date'='2019-01-01');

2.2查看库
(1)最常用查看库方式。
show databases;
(2)显示数据库的详细属性信息。
desc database t3;
desc database extended t3;
(3)查看正在使用哪个库。
select current_database();
2.3删除库
默认情况下,hive 不允许删除包含表的数据库。需要使用cascade 关键字。
drop database if exists t3 cascade;

2.4切换库
切换到t2数据库。
use t2;
3.HIVE对表的操作
3.1内部表与外部表
(1)内部表
表目录hive会自动创建在默认的HDFS目录下
create table worker_1(id int,name string,salary bigint,addr string)
row format delimited
fields terminated by ‘,’;
查找命令:/user/hive/warehouse/t2.db/test_1
(2)外部表
创建的时候,需要使用external关键字,并指定表对应hdfs上的目录/aa/bb。
create external table test_2(id int,name string,salary bigint,addr string)
row format delimited
fields terminated by ‘,’
location ‘/test’;

drop一个内部表时,表的元信息和表数据目录都会被删除。
drop一个外部表时,只删除表的元信息,表的数据目录不会被删除。
外部表的意义
通常,一个数据仓库系统,数据总有一个源头,而源由一般是别的应用程序产生的,其目录无法确定,为了方便映射,就可以在hive中用外部表映射。并且,就算hive中把这个表删了,也不会删除数据目录,就不会影响到别的应用系统。
3.2数据的导入
(1)导入
将hive服务器运行所在节点的本地磁盘上的文件导入表中。
注意:
这里load的文件是在开启server的节点上。不是在客户端节点上。
我们编辑的数据如果有中文,必须得是UTF-8编码格式,否则数据会出现乱码现象。
load data local inpath '/opt/testdata/hive/test_1.txt’' into table test_1;
加overwrite可以实现覆盖,不加overwrite是追加到表后面。
load data local inpath ‘/opt/testdata/hive/test_1.txt’ overwrite into table test_1;
将hdfs上的文件导入表中。
上传文件到HDFS。

(2)从别的表查询数据后插入到一张新建的表中。表会自动生成。
create table test_3
as
select id,name,salary
from worker_2
where salary>=15000;

将数据从hive的表中导出到本地磁盘的目录中
insert overwrite local directory ‘‘/opt/testdata/hive/test_1.log’
select * from test_1;
3.3 hive的复杂数据类型
array、map、struct。
现有数据:
1huangboguangzhou,xianggang,shenzhena1:30,a2:20,a3:100beijing,112233,13522334455,500
2 xuzheng xianggang b2:50,b3:40 tianjin,223344,13644556677,600
3 wangbaoqiang beijing,zhejiang c1:200 chongqinjg,334455,15622334455,20
建表语句:
create table movie_info(
id int,
name string,
work_location array<string>,
piaofang map<string,bigint>,
address struct<location:string,zipcode:int,phone:string,value:int>)
row format delimited
fields terminated by " "
collection items terminated by ","
map keys terminated by ":" ;
导入数据:
load data local inpath "/opt/testdata/hive/movie_info.txt" into table movie_info;
查询语句:
array:select work_location[0] from movie_info;

map:select piaofang["a1"] from movie_info;

struct:select address.location from movie_info;
3.4 hive的文件存储格式
Hive支持多种文件格式:sequence file、text file、parquet file、rc file、orc file。
textfile为默认格式,存储方式为行存储。数据不做压缩,磁盘开销大,数据解析开销大。
SequenceFile是Hadoop API提供的一种二进制文件支持,其具有使用方便、可分割、可压缩的特点。 SequenceFile支持三种压缩选择:NONE, RECORD, BLOCK。 Record压缩率低,一般建议使用BLOCK压缩。
RC file一种行列存储相结合的存储方式。
ORCFile数据按照行分块,每个块按照列存储,其中每个块都存储有一个索引。每一块的默认大小为256MB。ORC是hive给出的新格式,属于RCFILE的升级版,性能有大幅度提升,而且数据可以压缩存储,压缩快 快速列存取。
Parquet也是一种列式存储,同时具有很好的压缩性能;同时可以减少大量的表扫描和反序列化的时间。Snappy压缩方式。
创建seq表,对应的文件类型是sequencefile。
create table worker_seq(id int,name string)

本文详细介绍了HIVE的各种数据类型,包括基本数据类型和复杂数据类型的使用。此外,还阐述了HIVE对库的操作,如创建、查看、删除和切换库,并详细讲解了对表的操作,如内部表、外部表、数据导入导出、复杂数据类型、文件存储格式等。文章还涉及到了HIVE的函数、视图、表关联操作、分桶表以及窗口函数的使用,深入探讨了HIVE在大数据处理中的各种功能和技巧。
最低0.47元/天 解锁文章
826

被折叠的 条评论
为什么被折叠?



