1.什么是hive?
hive是基于hadoop的一个数据仓库工具,可以结构化的数据文件映射为一张数据库表,并提供类sql的查询功能。往往存储历史数据进行分析使用。
本质就是将sql转换为mapreduce的任务进行计算,底层有hdfs来提供数据的存储,就是一个将sql转换为mapreduce的任务的工具。
2.为啥使用hive?
学习成本低, *** 作简单,功能扩展方便
3.hive的特点
可扩展
Hive可以自由的扩展集群的规模,一般情况下不需要重启服务。
延展性
Hive支持用户自定义函数,用户可以根据自己的需求来实现自己的函数。
容错
良好的容错性,节点出现问题SQL仍可完成执行。
4.hive的架构图
5.hive与hadoop的关系:紧耦合
sql语句由hive转换为mapreduce ,hive的数据分析计算引擎默认是使用mapreduce,然后计算,最终保存到hdfs中。
6.hive的安装部署
使用mysql管理安装hive
强调:
元数据:构建表结构的数据,由mysql进行管理
bin/hive 客户端直接访问hive
7.hive ql
7.1
创建数据库
create database if not exists myhive;
use myhive;
修改数据库
alter database myhive2 set dbproperties('createtime'='20210611');
查看数据库信息
desc database myhive2;
desc database extended myhive2; 更详细
删除数据库
drop database myhive2;
drop database myhive cascade;如果库中有内容,就强制删除
7.2管理表(内部表)
管理表:由hive全权管理的表,在删除表的时候,会将表的元数据和全部数据删除
create table if not exists stu2(id int ,name string) row format delimited fields terminated by 't' stored as textfile location '/user/stu2';
加载数据
不推荐使用,每次插入都会触发一次mr,效率极低
insert into stu values (1,"zhangsan");
建议使用load
load data (local) inpath '/xxx' into table 表名;
7.2外部表
外部表:hive认为当前的表不完全由自己管理,所以在删除的时候,只会删除元数据,保留数据
create external table teacher (t_id string,t_name string) row format delimited fields terminated by 't';
7.3分区表
分区表:按照数据所在的文件目录进行分区
create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields terminated by 't';
加载数据到分区表
load data local inpath '/opt/servers/hivedatas/score.csv' into table score partition (month='201806');
查看分区
show partitions score;
添加分区
alter table score add partition(month='201805');
删除分区
alter table score drop partition(month='201805');
7.5分桶表
分桶表:分的是文件,按照文件区分数据
前提设置
开启分桶属性
set hive.enforce.bucketing=true;
设置reduce个数
mapreduce.job.reduces=3;
创建一张分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by 't';
分桶表不能将数据直接加载,需要间接的表insert overwrite的方式加载数据
1.创建普通表
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by 't';
2.普通表中加载数据
load data local inpath '/opt/servers/hivedatas/course.csv' into table course_common;
3.通过insert overwrite加载数据
insert overwrite table course select * from course_common cluster by(c_id);
8.hive的查询
类似于mysql
9.hive的FAQ
所有的离线数据处理场景都适用hive吗?
并不是所有场景都适合,逻辑简单又要求快速出结果的场景Hive优势更大。但是在业务逻辑非常复杂的情况下还是需要开发MapReduce程序更加直接有效。
Hive能作为业务系统的数据库使用吗?
不能。传统数据库要求能够为系统提供实时的增删改查,而Hive不支持行级别的增删改,查询的速度也不比传统关系型数据库,而是胜在吞吐量高,所以不能作为关系型数据库来使用。
Hive与传统MR方式处理数据相比能够提高运行效率吗?
Hive的使用中需要将HQL编译为MR来运行,所以在执行效率上要低于直接运行MR程序。但是对于我们来说,由于只需要编写调试HQL,而不用开发调试复杂的MR程序,所以工作效率能够大大提高。
Hive为什么不支持行级别的增删改?
Hive不支持行级别的增删改的根本原因在于他的底层HDFS本身不支持。在HDFS中如果对整个文件的某一段或某一行内容进行增删改,势必会影响整个文件在集群中的存放布局。需要对整个集群中的数据进行汇总,重新切块,重新发送数据到每个节点,并备份,这样的情况是得不偿失的。所以HDFS的设计模式使他天生不适合做这个事
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)