前提条件:已经安装好如下软件

Eclipse4.5  hadoop-2.7.3  jdk1.7.0_79

此篇文章基于上一篇文章:zookeeper高可用集群搭建

什么是Hive?

1、Hive是一个基于Hadoop文件系统之上的数据仓库结构。它为数据仓库的管理提供了许多功能:数据ETL(抽取、转换和加载)工具、数据存储管理和大型数据集的查询和分析能力。

2、同时Hive定义了类SQL的语句;它能够将结构化的数据文件映射为一张数据库表,并提供简单的SQL查询功能。还允许开发人员方便的使用Mapper和Reducer操作,可以将SQL语句转化为MapReduce任务运行,这对MapReduce框架来说是一个强有力的支持。

3、Hive的优势在于处理大数据,对于处理小数据没有优势,因为Hive的执行延迟比较高;主要延迟是发生在启动线程部分。

一、开始安装Hive:(仅在CloudDeskTop上安装)

上传安装文件到/software目录下;

下载地址:http://mirrors.shuosc.org/apache/hive/

解压到/software目录下,并修改它的名字;

[hadoop@CloudDeskTop software]$ mv apache-hive-1.2.2-bin/ hive-1.2.2

 配置Hive:

[hadoop@CloudDeskTop software]$ cd /software/hive-1.2.2/conf/
[hadoop@CloudDeskTop conf]$ cp hive-default.xml.template hive-site.xml
[hadoop@CloudDeskTop conf]$ vi hive-site.xml

334行:hdfs集群存放hive仓库元数据的位置

 333   <property>334     <name>hive.metastore.warehouse.dir</name>335     <value>/user/hive/warehouse</value>336     <description>location of default database for the warehouse</description>337   </property>

46行

  45   <property>46     <name>hive.exec.scratchdir</name>47     <value>/tmp/hive</value>48     <description>HDFS root scratch dir for Hive jobs which gets created with write all (733) permission. For each connecting user, an HDFS scratch dir: ${hive.exec.scratchdir}/&lt;userna     me&gt; is created, with ${hive.scratch.dir.permission}.</description>49   </property>

2911行

2910   <property>
2911     <name>hive.server2.logging.operation.log.location</name>
2912     <value>/tmp/hive/operation_logs</value>
2913     <description>Top level directory where operation logs are stored if logging functionality is enabled</description>
2914   </property>

51行

  50   <property>51     <name>hive.exec.local.scratchdir</name>52     <value>/tmp/hive</value>53     <description>Local scratch space for Hive jobs</description>54   </property>

56行

  55   <property>56     <name>hive.downloaded.resources.dir</name>57     <value>/tmp/hive/resources</value>58     <description>Temporary local directory for added resources in the remote file system.</description>59   </property>

[hadoop@CloudDeskTop conf]$ cp -a hive-log4j.properties.template hive-log4j.properties 

[hadoop@CloudDeskTop conf]$ vi hive-log4j.properties

使用[tail -f hive.log]可以动态实时查看日志的最新情况;

 17 # Define some default values that can be overridden by system properties18 hive.log.threshold=ALL19 hive.root.logger=INFO,DRFA    #logs目录需要自己创建,用来存放你操作hive数据库后产生的日志20 hive.log.dir=/software/hive-1.2.2/logs21 hive.log.file=hive.log

 72 #log4j.appender.EventCounter=org.apache.hadoop.hive.shims.HiveEventCounter73 log4j.appender.EventCounter=org.apache.hadoop.log.metrics.EventCounter

 [hadoop@CloudDeskTop software]$ vi /software/hive-1.2.2/bin/hive-config.sh

在最后添加:

export JAVA_HOME=/software/jdk1.7.0_79
export HADOOP_HOME=/software/hadoop-2.7.3
export HIVE_HOME=/software/hive-1.2.2

二、启动Hive前的准备:

【1、在slave节点启动zookeeper集群(小弟中选个leader和follower)】

  cd /software/zookeeper-3.4.10/bin/ && ./zkServer.sh start && cd - && jps
  cd /software/zookeeper-3.4.10/bin/ && ./zkServer.sh status && cd -

【2、master01启动HDFS集群】cd /software/ && start-dfs.sh && jps

【3、master01启动YARN集群】cd /software/ && start-yarn.sh && jps

【YARN集群启动时,不会把另外一个备用主节点的YARN集群拉起来启动,所以在master02执行语句:】

cd /software/ && yarn-daemon.sh start resourcemanager && jps

