前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >拿美团offer,Hive基础篇(持续更新中)

拿美团offer,Hive基础篇(持续更新中)

作者头像
木野归郎
发布2020-06-15 14:30:09
4910
发布2020-06-15 14:30:09
举报
文章被收录于专栏:share ai happiness

01

Hive基本概念

什么是Hive?

HIve:由 Facebook 开源用于解决海量结构化日志的数据统计。

Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类 SQL查询功能。

本质是:将 HQL 转化成 MapReduce 程序

1)Hive 处理的数据存储在 HDFS

2)Hive 分析数据底层的实现是 MapReduce

3)执行程序运行在 Yarn 上

Hive的优缺点?

优点:

1)操作接口采用类 SQL 语法,提供快速开发的能力(简单、容易上手)

2)避免了去写 MapReduce,减少开发人员的学习成本。3)Hive 的执行延迟比较高,因此 Hive 常用于数据分析,对实时性要求不高的场合;

4)Hive 优势在于处理大数据,对于处理小数据没有优势,因为 Hive 的执行延迟比较高。

5)Hive 支持用户自定义函数,用户可以根据自己的需求来实现自己的函数。

缺点:

1)Hive 的 HQL 表达能力有限

(1)迭代式算法无法表达

(2)数据挖掘方面不擅长

2)Hive 的效率比较低

(1)Hive 自动生成的 MapReduce 作业,通常情况下不够智能化

(2)Hive 调优比较困难,粒度较粗

这里不详细讨论架构原理,后期文章会更新

数据存储位置?

Hive是建立在Hadoop之上的,所有Hive的数据都是存储在HDFS中的,而数据库则可以将数据保存在块设备或者本地文件系统中。

索引?

HIve在加载数据的过程中不会对数据进行任何处理,甚至不会对数据进行扫描,因此也没有对数据中的某些key建立索引。HIve要访问数据中满足条件的特定值时,需要暴力扫描整个数据,因此访问延迟较高。

Hive和数据库比较?

延迟性高,适合大数据量。

扩展性高。

02

HIve数据类型

基本数据类型

对于 Hive 的 String 类型相当于数据库的 varchar 类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储 2GB 的字符数。

集合数据类型

Hive 有三种复杂数据类型 ARRAY、MAP 和 STRUCT。ARRAY 和 MAP 与 Java 中的 Array 和Map 类似,而 STRUCT 与 C 语言中的 Struct 类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。

03

DDL数据定义

1.创建数据库

