Hive安装

Hive学习之路 Hive安装

Hive安装

在安装hive的时候遇到过许多坑,翻遍了许多博客。今天安装成功了,所以记录一下,以便自己查阅
服务器是centos7,hadoop是hadoop-2.7.6,hive 是 apache-hive-2.3.0,jdk是1.7.0_80,mysql是5.7

1、hive下载地址
http://archive.apache.org/dist/hive/
我下载版本是 apache-hive-2.3.0-bin.tar.gz

解压文件

tar -zxf apache-hive-2.3.0-bin.tar.gz

重命名

mv apache-hive-2.3.0-bin hive

2、设置环境变量

编辑环境变量

vim /etc/profile

在文件最末尾添加

export HIVE_HOME=/usr/local/kafka/hive
export PATH=$PATH:$HIVE_HOME/bin

Hive安装
3、 mysql驱动架包
mysql驱动地址 http://dev.mysql.com/downloads/connector/j/
我下载的版本 mysql-connector-java-5.1.47.jar
把架包放在hive/lib目录下
Hive安装
4、 设置hive-env.sh文件
进入/hive/conf 目录下

复制hive-env.sh.template文件为 hive-env.sh

cp hive-env.sh.template  hive-env.sh

编辑 hive-env.sh文件

vim hive-env.sh

hive-env.sh文件添加一下内容

export HADOOP_HOME=/usr/local/kafka/hadoop-2.7.6
export HIVE_CONF_DIR=/usr/local/kafka/hive/conf

Hive安装
5、 设置hive-site.xml文件
进入/hive/conf

新建 hive-site.xml 文件

touch hive-site.xml

编辑 hive-site.xml 文件

vim hive-site.xml

hive-env.sh文件添加一下内容

<configuration>
	<property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
        <description>username to use against metastore database</description>
    </property>
   	<property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>123456</value>
        <description>password to use against metastore database</description>
    </property>
   	<property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://192.168.12.77:3306/hive?createDatabaseIfNotExist=true</value>
        <description>JDBC connect string for a JDBC metastore</description>
    </property>
    <property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <!--数据库驱动-->
        <value>com.mysql.jdbc.Driver</value>
        <description>Driver class name for a JDBC metastore</description>
    </property>
</configuration>

6、 初始化元数据库

进入/hive/bin目录下 执行初始化命令

./schematool -initSchema -dbType mysql

Hive安装
如果看到schemaTool completed,恭喜你安装成功了。
这时候我们查看连接的数据库,会发现多了hive库
Hive安装
7、 验证 Hive 安装

[[email protected] kafka]# hive --help
Usage ./hive <parameters> --service serviceName <service parameters>
Service List: beeline cleardanglingscratchdir cli hbaseimport hbaseschematool help hiveburninclient hiveserver2 hplsql jar lineage llapdump llap llapstatus metastore metatool orcfiledump rcfilecat schemaTool version 
Parameters parsed:
  --auxpath : Auxiliary jars 
  --config : Hive configuration directory
  --service : Starts specific service/component. cli is default
Parameters used:
  HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
  HIVE_OPT : Hive options
For help on a particular service:
  ./hive --service serviceName --help
Debug help:  ./hive --debug --help
[[email protected] kafka]# 

8、 初尝Hive 命令

进入hive命令行

[[email protected] kafka]# hive
which: no hbase in (/usr/local/kafka/zookeeper-3.4.10/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/open/maven/rj/apache-maven-3.5.2/bin:/usr/local/java/bin:/usr/local/kafka/hadoop-2.7.6/bin:/usr/local/kafka/hive/bin:/root/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/kafka/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/kafka/hadoop-2.7.6/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/usr/local/kafka/hive/lib/hive-common-2.3.0.jar!/hive-log4j2.properties Async: true
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.
hive> 

创建hive数据库myhive

hive> create database myhive;
OK
Time taken: 0.288 seconds

进入myhive库

hive> use myhive;
OK
Time taken: 0.022 seconds

查看当前数据库

hive> select current_database();
OK
myhive
Time taken: 0.648 seconds, Fetched: 1 row(s)

这时候我们会返现HDFS页面多了myhive文件夹
Hive安装现有一个文件student.txt,将其存入hive中,student.txt数据格式如下:

95002,刘晨,,19,IS
95017,王风娟,,18,IS
95018,王一,,19,IS
95013,冯伟,,21,CS
95014,王小丽,,19,CS
95019,邢小丽,,19,IS
95020,赵钱,,21,IS
95003,王敏,,22,MA
95004,张立,,19,IS
95012,孙花,,20,CS
95010,孔小涛,,19,CS
95005,刘刚,,18,MA
95006,孙庆,,23,CS
95007,易思玲,,19,MA
95008,李娜,,18,CS
95021,周二,,17,MA
95022,郑明,,20,MA
95001,李勇,,20,CS
95011,包小柏,,18,MA
95009,梦圆圆,,18,MA
95015,王君,,18,MA

在数据库myhive创建一张student表

hive>create table student(id int, name string, sex string, age int, department string) row format delimited fields terminated by ",";
OK
Time taken: 0.584 seconds

把文本出入到student表中

hive> load data local inpath "/usr/local/kafka/sd.txt" into table student;
Loading data to table myhive.student
OK
Time taken: 0.521 seconds

现在可以HDFS页面查看到存入的表
Hive安装

查询数据

hive> select * from student where sex='女';
OK
95002	刘晨	女	19	IS
95017	王风娟	女	18	IS
95018	王一	女	19	IS
95014	王小丽	女	19	CS
95019	邢小丽	女	19	IS
95003	王敏	女	22	MA
95012	孙花	女	20	CS
95007	易思玲	女	19	MA
95008	李娜	女	18	CS
95009	梦圆圆	女	18	MA
Time taken: 0.068 seconds, Fetched: 10 row(s)

查看表结构

hive>  desc student;
OK
id                  	int                 	                    
name                	string              	                    
sex                 	string              	                    
age                 	int                 	                    
department          	string              	                    
Time taken: 0.047 seconds, Fetched: 5 row(s)

结束语

我查阅了大量的博客,具有指导意义的博客附上链接
https://www.cnblogs.com/qingyunzong/p/8708057.html