0


Hive的集群的搭建-内嵌模式-本地模式-远程链接

一、hive的简介

1.1简介

    Hive 是一个框架,可以通过编写sql的方式,自动的编译为MR任务的一个工具

Hive是一个数据仓库工,可以将数据加载到表中,编写sql进行分析,底层依赖Hadoop,所以每一次都需要启动hadoop(hdfs以及yarn),Hive的底层计算框架可以使用MR、也可以使用Spark、TEZ,Hive不是数据库,而是一个将MR包了一层壳儿。类似于一个中介

1.2hive网址

    Hive官网地址:Apache Hive,目前最新的版本稳定版(realease):3.1.2

二、安装

2.1内嵌模式

    前提是在hive的官网下载3.1.2的安装包
上传 压缩包  /opt/modules
解压:
tar -zxvf apache-hive-3.1.2-bin.tar.gz -C /opt/installs/
重命名:
mv apache-hive-3.1.2-bin/ hive
配置环境变量:vi /etc/profile
  export HIVE_HOME=/opt/installs/hive
  export PATH=$HIVE_HOME/bin:$PATH
刷新环境变量:
source /etc/profile
配置hive-env.sh
进入这个文件夹下:/opt/installs/hive/conf
cp hive-env.sh.template hive-env.sh
修改hive-env.sh 中的内容:
export HIVE_CONF_DIR=/opt/installs/hive/conf
export JAVA_HOME=/opt/installs/jdk
export HADOOP_HOME=/opt/installs/hadoop
export HIVE_AUX_JARS_PATH=/opt/installs/hive/lib

进入到conf 文件夹下,修改这个文件hive-site.xml
cp hive-default.xml.template hive-site.xml
接着开始修改:
把Hive-site.xml 中所有包含${system:java.io.tmpdir}替换成/opt/installs/hive/tmp。如果系统默认没有指定系统用户名,那么要把配置${system:user.name}替换成当前用户名root。

启动集群:

start-all.sh

给hdfs创建文件夹

[root@go01 conf] # hdfs dfs -mkdir -p /user/hive/warehouse 
[root@go01 conf] # hdfs dfs -mkdir -p /tmp/hive/ 
[root@go01 conf] # hdfs dfs -chmod 750 /user/hive/warehouse 
[root@go01 conf] # hdfs dfs -chmod 777 /tmp/hive

初始化元数据,因为是内嵌模式,所以使用的数据库是derby

schematool --initSchema -dbType derby

在hive-site.xml中,3215行,96列的地方有一个非法字符

将这个非法字符,删除,保存即可。然后需要再次进行元数据的初始化操作

测试是否激活成功

输入hive  
hive> show databases;
OK
default

2.1.1测试内嵌模式

-- 进入后可以执行下面命令进行操作:
hive>show databases;      -- 查看数据库
hive>show tables;           -- 查看表
-- 创建表
hive> create table dog(id int,name string);
hive> select * from dog;
hive> insert into dog values(1,'wangcai');
hive> desc dog; -- 查看表结构
hive> quit; -- 退出

2.1.2内嵌模式的弊端

假如有一个窗口在使用你的hive,另一个窗口能进入,但是会报错

2.2本地模式-最经常使用的模式

使用本地模式的最大特点是:将元数据从derby数据库,变为mysql数据库,并且支持多窗口同时使用,并且后边还可以进行远程链接

首先,先检查虚拟机的mysql是否正常

systemctl status mysqld

然后,删除以前的derby数据

进入到hive中,删除 
rm -rf metastore_db/ derby.log

其次,修改配置文件 hive-site.xml

直接添加就行

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
--><configuration>

<!--配置MySql的连接字符串-->
<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
  <description>JDBC connect string for a JDBC metastore</description>
</property>
<!--配置MySql的连接驱动-->
<property>
  <name>javax.jdo.option.ConnectionDriverName</name>
  <value>com.mysql.cj.jdbc.Driver</value>
  <description>Driver class name for a JDBC metastore</description>