【4、查看两个master谁是主节点:】
[hadoop@master01 software]$ hdfs haadmin -getServiceState nn1
active (主节点)
[hadoop@master01 software]$ hdfs haadmin -getServiceState nn2
standby (备用主节点)

【5、查看两个resourcemanager谁是主:】
[hadoop@master01 hadoop]$ yarn rmadmin -getServiceState rm1
active(主)
[hadoop@master01 hadoop]$ yarn rmadmin -getServiceState rm2
standby(备用)

启动Hive:

【此时会在当前目录下生成:】

【如果发生问题,删除如下两个文件】

【如果问题依旧不能解决,直接删除metastore_db文件试一试】

大数据学习交流群:217770236 让我我们一起学习大数据

三、Hive测试

【下面的测试都遵循如下两图的目录准则:】

【1、首先 cd /home/hadoop/test/hive/src】

新增t_user数据文件:并上传到hdfs集群;[hdfs dfs -put t_user /user/hive/warehouse/mmzs.db/t_user/]

新增myuser数据文件:并上传到hdfs集群;[hdfs dfs -put myuser /user/hive/warehouse/mmzs.db/t_user/]

新增myuser02数据文件:暂时不上传,后面用另外一种方式上传;

【2、首先 cd /software/hive-1.2.2/bin】

【创建测试数据库:】create database mmzs;

【不进入./hive命令终端执行sql语句】 echo -e "select * from mmzs.t_user;"|./hive -S

【3、将结果写入test.sql文件,将执行其结果输出到指定目录的文件中】

【不进入./hive命令终端,动态执行sql语句】

【4、在hive数据库中创建表,[row format delimited fields terminated by '\t']是指定数据每行按照空白进行分割】

 create table mmzs.t_user(userId int, username string, userage int, userheight double) row format delimited fields terminated by '\t';

【另外一种上传数据文件到集群hive数据库的方式,load data就是上传的关键字,local是将本地文件拷贝到hive数据库,不加就是导入hdfs集群数据到hive数据库是一种剪切方式】

【本地文件不覆上传 】./hive -S -e "load data local inpath '/home/hadoop/test/hive/src/myuser02' into table mmzs.t_user;" 

【使用overwrite本地文件覆盖上传】./hive -S -e "load data local inpath '/home/hadoop/test/hive/src/myuser02' overwrite into table mmzs.t_user;"

【hdfs文件不覆盖上传 】./hive -S -e "load data local inpath '/data/myuser' into table mmzs.t_user;"

【小结:】注意:

使用HDFS上传和使用load data导入本地文件:从本质意义上讲都是文件的转移过程。
如果转移的文件是来自于本地则发生数据拷贝,如果转移的文件是来自于HDFS文件系统则发生数据移动;
overwrite关键字在load data句法中将导致hive表中的数据先被清空,然后再转移数据,即发生hive表的覆盖写入操作;如果没有overwrite关键字则发生数据文件的追加操作,新建一个文件加后缀"已存在同名文件名字_copy_数字";
【5、在hive数据库中的t_user表中添加一条数据:会产生map任务】
insert into mmzs.t_user(userid,username,userage,userheight) values (9,'zhaoyuan',34,1.78);
执行过程比较缓慢,可以【http://active状态的master的IP地址:8088】查看任务进度;

【6、hive支持大多数sql语句,但也有不支持的sql语句:】

  insert into mmzs.t_user select 1,'zhaoyun02',78,1.90;

【7、聚合函数有个统计过程,会产生MapTask和ReduceTask】

 select count(userid) from t_user ;

 select avg(userage) from t_user;

【8、非聚合函数,不会产生Job任务】

 select username,reverse(username),userage from mmzs.t_user;

 select username,length(username),userage from mmzs.t_user;

【9、hive数据库结果的下载】

下载到本地:(1个Job)

下载到hdfs集群:(3个Job,应该是因为有3个slave,有3个备份的原因)

【10、通过子查询的方式来创建表和创建数据:显示的是3个Job(Total jobs只是计划的任务数),实际运行了1个;请用浏览器的方式查看或者你可以看到只出现了Launching Job 1 out of 3】

 create table mmzs.t_user as select * from mmzs.t_user where 1=2;(拷贝后的表,读取数据时的分隔符会回复到默认的)

insert into mmzs.t_user_new select * from mmzs.t_user where userid=1; 

【11、在10的基础上,通过hdfs集群的方式为表新增数据】

通过hdfs集群,直接将原表中的数据文件拷贝到新表中:

由于myuser02中的数据文件创建时使用的是Tab键作为分隔符,所以出现如下情况:

所以我们重新传了用^A(hive的默认分隔符,通过按"ctrl+V"和"ctrl+A"键产生)作为分隔符的数据文件:

