Hive的核心技术及案例总结

版权声明:本博客都是作者10多年工作总结 https://blog.csdn.net/Peter_Changyb/article/details/81977665

1. Hive是什么?

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

 

2. Hive的设计目标?

1、Hive的设计目标是使Hadoop上的数据操作与传统SQL相结合,让熟悉SQL编程开发人员能够轻松向Hadoop平台迁移

2、Hive提供类似SQL的查询语言HQL,HQL在底层被转换为相应的MapReduce操作

3、Hive在HDFS上构建数据仓库来存储结构化的数据,这些数据一般来源与HDFS上的原始数据,使用Hive可以对这些数据执行查询、分析等操作。

 

3. Hive的数据模型

  • Hive数据库
  • 内部表
  • 外部表
  • 分区
  • Hive的视图

Hive 在创建内部表时,会将数据移动到数据仓库指向的路径,若创建外部表,仅记录数据所在的路径,不对数据位置做任何改变,在删除表的时候,内部表的元数据和数据会被一起删除,外部表只会删除元数据,不删除数据。这样来说,外部表要比内部表安全,数据组织液更加灵活,方便共享源数据。

 

4. Hive的调用方式

1、Hive Shell

2、Thrift

3、JDBC

4、ODBC

 

5. Hive的运行机制

1、将sql转换成抽象语法树

2、将抽象语法树转化成查询块

3、将查询块转换成逻辑查询计划(操作符树)

4、将逻辑计划转换成物理计划(M\Rjobs)

 

6. Hive的优势

1、并行计算

2、充分利用集群的CPU计算资源、存储资源

3、处理大规模数据集

4、使用SQL,学习成本低

 

7. Hive应用场景

1、海量数据处理

2、数据挖掘

3、数据分析

4、SQL是商务智能工具的通用语言,Hive有条件和这些BI产品进行集成

 

8. Hive不适用场景

1、复杂的科学计算

2、不能做到交互式的实时查询

 

9. Hive和数据库(RDBMS)的区别

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

 

2、数据格式。Hive中没有定义专门的数据格式,由用户指定,需要指定三个属性:列分隔符,行分隔符,以及读取文件数据的方法。数据库中,存储引擎定义了自己的数据格式。所有数据都会按照一定的组织存储。

 

3、数据更新。Hive的内容是读多写少的,因此,不支持对数据的改写和删除,数据都在加载的时候中确定好的。数据库中的数据通常是需要经常进行修改。

 

4、执行延迟。Hive在查询数据的时候,需要扫描整个表(或分区),因此延迟较高,只有在处理大数据是才有优势。数据库在处理小数据是执行延迟较低。

 

5、索引。Hive没有,数据库有

 

6、执行。Hive是MapReduce,数据库是Executor

 

7、可扩展性。Hive高,数据库低

 

8、数据规模。Hive大,数据库小

 

hive 代码简单例子:

创建一个名为 ”test“的table

create table students (name string,age int,city string,class string) row format delimited fields terminated by ',';

load data local inpath "/opt/students.txt" into table students;

create EXTERNAL table IF NOT EXISTS studentX (name string,age int,city string,class string) partitioned by (grade string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

alter table studentX add partition (grade='excellent') location '/testM/excellent/';

alter table studentX add partition (grade='good') location '/testM/good/';

alter table studentX add partition (grade='moderate') location '/testM/moderate/';

#加载数据

load data inpath "/testtry/studentsm.txt" into table studentX partition (grade='excellent');

load data inpath "/testtry/students.txt" into table studentX partition (grade='good');

show partitions studentX;

select * from studentX where grade='excellent';

表删除操作:drop table students;

创建一个名为”test“的table

create table students (name string,age int,city string,class string) row format delimited fields terminated by ',';

load data local inpath "/bin/students.txt" into table students;

###

练习:创建外部表,指定数据存放位置

create EXTERNAL table IF NOT EXISTS studentX (name string,age int,city string,class string) partitioned by (class string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

alter table test add partition (class='one') location '/testmore/one';

对表进行查询

Select * from students;

分区表操作

hive>create table students (name string,age int,city string,class string) partitioned by (class string) row format delimited fields terminated by ',';

hive>load data local inpath "students.txt" into table students partition (class='one');

hive>show partitions students;

hive>select * from students where grade='two';

查询操作

group by、 order by、 join 、 distribute by、 sort by、 clusrer by、 union all

猜你喜欢

转载自blog.csdn.net/Peter_Changyb/article/details/81977665
今日推荐