沉沙
2018-10-08
来源 :
阅读 1709
评论 0
摘要:本篇教程探讨了大数据技术学习笔记(15)Hive的体系结构与安装配置、数据模型,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。
本篇教程探讨了大数据技术学习笔记(15)Hive的体系结构与安装配置、数据模型,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。
<
一.常见的数据分析引擎
Hive:Hive是一个翻译器,一个基于Hadoop之上的数据仓库,把SQL语句翻译成一个 MapReduce程序。可以看成是Hive到MapReduce的映射器。
Hive HDFS
表 目录
数据 文件
分区 目2
2.Pig
3.Impala
4.Spark SQL
二.Hive 的体系结构
用户接口主要有三个:
1.CLI Shell命令行
2.JDBC/ODBC:Hive的Java,与传统JDBC相似
3.Web管理界面
三.Hive的安装和配置
1、安装模式:嵌入模式 ----> 需要Hive自带的一个关系型数据库:Derby
本地模式、远程模式 ----> 需要MySQL数据库的支持
tar -zxvf apache-hive-2.3.0-bin.tar.gz -C ~/training/
环境变量:vi ~/.bash_profile
复制代码
HIVE_HOME=/root/training/apache-hive-2.3.0-bin
export HIVE_HOME
PATH=$HIVE_HOME/bin:$PATH
export PATH
复制代码
2、嵌入模式
(1)使用Hive自带的Derby数据库来存储元信息
(2)Hive只支持一个连接
创建 conf/hive-site.xml
复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:derby:;databaseName=metastore_db;create=true</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>org.apache.derby.jdbc.EmbeddedDriver</value>
</property>
<property>
<name>hive.metastore.local</name>
<value>true</value>
</property>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>file:///root/training/apache-hive-2.3.0-bin/warehouse</value>
</property>
</configuration>
复制代码
初始化MetaStore:
schematool -dbType derby -initSchema
日志:
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
3.远程模式:MySQL
(1)配置MySQL的数据库://www.mysqlfront.de/
(2)配置hive-site.xml: JDBC的参数
复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hiveowner</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>Welcome_1</value>
</property>
</configuration>
复制代码
(3)把MySQL数据库的驱动放到: Hive/lib下
(4)初始化MySQL数据库
老版本的Hive:第一次运行Hive
新版本的hive:schematool -dbType mysql -initSchema
四. Hive的数据模型
内部表:相当于MySQL(Oracle)中表,将数据保存到Hive自己的数据仓库的 目录中: /usr/hive/warehouse
建表:
复制代码
create table emp
(empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int,
deptno int
);
复制代码
创建表,并且指定分隔符
复制代码
create table emp1
(empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int,
deptno int
)row format delimited fields terminated by ',';
复制代码
导入数据:load相当于ctrl+X
load data inpath '/scott/emp.csv' into table emp; ----> 导入HDFS
load data local inpath '/root/temp/***' into table emp; ----> 导入本地文件
创建部门表,保存部门数据
复制代码
create table dept
(deptno int,
dname string,
loc string
)row format delimited fields terminated by ',';
load data inpath '/scott/dept.csv' into table dept;
复制代码
2.分区表:提高查询的效率----> 查看SQL的执行计划
分区 ----> 目录
(*)根据员工的部门号建立分区
复制代码
create table emp_part
(empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int
)partitioned by (deptno int)
row format delimited fields terminated by ',';
复制代码
往分区表中导入数据:指明分区
insert into table emp_part partition(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=10;
insert into table emp_part partition(deptno=20) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=20;
insert into table emp_part partition(deptno=30) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=30
3.外部表 external table 相对于内部表
(*)实验的数据
[root@bigdata11 ~]# hdfs dfs -cat /students/student01.txt
1,Tom,23
2,Mary,24
[root@bigdata11 ~]# hdfs dfs -cat /students/student02.txt
3,Mike,26
(*)定义:(1)表结构 (2)指向的路径
复制代码
create external table students_ext
(sid int,sname string,age int)
row format delimited fields terminated by ','
location '/students';
复制代码
4、桶表:本质也是一种分区表,类似Hash分区
桶 ----> 文件
创建一个桶表,按照员工的职位job分桶
复制代码
create table emp_bucket
(empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int,
deptno int
)clustered by (job) into 4 buckets
row format delimited fields terminated by ',';
复制代码
使用桶表,需要打开一个开关
set hive.enforce.bucketing=true;
使用子查询插入数据
insert into emp_bucket select * from emp1;
5、视图:view
(*)视图是一个虚表,虚:视图是不存数据的
(*)优点:简化复杂的查询
(*)举例:查询部门名称、员工的姓名
复制代码
create view myview
as
select dept.dname,emp1.ename
from emp1,dept
where emp1.deptno=dept.deptno;
select * from myview;
复制代码
6、Hive的查询
(1)查询所有的员工信息
select * from emp1;
(2)查询员工信息:员工号 姓名 薪水
select empno,ename,sal from emp1;
(3)多表查询:查询部门名称、员工的姓名
select dept.dname,emp1.ename
from emp1,dept
where emp1.deptno=dept.deptno;
(4)子查询:hive只支持:from和where后面的子查询
(5)内置函数:select max(sal) from emp1;
(6)n条件函数 就是一个if else: 做一个报表:涨工资,总裁1000 经理800 其他400
select empno,ename,job,sal,
case job when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+800
else sal+400
end
from emp1;
select empno,ename,job,sal,
case job when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+800
else sal+400
end
from emp;
本文由职坐标整理发布,学习更多的大数据技术相关知识,请关注职坐标大技术云计算大技术技术频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号