然后我们就能看到表中新增的数据了:

【12、第一阶段测试完成,删除所有测试的表】

  hive> show databases;hive> use mmzs;hive> show tables;hive> drop table t_user;hive> drop table if exists t_user_new;

四、Hive高级测试(一)

【hive>均是在 cd /software/hive-1.2.2/bin 目录下执行./hive命令或者./hive --hiveconf hive.root.logger=ERROR,console命令】

【1、创建测试所需表】

hive> use mmzs;
hive> create table if not exists emp(eno int,ename string,eage int,bithday date,sal double,com double,gender string,dno int) row format delimited fields terminated by '\t';
hive> create table if not exists dept(dno int,dname string,loc string) row format delimited fields terminated by '\t';

【2、创建测试所需表的数据】

cd /home/hadoop/test/hive/src

hdfs dfs -put emp01 /user/hive/warehouse/mmzs.db/emp
hdfs dfs -put dept01 /user/hive/warehouse/mmzs.db/dept

【3、测试开始】

hive> use mmzs;

【遇到create和insert操作会产生Job作业(只有Map作业)】

【遇到含有group by、order by操作或聚合函数操作时都会产生Job作业(有Map和Reduce)】

【当有同时含有group by和order by时会产生两个Job:(有Map,也有Reduce)】

select dno,gender,count(1) renshu from emp where eage>25 group by gender,dno order by renshu desc;

【产生一个Job:(只有Map,没有Reduce)】

select e.*,d.* from emp e,dept d where e.dno=d.dno;

select eno,ename,e.dno,d.dname from emp e inner join dept d on e.dno=d.dno;

【多列排序:仍然只产生一个Job,(有Map,也有Reduce)】
select dno,eno,ename,sal,com from emp order by sal desc,com desc;

【产生两个Job:(因为多了个group by)(有Map,也有Reduce)】

select dno,eno,ename,sal,com from emp group by dno,eno,ename,sal,com order by sal desc,com desc;

select d.dno,avg(sal) avgsal from emp e inner join dept d on e.dno=d.dno where eage>20 group by d.dno order by avgsal;

【产生三个Job作业:(有Map,也有Reduce)子查询只支持在from后面写这一种格式】
select d.dname,avgsal from (select d.dno,avg(sal) avgsal from emp e inner join dept d on e.dno=d.dno where eage>20 group by d.dno order by avgsal)mid,dept d where mid.dno=d.dno;

 遇到子查询不能处理的,可以将子查询结果用去做关联查询;

以下语句会出错:(因为hive目前不支持where后面子查询的语法)

select e.* from emp e where e.sal>(select avg(sal) from emp dno=e.dno);

【只支持limit查看前N条记录:不能用它来像mysql那样子分页,不产生Job】
select * from emp limit 6;  //不产生Job
select * from emp limit 2,5;//会产生错误

【Hive的分页查询:只产生一个Job,(有Map,也有Reduce)】

select row_number() over() seq,e.* from emp e;
//row_number()是标识添加行号的关键字,按照over内的条件进行排列后,添加行号seq字段,
//over内不写条件,默认是在,将按照第一个字段降序排列后的结果,前加行号

select * from (select row_number() over(order by sal desc) seq,e.* from emp e) mid where mid.seq>5 and mid.seq<11;  //分页查询

五、Hive高级测试(二)

【Hive目前不支持索引】

【Hive支持分区:静态分区和动态分区的创建表的方式是一样的】

create table t_user(userid int,username string,userage int) partitioned by(dno int) row format delimited fields terminated by '\t';//单级分区;dno是分区的字段名,添加分区时必须名字一样
create table t_user(userid int,username string,userage int) partitioned by(dno int,gender string) row format delimited fields terminated by '\t';//多级分区;dno是分区的字段名,添加分区时必须名字一样

【测试数据:】

 进入目录:cd /home/hadoop/test/hive/src

【静态分区:】

alter table t_user add partition(dno=1);//添加分区,分区名是"dno=1"
alter table t_user drop partition(dno=1);//删除分区名是"dno=1"的分区 

添加1、2、3、4共四个分区;

【为静态分区表添加数据:4种方式】
// 集群中操作
hdfs dfs -put bak /user/hive/warehouse/mmzs.db/t_user/dno=1//①
// ./hive中操作
load data local inpath '/home/hadoop/test/hive/src/bak' into table t_user partition(dno=1);//②
load data local inpath '/home/hadoop/test/hive/src/bak' overwrite into table t_user partition(dno=1);//②
insert into t_user partition(dno=3) select eno,ename,eage from emp where eno<4;//③必须添加所有字段,不可以写t_user(userid,username,userage)的方式
insert overwrite table t_user partition(dno=4) select eno,ename,eage from emp where eno>4 and eno<9;//④分页