</property>
<!--配置登录MySql的用户-->
<property>
  <name>javax.jdo.option.ConnectionUserName</name>
  <value>root</value>
  <description>username to use against metastore database</description>
</property>
<!--配置登录MySql的密码-->
<property>
  <name>javax.jdo.option.ConnectionPassword</name>
  <value>123456</value>
  <description>password to use against metastore database</description>
</property>
<!-- 以下两个不需要修改,只需要了解即可 -->
<!-- 该参数主要指定Hive的数据存储目录  -->
<property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/user/hive/warehouse</value>
  </property>
<!-- 该参数主要指定Hive的临时文件存储目录  -->
 <property>
    <name>hive.exec.scratchdir</name>
    <value>/tmp/hive</value>
  </property>

</configuration>

将mysql的驱动包,上传至 hive 的lib 文件夹下

初始化元数据(本质就是在mysql中创建数据库,并且添加元数据)

schematool --initSchema -dbType mysql

测试:打开两个会话窗口测试,看是否会出错

create database mydb01;
use mydb01;

create table stu (id int,name string);

insert 语句 走MR任务
insert into stu values(1,'wangcai');
select * from stu;
select * from stu limit 10; 
不走MR任务。
创建表的时候,varchar类型需要指定字符长度,否则报错!

三、远程链接

如果想使用远程链接工具链接虚拟机hive需要配置hive文件

3.1配置远程链接文件

3.1.1创建临时文件

[root@ks ~]# cd /opt/installs/hive/
[root@ks hive]# mkdir iotmp
[root@ks hive]# chmod 777 iotmp

3.1.2前期准备工作

    修改hive-site.xml文件
<!--Hive工作的本地临时存储空间-->
<property>
    <name>hive.exec.local.scratchdir</name>
    <value>/opt/installs/hive/iotmp/root</value>
</property>
<!--如果启用了日志功能,则存储操作日志的顶级目录-->
<property>
    <name>hive.server2.logging.operation.log.location</name>
    <value>/opt/installs/hive/iotmp/root/operation_logs</value>
</property>
<!--Hive运行时结构化日志文件的位置-->
<property>
    <name>hive.querylog.location</name>
    <value>/opt/installs/hive/iotmp/root</value>
</property>
<!--用于在远程文件系统中添加资源的临时本地目录-->
<property>
    <name>hive.downloaded.resources.dir</name>
    <value>/opt/installs/hive/iotmp/${hive.session.id}_resources</value>
</property>
hive.downloaded.resources.dir:
在 hdfs 上下载的一些资源会被存放在这个目录下,hive 一定要小写,否则报:
cause: java.net.URISyntaxException: Illegal character in path at index 26: /opt/installs/hive/iotmp/${Hive.session.id}_resources/json-serde-1.3.8-jar-with-dependencies.jar

修改 core-site.xml【hadoop】的

<property>
    <name>hadoop.proxyuser.root.hosts</name>
    <value>*</value>
</property>
<property>
    <name>hadoop.proxyuser.root.groups</name>
    <value>*</value>
</property>
<property>
    <name>hadoop.http.staticuser.user</name>
    <value>root</value>
</property>
<!-- 不开启权限检查 -->
<property>
   <name>dfs.permissions.enabled</name>
   <value>false</value>
</property>

修改集群的三个core-site.xml,记得修改一个,同步一下,并且重启hdfs

xsync.sh core-site.xml
stop-dfs.sh
start-dfs.sh

3.2开始配置远程服务(两个)

3.2.1配置hiveserver2服务

修改hive-site.xml

<property>
    <name>hive.server2.thrift.bind.host</name>
    <value>bigdata01</value>
    <description>Bind host on which to run the HiveServer2 Thrift service.</description>
  </property>
  <property>
    <name>hive.server2.thrift.port</name>
    <value>10000</value>
    <description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
  </property>

然后可以启动