创建一个数据库,数据库在 HDFS 上的默认存储路径是/user/hive/warehouse/*.db。

创建一个数据库标准写法:

代码语言:javascript
复制
create database if not exists db_hive;

创建数据库指定在hdfs上存储的位置:

代码语言:javascript
复制
create database db_hive2 location '/db_hive2.db';

2.修改数据库

用户可以使用 ALTER DATABASE 命令为某个数据库的 DBPROPERTIES 设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。

代码语言:javascript
复制
alter database db_hive set dbproperties('createtime'='20170830');

在 mysql 中查看修改结果:

代码语言:javascript
复制
desc database extended db_hive;

db_name:db_hive

comment location:

hdfs://hadoop102:8020/user/hive/warehouse/db_hive.db

owner_name:hive

owner_type:USER

parameters:{createtime=20190830}

3.查询数据库

1)显示数据库

hive> show databases;

2)过滤显示查询的数据库

hive> show databases like 'db_hive*';

3)显示数据库信息

hive> desc database db_hive;

4)显示数据库详细信息,extended

hive> desc database extended db_hive;

5)切换当前数据库

hive (default)> use db_hive;

4.删除数据库

1)删除空数据库

hive>drop database db_hive2;

2)如果删除的数据库不存在,最好采用 if exists 判断数据库是否存在

3)如果数据库不为空,可以采用 cascade 命令,强制删除

hive> drop database db_hive cascade;

5.创建表

1)建表语法

2)字段解释说明

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

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

(3)COMMENT:为表和列添加注释。

(4)PARTITIONED BY 创建分区表

(5)CLUSTERED BY 创建分桶表

(6)SORTED BY 不常用

(7)ROW FORMAT

DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char]

[MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]

用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,将会使用自带的 SerDe。在建表的时候,用户还需要为表指定列,用户 在指定表的列的同时也会指定自定义的 SerDe,Hive 通过 SerDe 确定表的具体的列的数据。

(8)STORED AS 指定存储文件类型

常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE (列式存储格式文件)

如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。

(9)LOCATION :指定表在 HDFS 上的存储位置。

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

1)管理表

(1)理论 默认创建的表都是所谓的管理表,有时也被称为内部表。因为这种表,Hive 会(或多或少地)控 制 着 数 据 的 生 命 周 期 。Hive 默 认 情 况 下 会 将 这 些 表 的 数 据 存 储 在 由 配 置 项 hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定义的目录的子目录下。当我们删除一个 管理表时,Hive 也会删除这个表中数据。管理表不适合和其他工具共享数据。

(2)实例操作

创建普通表

代码语言:javascript
复制
create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t' stored as textfile
location '/user/hive/warehouse/student2';

根据查询结果创建表(查询的结果会添加到新创建的表中)

代码语言:javascript
复制
create table if not exists student3
as select id, name from student;
create table if not exists student4 like student;

查询表的类型

代码语言:javascript
复制
hive (default)> desc formatted student2;

Table Type: MANAGED_TABLE

6.分区表

分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

1)外部表

(1)理论

因为表是外部表,所以 Hive 并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。

(2)管理表和外部表的使用场景:

每天将收集到的网站日志定期流入 HDFS 文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过 SELECT+INSERT 进入内部表。

(3)实例

分别创建部门和员工外部表,并向表中导入数据。

创建部门表

代码语言:javascript
复制
create external table if not exists default.dept(
deptno int, dname string, loc int
)
row format delimited fields terminated by '\t';

创建员工表

代码语言:javascript
复制
create external table if not exists default.emp(
empno int, 
ename string, 
job string, 
mgr int, 
hiredate string, 
sal double, 
comm double, 
deptno int)
row format delimited fields terminated by '\t';

查看创建的表

代码语言:javascript
复制
hive (default)> show tables;

向外部表中导入数据

代码语言:javascript
复制
导入数据
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept;
hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table default.emp;
查询结果
hive (default)> select * from emp;
hive (default)> select * from dept;

查看表格式化数据

代码语言:javascript
复制
hive (default)> desc formatted dept;

Table Type: EXTERNAL_TABLE

2)分区表基本操作

引入分区表(需要根据日期对日志进行管理)

代码语言:javascript
复制
/user/hive/warehouse/log_partition/20170702/20170702.log
/user/hive/warehouse/log_partition/20170703/20170703.log
/user/hive/warehouse/log_partition/20170704/20170704.log

创建分区表语法

代码语言:javascript
复制
hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';

加载数据到分区表中

代码语言:javascript
复制
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition
partition(month='201709');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition
partition(month='201708');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition
partition(month='201707');

查询分区表中数据

代码语言:javascript
复制
单分区查询
hive (default)> 
select * from dept_partition where month='201709';
多分区联合查询
hive (default)> 
select * from dept_partition where month='201709' union
select * from dept_partition where month='201708' union
select * from dept_partition where month='201707';

增加分区

代码语言:javascript
复制
创建单个分区
hive (default)> 
alter table dept_partition add partition(month='201706') ;
同时创建多个分区
hive (default)> 
alter table dept_partition add partition(month='201705') partition(month='201704');

删除分区

代码语言:javascript
复制
删除单个分区
hive (default)> 
alter table dept_partition drop partition (month='201704');
同时删除多个分区
hive (default)> 
alter table dept_partition drop partition (month='201705'), partition (month='201706');

查看分区表有多少分区

代码语言:javascript
复制
hive> show partitions dept_partition;

查看分区表结构

代码语言:javascript
复制
hive> desc formatted dept_partition;

3)分区表注意事项

创建二级分区表

代码语言:javascript
复制
hive (default)> 
create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
代码语言:javascript
复制
加载数据到二级分区表中
hive (default)> 
load data local inpath '/opt/module/datas/dept.txt' into 
table default.dept_partition2 partition(month='201709', day='13');

查询分区数据
hive (default)> 
select * from dept_partition2 where month='201709' and day='13';

把数据直接上传到分区目录上,让分区表和数据产生关联的两种方式

代码语言:javascript
复制
方式一:上传数据后修复
上传数据

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;
dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12;

查询数据(查询不到刚上传的数据)
select * from dept_partition2 where month='201709' and day='12';

执行修复命令
hive>msck repair table dept_partition2;

再次查询数据
select * from dept_partition2 where month='201709' and day='12';
代码语言:javascript
复制
方式二:上传数据后添加分区
上传数据
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11;
dfs -put /opt/module/datas/dept.txt
/user/hive/warehouse/dept_partition2/month=201709/day=11;
执行添加分区
alter table dept_partition2 add partition(month='201709', day='11');
查询数据
select * from dept_partition2 where month='201709' and day='11';
代码语言:javascript
复制
方式三:上传数据后 load 数据到分区
创建目录
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10;
上传数据
load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2
partition(month='201709',day='10');
查询数据
select * from dept_partition2 where month='201709' and day='10';

7.修改表

1)重命名表

代码语言:javascript
复制
 alter table dept_partition2 rename to dept_partition3;

2)增加、修改和删除表分区

见上面分区表实例

3)增加/修改/替换列信息

语法:

代码语言:javascript
复制
查询表结构
hive>desc dept_partition;
添加列
alter table dept_partition add columns(deptdesc string);
查询表结构
hive>desc dept_partition;
更新列
alter table dept_partition change column deptdesc desc int;
查询表结构
hive>desc dept_partition;
替换列
alter table dept_partition replace columns(deptno string, dname string, loc string);
查询表结构
hive>desc dept_partition;

8.删除表

代码语言:javascript
复制
hive (default)> drop table dept_partition;

04

DML数据操作

1.数据导入

1)向表中装载数据(Load)

2)实例

代码语言:javascript
复制
创建一张表
create table student(id string, name string) 
row format delimited fields terminated by '\t';

加载本地文件到 hive
load data local inpath '/opt/module/datas/student.txt' into table default.student

加载 HDFS 文件到 hive 中
上传文件到 HDFS
dfs -put /opt/module/datas/student.txt /user/doit/hive

加载 HDFS 上数据
load data inpath '/user/doit/hive/student.txt' into table default.student;

加载数据覆盖表中已有的数据
上传文件到 HDFS
dfs -put /opt/module/datas/student.txt /user/doit/hive

加载数据覆盖表中已有的数据
load data inpath '/user/doit/hive/student.txt' overwrite into table default.student;

3)通过查询语句向表中插入数据(Insert)

代码语言:javascript
复制
创建一张分区表
create table student(id int, name string) partitioned by (month string) row format
delimited fields terminated by '\t'

基本插入数据
hive (default)> insert into table student partition(month='201709') values(1,'wangwu');

基本模式插入(根据单张表查询结果)
hive (default)> insert overwrite table student partition(month='201708')
select id, name from student where month='201709';

多插入模式(根据多张表查询结果)
hive (default)> from student
insert overwrite table student partition(month='201707')
select id, name where month='201709' insert overwrite table student partition(month='201706')
select id, name where month='201709';

4)查询语句中创建表并加载数据(As Select)

见管理表那块。

根据查询结果创建表(查询的结果会添加到新创建的表中)

代码语言:javascript
复制
create table if not exists student3
as select id, name from student;

5)创建表时通过 Location 指定加载数据路径

代码语言:javascript
复制
创建表,并指定在 hdfs 上的位置
create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t' location '/user/hive/warehouse/student5';

上传数据到 hdfs 上
dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;

查询数据
hive (default)> select * from student5;

6)Import 数据到指定 Hive 表中

注意:先用 export 导出后,再将数据导入。

代码语言:javascript
复制
import table student2 partition(month='201709') from
'/user/hive/warehouse/export/student';

2.数据导出

1)Insert 导出

代码语言:javascript
复制
将查询的结果导出到本地
insert overwrite local directory '/opt/module/datas/export/student' select * from student;
将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
将查询的结果导出到 HDFS 上(没有 local)
insert overwrite directory '/user/doit/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;

2)Hadoop 命令导出到本地

代码语言:javascript
复制
dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;

3)Hive Shell 命令导出

代码语言:javascript
复制
bin/hive -e 'select * from default.student;' >
/opt/module/datas/export/student4.txt;

4)Export 导出到 HDFS 上

代码语言:javascript
复制
export table default.student to '/user/hive/warehouse/export/student';

5)Sqoop 导出

3.清除表中数据(Truncate)

注意:Truncate 只能删除管理表,不能删除外部表中数据hive (default)> truncate table student;

由于篇幅过长,基础篇分为两部分。

整理不易,希望对你有多帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OnlyCoding 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档