HIVE 是数据仓库,本质上也是数据库。
就是一个数据库。
数据仓库是一个面向主题的(商品的推荐系统内容是商品的信息)、集成的(分散型地数据进行加工处理,原来的数据可能来自MySQL或者文本等)、不可更新的(主要为了决策分析,涉及到的操作主要是数据的查询,增加和删除用的少)、随时间不变化的数据集合,它用于支持企业或组织的决策分析处理。
数据源:
数据存储及管理(ETL):
数据仓库引擎: 各种各样的服务器
前端展示:
星型模型(商品推荐系统为例)
雪花模型(基于星型模型基础上发展的)
传统的方式可以使用Oracle或者MySQL搭建数据仓库,这种方式数据保存在Oracle或者是MySQL中。
HIVE是建立在Hadoop HDFS上的数据仓库的基础架构。 可以用来进行数据提取转化加载(ETL)。 定义了简单的类似SQL的查询语言,称为HQL。 允许熟悉MapReduce的开发者开发自定义的mapper和reducer来处理内建的mapper和reducer无法完成的复杂工作。 Hive是SQL的解析引擎,将SQL语句转移成M/R Job然后在Hadoop上执行。
HIVE的表其实就是HDFS的目录/文件(表是目录,数据是文件)。
解释器、编译器、优化器完成HQL查询语句从词法分析、语法分析、编译、优化以及查询计划(Plan)的生成。生成的查询计划存储在HDFS中,并在随后有MapReduce调用执行。
explain #查看对应语句的执行计划
建立索引可以提高查询速度。
安装模式有三种:
嵌入模式:
本地模式:
远程模式:
不需要做任何配置
$ hive #直接进到了嵌入模式,在当前目录创建metastore_db
#为hive建立相应的mysql账户,并赋予足够的权限
hadoop@ubuntu:~$ mysql -uroot -pmysql
mysql> CREATE USER 'hive' IDENTIFIED BY 'mysql';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hive'@'%' WITH GRANT OPTION;
mysql> flush privileges;
#默认情况下Mysql只允许本地登录,所以需要修改配置文件将地址绑定给注释掉:
mysql -uuname -ppassward
create database hive; #保存元数据
show databases;
#也可以通过MySQL-font等查看
配置文件(本地模式的区别是连接的是本地的数据库,localhost)
在数据库中的tbls中可以查看表的信息。
#进入hive
$ hive
$ hive --service cli
#清屏
ctrl+l
!clear
show tables --这是注释哦;
show functions;
#查看表的结构
desc tb
#查看hdfs上的文件
dfs -ls
#执行操作系统的命令
!ls
#hive的表以目录,数据以文件的形式存储在hdfs中
dfs -ls /user
dfs -lsr /user/hive
#执行HQL语句
#有的时候转换成MR的作业,遍历的话不用执行MR作业
select *** from ***
#执行SQL的脚本
source SQL文件
#静默模式,不打印调试信息
hive -S #silent
#在系统的CLI中执行
hive -S -e 'show tables'
Web只能做查询操作。
解压src源文件,进入hwi的目录
jar cvfM0 hive-hwi-0.13.0.war -C web/
cp hive-hwi-0.13.0.war $HIVE_HOME/lib
vi hive-site.xml
#后边得添加jdk下面的jar包
cp jdk/tools.jar $HIVE_HOME/lib
数组,数组的元素是 [1,2,3,4]:
Map,数组元素是<’English’,85>:
数组和map联合起来:
结构体:
select unix_timestamp();
表分为:
create table t1
(tid int, tname string, age int)
create table t2
(tid int, tname string, age int)
location '/mytable/hive/t2' #指定HDFS中的其他位置
create table t3
(tid int, tname string, age int)
row format delimited fields terminated by ',';
create table t4
as
select * from t3; #列与列之间没有分隔符
create table t5
row format delimited fields terminated by ','
as
select * from t3;
#添加新的列
desc t1;
alter table t1 add columns(english int);
desc t1;
#删除表
#如果开启了HADOOP的回收站,那么删除后
drop table t1;
当数据很大的时候,需要按照一定的条件分区,这样可以提高查询效率。 查看查询效率,可以查看查询计划。
#未建立分区表的查询计划
explain select * from sample_data where gender='M';
#建立分区表的查询计划,执行计划从下往上读
explain select * from partition_table where gender='M';
查询性别是”M”学生需要进行全文扫描,分区后降低了扫描的记录数提高了查询的效率。
create table partition_table
(sid int, sname string)
partitioned by (gender string)
row format delimited fields terminated by ',';
#向表中添加各种分区数据
insert into table partition_table partition(gender='M')
select sid name from sample_data
where gender='M';
insert into table partition_table partition(gender='F')
select sid name from sample_data
where gender='F';
外部表的HIVE中只有表的定义与结构没有数据,数据存放在HDFS中。创建表和加载数据一次性完成。 内部表HIVE数据仓库中也是有数据的。
#将数据放在hdfs上
hdfs dfs -put student01.txt /input
hdfs dfs -put student02.txt /input
hdfs dfs -put student03.txt /input
create external table external_table
(sid int, sname string, age int)
row format delimited fields terminated by ','
location '/input'; #外部表创建的时候location指向HDFS中
#删除外部HDFS中的数据会影响hive查询的数据记录
select * from external_table
hdfs dfs -rm /input/student03.txt
select * from external_table
create table bucket_table
(sid int, sname string, age int)
clustered by (sname) into 5; #根据sname进行hash运算,创建5个桶的桶表
desc bucket_table;
部门表和员工表: 注意外键
要求功能: 查询员工信息:员工号,姓名,月薪,年薪 ,部门名称
#创建视图
create viem empinfo
as
select e.empno, e.ename, e.sal, e.sal*12 annlsal,d.name
from emp e,dept d
where e.deptno=d.deptno
#从视图中查询数据,和查询表的方法完全一样
select * from empinfo
HIVE不支持物化视图,视图中存数据。