1. 该服务端口号默认是10000
2. 可以单独启动此服务进程,供远程客户端连接;此服务内置metastore服务。
3. 启动方式:
   方法1:
       直接调用hiveserver2。会进入监听状态不退出。
   方法2:
       hive --service hiveserver2 &    # 进入后台启动
   方法3:
      nohup hive --service hiveserver2 >/dev/null 2>&1 & #信息送入黑洞。

启动之后可以看到服务

出现4个服务,就说明hive启动成功,过程可能有点慢,稍微等一会

然后使用beeline进行测试

连接方式:
方式1:
       step1. beeline 回车
       step2. !connect jdbc:hive2://xxxxxx(本机域名):10000 回车
       step3. 输入用户名 回车 数据库用户名
       step4. 输入密码 回车  此处的密码是数据库密码
方法2(直连):
    beeline -u jdbc:hive2://xxxxxx(本机域名):10000 -n 用户名
解析: 
    hive2,是Hive的协议名称
    ip:  Hiveserver2服务所在的主机IP。
    10000,是Hiveserver2的端口号
退出:
    Ctrl+ C 可以退出客户端

然后修改修改hive-site.xml

   修改hive-site.xml的配置
   注意:想要连接metastore服务的客户端必须配置如下属性和属性值
    <property>
        <name>hive.metastore.uris</name> 
        <value>thrift://xxxx(本机域名):9083</value>
    </property>

    解析:thrift:是协议名称
         ip为metastore服务所在的主机ip地址
         9083是默认端口号

启动方式

方法1:
        hive --service metastore &
方法2:
        nohup hive --service metastore 2>&1 >/dev/null &  #信息送入黑洞。
         解析:2>&1 >/dev/null   意思就是把错误输出2重定向到标准输出1,也就是屏幕,标准输出进了“黑洞”,也就是标准输出进了黑洞,错误输出打印到屏幕。
              Linux系统预留可三个文件描述符:0、1和2,他们的意义如下所示:
                0——标准输入(stdin)-- System.in
                1——标准输出(stdout)--System.out
                2——标准错误(stderr) --System.err

测试是否成功

没有启动metastore 服务器之前,hive进入报错!
hive> show databases;
FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

启动之后,直接测试,发现可以使用。
hive> show databases;
OK
default
Time taken: 1.211 seconds, Fetched: 1 row(s)

3.3使用远程链接工具链接hive

常见的Hive连接工具有:IDEA、DBeaver、DataGrap,这里使用的是datagrap

请检查你的 metastore和hiveserver2是否启动
ps -ef|grep metastore
ps -ef|grep hiveserver2

假如没有启动:
nohup hive --service metastore 2>&1 >/dev/null &
nohup hive --service hiveserver2 2>&1 >/dev/null &

由于没办法看到4个session ID,等一下。

出现Succeeded,代表链接成功,就可以使用远程链接工具,链接虚拟机hive

四、Hive 的一些测试操作

4.1操作数据库

--创建数据库
hive> create database yhdb01;
--先判断数据库是否存在再创建
hive> create database if not exists yhdb02;
--判断数据库并在创建数据库时添加注释
hive> create database if not exists yhdb03 comment 'this is a database of yunhe';

hive默认元数据是放在mysql数据库中的,但是mysql数据库在刚开始的时候是不支持中文的,hive的元数据的数据库名字叫hive,创建该数据库的时候使用的字符集是latin 字符,不支持中文。
不信的话,创建一个带有中文解释的数据库:
create database if not exists yhdb02 comment "大数据"

创建hive数据库之后,在你的hdfs上会多一个文件夹:

使用web界面。在网址上输入本机域名:9870 就可以看到web界面

能看到这个界面后,就代表本地模式完全启动成功了


本文转载自: https://blog.csdn.net/KasarJ/article/details/142106142
版权归原作者 KasarJ 所有, 如有侵权,请联系我们删除。

“Hive的集群的搭建-内嵌模式-本地模式-远程链接”的评论:

还没有评论