【动态分区:】  
【为静态分区表添加数据:只支持insert方式】
//临时修改,启动动态分区;也可在配置文件hive-site.xml配置,永久生效
set hive.exsec.dynamic.partition.mode=nonstrict;//hive-site.xml
insert into t_user partition(dno) values(1,"liganggang",89,1);//插入数据,分区确定在后面指定;
insert into t_user partition(dno=5) select eno,ename,eage from emp where eno<4;//如果没有的分区会自动创建//多级动态分区;多级静态和单级静态类似
insert into t_user partition(dno,gender) select eno,ename,eage,dno,gender from emp;//select中的dno,gender的顺序要和建表是的partition(dno,gender)的顺序和保持一致;否则会乱建分区
//into改变成overwrite table表示有相同的时,覆盖插入;

truncate table t_user;//清空表中的数据
drop table t_user;//删除表

数据移植小结:
A、从本地到Hive表:
  使用HDFS的put命令上传数据文件
  使用Hive的load data local inpath句法导入数据文件
B、从Hive表到Hive表
  使用HDFS的cp命令实现数据文件拷贝
  使用普通的insert into句法插入单条记录数据
  使用insert....select...from...句法实现批量条件数据拷贝
  使用insert overwrite table....select....句法实现数据拷贝
C、从Hive表到本地
  使用HDFS的get命令下载数据文件
  使用insert overwrite local directory句法实现Hive表批量条件数据导出
  使用输出定向符(>或>>)直接通过标准输出流将select查询结果其写入本地文件

六、Hive高级测试(三)
1、构建数据
[hadoop@CloudDeskTop src]$ pwd
/home/hadoop/test/hive/src

 //创建表后hdfs dfs -put testarray /user/hive/warehouse/mmzs.db/tuser01hdfs dfs -put testmap /user/hive/warehouse/mmzs.db/tuser02hdfs dfs -put testarraymap user/hive/warehouse/mmzs.db/tuser03hdfs dfs -put teststruct /user/hive/warehouse/mmzs.db/tuser04

 2、进行测试
#首先[hadoop@CloudDeskTop ~]$ cd /software/hive-1.2.2/bin
[hadoop@CloudDeskTop bin]$ ./hive 

#创建的外部表不会因为表的删除而删除数据;
#外部表不会在默认的数据存放地址新建文件夹,从始至终都只有指定的目录下有数据//表的数据存放在指定外部hdfs集群地址"/test"目录中:
hive> create external table tuser00(userid int,username string,userage int) row format delimited fields terminated by '\t' location '/test';//在数据库下创建外部表就需要添加数据库的前缀
hive> use mmzs;
hive> create external table mmzs.tuser00(userid int,username string,userage int) row format delimited fields terminated by '\t' location '/test';

//创建一个带有数组的表:
hive> create table if not exists mmzs.tuser01(id bigint,name string,loves array<string>) row format delimited fields terminated by '\t' collection items terminated by ',';

select id,name,loves[1] from mmzs.tuser01;

//创建一个带有map的表:
hive> create table if not exists mmzs.tuser02(id bigint,name string,info map<string,double>) row format delimited fields terminated by '\t' collection items terminated by ',' map keys terminated by ':';
//注:map keys terminated by ':'中的 :号是元素之间的分隔符,为了解析数据,可以是任意符号。

hive> select id,name,info["age"],info['height'] from mmzs.tuser02;//(如果查询的字段没有,会返回一个人Null)

//创建一个带有数组和map的表
hive> create table mmzs.tuser03(id bigint,name string,loves array<string>,info map<String,double>) row format delimited fields terminated by '\t' collection items terminated by ',' map keys terminated by ':';

//Struct类型(info是一个STRUCT类型,那么可以通过info.height得到这个用户的身高)
hive> create table mmzs.tuser04(id bigint,name string,info struct<age:int,height:double>) row format delimited fields terminated by '\t' collection items terminated by ',';//(如果查询的字段没有,会报异常)
hive> select id,name,info.age,info.height from mmzs.tuser04;

转载于:https://www.cnblogs.com/mmzs/p/8079491.html

