一、简介
hive是基于Hadoop的一个数据仓库工具。 hive数据仓库工具能将结构化的数据文件映射为一张数据库表,并提供SQL查询功能,能将SQL语句转变成MapReduce任务来执行。 Hive的优点是学习成本低,可以通过类似SQL语句实现快速MapReduce统计,使MapReduce变得更加简单。 hive十分适合对数据仓库进行统计分析。 延迟较高(MapReduce本身延迟,Hive SQL向MapReduce转化优化提交),适合做大数据的离线处理(TB PB级别的数据,统计结果延迟1天产出)。不擅长小量的数据,和实施计算。 数据库和数据仓库的区别:数据库处理的数据量小、数据价值高。数据 仓库体量大,数据价值 较低。
二、执行流程
在hive架构中,各个节点的作用: HDFS:用来存储hive仓库的数据文件 yarn:用来完成hive的HQL转化的MR程序的执行 MetaStore:保存管理hive维护的元数据 Hive:用来通过HQL的执行,转化为MapReduce程序的执行,从而对HDFS集群中的数据文件进行统计。 
三、hive安装过程
基于hdfs、yarn、mysql已安装 完成 的机器部署。在安装前应可以正常启动如下进程 NameNode NodeManager DataNode SecondaryNameNode ResourceManager
1.上传apache-hive安装包到Linux服务器上,解压后改名字
[root@hadoop ~]# tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/installs
[root@hadoop ~]# mv apache-hive-1.2.1-bin hive1.2.1
2.添加到环境变量。vim /etc/profile
export HIVE_HOME=/opt/installs/hive1.2.1
export PATH=$PATH:$HIVE_HOME/bin
3.重新加载环境变量 使刚才的 配置生效
[root@hadoop ~]# source /etc/profile
4.配置hive的配置文件
1.hive-env.sh
先复制一个配置文件,并更改(在安装目录的conf目录下)
cp hive-env.sh.template hive-env.sh
vim hive-env.sh
# 配置hadoop目录
HADOOP_HOME=/opt/installs/hadoop2.9.2/
# 指定hive的配置文件目录
export HIVE_CONF_DIR=/opt/installs/hive1.2.1/conf/
2.hive-site.xml
先复制一个配置文件,并更改(在安装目录的conf目录下)
cp hive-default.xml.template hive-site.xml
删除configuration里面的所有内容,并添加下列内容(删除中文注释)
<configuration>
<!--hive的元数据保存在mysql中,需要连接mysql,这里配置访问mysql的信息-->
<!--url:这里必须用ip-->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop10:3306/hive</value>
</property>
<!--drivername-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<!--username-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<!--password-->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>
</configuration>
5.登录mysql数据库创建一个数据库
create database hive
6.由于连接需要一个jar包,所以需要提前 将jar包放到安装目录的lib目录下 
四、启动使用hive
1.在启动之前需要先启动hdfs和yarn 2.初次使用需要初始化元数据
schematool -dbType mysql -initSchema
3.1第一种启动方式
输入hive启动
[root@hadoop10 ~]# hive
Logging initialized using configuration in jar:file:/opt/installs/hive1.2.1/lib/hive-common-1.2.1.jar!/hive-log4j.properties
hive>
3.2第二种启动方式
启动hive的服务器,可以允许远程连接方式访问。
[root@hadoop10 ~]# hiveserver2
[root@hadoop10 ~]# hiveserver2 &
使用这种方式启动可以使用其他客户端 操作,或者使用可视化工具进行连接
beeline客户端
[root@hadoop10 ~]# beeline
beeline> !connect jdbc:hive2://hadoop10:10000
回车输入mysql用户名
回车输入mysql密码
4.客户端操作命令
1.查看数据库
hive> show databases;
2. 创建一个数据库
hive> create database test;
3. 查看database
hive> show databases;
4. 切换进入数据库
hive> use test;
5.查看所有表
hive> show tables;
6.创建一个表
hive> create table t_user(id int,name string,age int);
7. 添加一条数据(转化为MR执行--不让用,仅供测试)
hive> insert into t_user values('1001','zhangsan',20);
8.查看表结构
hive> desc t_user;
9.查看表的schema描述信息。(表元数据,描述信息)
hive> show create table t_user;
# 明确看到,该表的数据存放在hdfs中。
10 .查看数据库结构
hive> desc database baizhi;
11.查看当前库
hive> select current_database();
12 其他sql
select * from t_user;
select count(*) from t_user; (Hive会启动MapReduce)
select * from t_user order by id;
五、Java操作hive
1.导入maven依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
</dependency>
2.编写代码进行测试
public static void main(String[] args) throws Exception {
BasicConfigurator.configure();
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://hadoop10:10000/baizhi","root","admins");
String sql = "select * from t_user1";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id+":"+name+":"+age);
}
rs.close();
pstm.close();
conn.close();
}
六、hive中的数据类型
primitive,array,map,struct 
create table t_tab(
score array<float>,
字段名 array<泛型>
);
create table t_tab(
score map<string,float>
);
create table t_tab(
info struct<name:string,age:int,sex:char(1)>,
列名 struct<属性名:类型,属性名:类型>
);
七、Hive数据导入
1.建表
create table t_person(
id string,
name string,
salary double,
birthday date,
sex char(1),
hobbies array<string>,
cards map<string,string>,
addr struct<city:string,zipCode:string>
) row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by '|'
lines terminated by '\n';
 2.导入数据 例: 2,李四,9000.0,2019-8-9,0,抽烟-喝酒-烫头,123456|中国银行-22334455|建设银行,郑州-45000
# 在hive命令行中执行
-- local 代表本地路径,如果不写,代表读取文件来自于HDFS
-- overwrite 是覆盖的意思,可以省略。
load data [local] inpath ‘/opt/datas/person1.txt’ [overwrite] into table t_person;
# 本质上就是将数据上传到hdfs中(数据是受hive的管理)
3.系统自带分隔符 首先需要导入两个jia包 
json分隔符:
例:{"id":1,"name":"zhangsan","sex":0,"birth":"1991-02-08"}
1.建表
create table t_person2(
id string,
name string,
sex char(1),
birth date
)row format serde 'org.apache.hive.hcatalog.data.JsonSerDe';
2.加载文件数据
load data local inpath '/opt/person.json' into table t_person2;
正则分隔符:
例:WARN 192.168.1.2 2019-10-19 QQ com.baizhi.service.IUserService#login
1.建表
create table t_access(
level string,
ip string,
log_time date,
app string,
service string,
method string
)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'--正则表达式的格式转化类
with serdeproperties("input.regex"="(.*)\\s(.*)\\s(.*)\\s(.*)\\s(.*)#(.*)");--(.*) 表示任意字符 \\s表示空格
2.加载文件数据
load data local inpath '/opt/access.log' into table t_access;
|