一、mysql安装

在配置Hive之前一般都需要安装和配置MySQL,因为Hive为了能操作HDFS上的数据集,那么他需要知道数据的切分格式,如行列分隔符,存储类型,是否压缩,数据的存储地址等信息。
为了方便以后操作所以他需要将这些信息通过一张表存储起来,然后将这张表(元数据)存储到mysql中。为了啥存储到mysql里(实际是远程mysql),因为hive本身就是一个解释器,所以他不存储数据,可以参考上一篇文章[MySQL安装]进行安装(https://www.cnblogs.com/ruo1101/p/17470047.html "MySQL安装")

二、Hive的安装与启动

1.安装Hive

1.1 下载Hive

官方下载地址:https://archive.apache.org/dist/hive/

1.2 上传文件到hadoop集群

将准备好Hive文件安装包,将压缩包上传到到事先准备好的目录:/opt/software

再将压缩文件mysql-connector-java-5.1.48.jar上传到目录:/opt/software

1.3 将压缩包解压到文件目录

进入hive压缩文件所在目录:/opt/software,再执行以下代码解压hive文件到文件目录:/opt/module

tar -zxvf apache-hive-3.1.3-bin.tar.gz -C /opt/module

将目录(/opt/module/apache-hive-3.1.3-bin)重命名为(/opt/module/hive)

mv /opt/module/apache-hive-3.1.3-bin /opt/module/hive

再将压缩文件mysql-connector-java-5.1.48.jar解压到目录:/opt/module/hive/lib

cp mysql-connector-java-5.1.48.jar /opt/module/hive/lib
1.4 修改Hive配置

需要使用root权限

进入hive配置目录:/opt/module/hive/conf/

并且对hive配置文件(hive-deafule.xml.template)进行重命名

cp /opt/module/hive/conf/hive-deafule.xml.template hive-site.xml
或者
mv /

进入hive目录对文件hive-site.xml进行配置

或者直接创建一个配置文件hive-site.xml

hive-deafule.xml.template中的所需配置复制到hive-site.xml并针对修改其属性

修改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>

  <!-- WARNING!!! This file is auto generated for documentation purposes ONLY! -->
  <!-- WARNING!!! Any changes you make to this file will be ignored by Hive.   -->
  <!-- WARNING!!! You must make your changes in hive-site.xml instead.         -->
  <!-- Hive Execution Parameters -->
  <property>
  <!-- 链接的URL  -->
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://hadoop102:3306/metastore?useSSL=false</value>
  <description>
      JDBC connect string for a JDBC metastore.
      To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
      For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
    </description>
  </property>

  <!-- 使用的驱动 -->
  <property>
  <name>javax.jdo.option.ConnectionDriverName</name>
  <value>com.mysql.cj.jdbc.Driver</value>
  <description>Driver class name for a JDBC metastore</description>
  </property>

  <!-- 用户名 -->
  <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>hive.metastore.schema.varification</name>
  <value>false</value>
  <description>
      Enforce metastore schema version consistency.
      True: Verify that version information stored in is compatible with one from Hive jars.  Also disable automatic
            schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures
            proper metastore schema migration. (Default)
      False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
    </description>
  </property>

  <!-- 元数据存储权限 -->
  <property>
    <name>hive.metastore.schema.verification.record.version</name>
    <value>false</value>
    <description>
      When true the current MS version is recorded in the VERSION table. If this is disabled and verification is
       enabled the MS will be unusable.
    </description>
  </property>

  <!-- Hive在hdfs中的工作目录 -->
  <property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/user/hive/warehouse</value>
    <description>location of default database for the warehouse</description>
  </property>
</configuration>
1.5 修改环境变量

需要root权限

进入环境变量目录文件:/etc/profile.d/my_env.sh

vim /etc/profile.d/my_env.sh

追加写入以下内容:

#Hive
export HIVE_HOME=/opt/module/hive
export PATH=$PAH:$HIVE_HOME/bin

刷新环境变量

source /etc/profile
1.6 解决jar冲突问题

Hive由日志slf4j的配置,但是我们的yarn自己也有,两者会冲突。

需要root权限

先刷新环境变量,再执行以下代码

mv $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.jar $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.jar.bak

解决版本对应问题

mv $HIVE_HOME/lib/guava-19.0.jar $HIVE_HOME/lib/guava-19.0.jar.bak
cp /opt/module/hadoop-3.1.3/share/hadoop/common/lib/guava-27.0-jar /opt/module/hive/lib/
1.7 初始化Hive元数据仓库
schematool -initSchema -dbType mysql -verbose

保证数据库已创建,数据名与hive配置中的数据库保持一致。

mysql所在集群机器,启动mysql【hadoop102】,mysql与yarn同在一台机器。

检查mysql数据库是否存在,是否初始化过,如果已经初始化过则先将数据库删除,重新初始化。

2. 启动Hive

2.1 启动hadoop集群

Hive的启动依赖于hadoop的启动,必须先启动hadoop(dfs以及yarn).

2.2 启动hive【hadoop10】
hive # 启动hive命令

一般报错即为dfs安全模式的原因,关闭安全模式即可: hdfs dfsadmin -safemode leave

用root用户启动报错(权限有问题)如下:

chown: changing ownership of '/tmp': Permission denied. user=root is not the owner of inode=/tmp

需用使用 export HADOOP_USER_NAME=普通用户

然后使用hadoop fs -chmod -R 777 /tmp

再次启动hive即可

2.3 验证安装环境
show databases

3. Hive的常用基本操作

3.1 查看当前存在数据库

show databases;

3.2 选择一个库

use 数据库名;

3.3 查看当前库的表

show tables;

3.4 查看一个表

desc 表名

3.5 显示表头,数据库名

在hive中设置参数,临时生效,退出hive后失效

# 显示数据库名,该参数默认值为false

set hive.cli.print.current.db=true;

# 显示列名,该设置会显示表名.列名,默认值为false

set hive.cli.print.header=true;

# 不显示表名,默认值为true

set hive.resultset.use.unique.column.names=false;

更改hive-site.xml使配置永久生效

<property>
    <name>hive.cli.print.header</name>
    <value>true</value>
    <description>是否打印表头,默认值为false,即不打印</description>
</property>

<property>
    <name>hive.cli.print.current.db</name>
    <value>true</value>
    <description>是否显示数据库名,默认值为false,即不显示</description>
</property>

<property>
    <name>hive.resultset.use.unique.column.names</name>
    <value>false</value>
    <description>表头中是否显示表名,默认值为true,即显示</description>
</property>

4 案列操作

新建一个表

create table test (id int,name string);

插入一条数据

insert into test values(1,'RuoZoe');

并查询

select * from test;

有结果即可

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/ruo1101/p/17575376.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!