Hive环境搭建及测试相关推荐

  1. hive环境搭建提示: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument

    hive环境搭建提示: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument SLF4J: A ...

  2. tensorflow环境下的识别食物_Tensorflow object detection API 搭建属于自己的物体识别模型——环境搭建与测试...

    1.开发环境搭建 ①.安装Anaconda 建议选择 Anaconda3-5.0.1 版本,已经集成大多数库,并将其作为默认python版本(3.6.3),配置好环境变量(Anaconda安装则已经配 ...

  3. Hive环境搭建--轻量级安装so easy!

    Hive环境搭建 一.准备工作 1.安装包下载 2.虚拟机需先安装好hadoop 3.安装mysql 二.安装hive 三.配置环境变量 1.配置环境变量 2.修改hive配置文件 四.启动hive ...

  4. Hadoop+hive+flask+echarts大数据可视化项目之hive环境搭建与系统数据的分析思路

    Hadoop+hive+flask+echarts大数据可视化项目(四) --------------hive环境搭建与系统数据的分析思路---------------- 关注过Hadoop+hive ...

  5. Linux 下的 ActiveMQ C++ 环境搭建与测试

    Linux 下的 ActiveMQ C++ 环境搭建与测试 一.下载安装jdk jdk官网下载地址:https://www.oracle.com/technetwork/java/javase/dow ...

  6. Tensorflow object detection API 搭建属于自己的物体识别模型1环境搭建与测试

    https://blog.csdn.net/dy_guox/article/details/79111949 后续博客地址(附带视频教程) ------------------------------ ...

  7. 【LoRa32U4II】介绍以及基于Arduino IDE编译环境搭建及测试

    目录 LoRa 模块LoRa32u4 II介绍 LoRa32u4 II 资料下载 LoRa32u4 II 规格介绍 LoRa32u4 II 脚位说明 编译环境介绍 电脑系统 编译软件 Arduino需 ...

  8. monkey测试linux设备,Monkey测试环境搭建及测试执行

    一.环境准备 1.JDK 2.Android SDK 二.JAVA环境的搭建 1.JDK 下载地址:https://www.oracle.com/technetwork/java/javase/dow ...

  9. Mac上Hive环境搭建

    本文介绍在Mac上搭建Hive环境. 建议首先配置好Hadoop,搭建与配置可以参考我之前的博文Mac Hadoop的安装与配置. 当然你也可以选择使用Docker搭建环境,本文不作介绍. 安装 对于 ...

  10. 数据仓库组件:Hive环境搭建和基础用法

    本文源码:GitHub || GitEE 一.Hive基础简介 1.基础描述 Hive是基于Hadoop的一个数据仓库工具,用来进行数据提取.转化.加载,是一个可以对Hadoop中的大规模存储的数据进 ...

最新文章

  1. 字节跳动教育大规模裁员:赔付方式N+2
  2. Spring注解详解
  3. unix环境高级编程-线程(2)
  4. 正则表达式 \w \d . \s常用字符的简写
  5. arma找不到合适的模型_TAP300R系列直角方肩立铣刀,您还在为找不到合适刀具发愁吗?...
  6. 《系统集成项目管理工程师》必背100个知识点-12项目整体管理的过程
  7. MySQL数据库开发 (视频)
  8. python自动化测试-【自动化测试】Python - unittest单元测试框架
  9. 哪些因素影响大数据的发展
  10. 1808福师计算机基础第二次作业,1808福师计算机基础第一次作业.pdf
  11. 学业水平测试计算机考试软件,普通高中学业水平考试系统
  12. Arduino 和 TB6612FNG 驱动直流电机
  13. 配置微信公众号之公众号绑定小程序
  14. 【CISCN2021】第十四届全国大学生信息安全竞赛初赛-writeup
  15. Hive数据类型(转:https://blog.csdn.net/xiaoqi0531/article/details/54667393)
  16. 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题...
  17. 陶瓷纤维毯行业调研报告 - 市场现状分析与发展前景预测
  18. VR光学设计的关键参数
  19. 神经网络芯片概念股,图神经网络预测股票
  20. Springboot与手机的互动

热门文章

  1. A[1083]List Grade 水题
  2. 【同余最短路】P3403+P2371+P2662+牛客4853D
  3. 【cf:1100F】 Ivan and Burgers(多次区间最大异或值查询----线性基+离线+思维)
  4. 【hdu1018】Big Number(求n!的位数----斯大林公式/log函数)
  5. php 覆盖静态方法吗,覆盖PHP中的静态方法
  6. java int integer_浅谈java中int和Integer的区别
  7. 比赛结束(第二届易观算法大赛)
  8. Simple-RNN with Keras
  9. 翻译: 2.6 概率论 深入神经网络 pytorch
  10. 自动驾驶 2-1 传感器和计算硬件 Sensors and Computing Hardware