zoukankan      html  css  js  c++  java
  • Hive 教程(一)-安装与配置解析

    安装就安装 ,不扯其他的

    hive 依赖

    在 hive 安装前必须具备如下条件

    1. 一个可连接的关系型数据库,如 Mysql,postgresql 等,用于存储元数据

    2. hadoop,并启动 hdfs

    3. HBase,非必须,但是如果不装,会有警告,不过不影响使用

    4. java,1.8 以上版本

    准备工作

    1. 下载安装包 

    https://mirrors.tuna.tsinghua.edu.cn/apache/hive/  清华镜像,下载速度快

    http://apache.org/dist/hive/  官网,下载速度慢

    选择含有 bin 的 tar 包,本文安装 hive-2.3.6

    2. 上传服务器

    最好上传到 hadoop 的 master 上,我是这么做的;无需所有节点都上传

    3. 解压,或许你可以重命名一下,方便操作

    环境变量

    export HIVE_HOME=/opt/SoftWare/Hive/hive‐2.3.2
    export PATH=$PATH:$HIVE_HOME/bin

    按照自己的路径修改即可

    此时 hive 已安装成功,执行 hive --version 可查看版本

    配置

    1. 首先修改 hive-env.sh 文件,本身不存在

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

    添加如下内容

    export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64
    export HADOOP_HOME=/usr/lib/hadoop-2.6.5
    export HIVE_HOME=/usr/lib/hive2.3.6
    export SPARK_HOME=/usr/lib/spark

    2. 修改  hive-site.xml 文件,本身也不存在

    cp hive-default.xml.template hive-site.xml

    注意hive- default.xml.template这个要复制二份,一个是hive-default.xml,另一个是hive-site.xml,其中 hive-site.xml为用户自定义配置,hive-default.xml为全局配置;

    hive启动时,hive-site.xml自定义配置会覆盖 hive-default.xml全局配置的相同配置项。

    非常不建议直接将hive-default.xml 直接复制为hive-site后进行修改,因为这样的话,我们根本不记得对那些配置项进行过修改,由于hive-site的作用是覆盖默认的配置,我们只需要将需要修改地方配置到hive-site.xml文件中即可。 【本文省略该过程】

    修改如下内容   【只是基础配置,此配置是首先把 hive 跑起来,如果应用在特殊场景,可能还需其他配置】

    主要是配置数据库的连接信息, Hive 默认使用 Derby 数据库作为元数据库,这里修改为 postgres

    <!‐‐数据库配置‐‐>
    <property>
        <name>javax.jdo.option.ConnectionURL</name><!‐‐数据库连接地址‐‐>
        <value>jdbc:mysql://192.168.100.103:3306/ccx_hive?createDatabaseIfNotExist=true</value><!‐‐使用MySQL存储元数据信息‐‐>
        <value>jdbc:postgresql://172.16.89.80:5432/db?ssl=true;databaseName=metastore_db;create=true</value><!‐‐使用postgres存储元数据信息‐‐>
        <value>jdbc:postgresql://172.16.89.80:5432/ball</value><!‐‐ball是数据库,最好事先建立,否则可能要其他配置,如create=true, 可以自己试试‐‐>
        <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.jdbc.Driver</value><!‐‐mysql‐‐>
        <value>org.postgresql.Driver</value><!‐‐postgres‐‐>
        <description>Driver class name for a JDBC metastore</description>
    </property>
            
    <property><!‐‐数据库用户名‐‐>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>u_ccx_hive</value>
    </property>
    
    <property><!‐‐数据库密码‐‐>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>123456</value>
    </property>
    
    
    <!‐‐hive 执行引擎‐‐>
    <property>
        <name>hive.execution.engine</name>
        <value>mr</value><!‐‐mapreduce 作为引擎‐‐>    
        <value>spark</value><!‐‐spark 作为引擎‐‐>
        <description>
          Expects one of [mr, tez, spark].
          Chooses execution engine. Options are: mr (Map reduce, default), tez, spark. While MR
          remains the default engine for historical reasons, it is itself a historical engine
          and is deprecated in Hive 2 line. It may be removed without further warning.
        </description>
    </property>
    
    
    <property>
        <name>hive.metastore.schema.verification</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>

    每个 value 值不能有空格

    上传 数据库驱动

    上传驱动 postgresql-9.2-1003.jdbc4.jar 到 hive 的 lib 目录下

    存储元数据到 数据库

    数据库需提前建立,这一步就是初始化该数据库

    bin/schematool -dbType mysql -initSchema
    bin/schematool -dbType postgres -initSchema

    此时可查看数据库,新建了一堆表

    启动 hive 服务

    以这种方式启动的 hive 可以用客户端直接访问,试试这个工具 DBVisualizer

    /bin/hive --service hiveserver2

    HiveServer2 很好的解决 HiveServer 存在的安全性、并发性等问题,HiveServer 目前已经不用了

    这个服务启动程序在${HIVE_HOME}/bin/hiveserver2里面,也可以这样启动

    hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001 #指定端口

    也可以在配置文件中指定端口号

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

    启动 hive 的 shell 客户端

    shell 客户端方便操作

    [root@master bin]# hive
    #输入show tables;显示以下信息,说明Hive已经启动
    hive> show tables;
    OK
    Time taken: 1.594 seconds

    Hive 检测

    Hive 装好了,但是 Hive 和 hadoop 什么关系呢?实操解释

    在 hive 中创建数据库

    hive> create database hive1;    # 创建数据库
    OK
    Time taken: 0.478 seconds
    hive> show databases;    # 显示数据库
    OK
    default
    hive1    # 创建成功
    Time taken: 0.132 seconds, Fetched: 2 row(s)

    问题来了,创建成功了,但是这个库在哪呢?跟 hadoop 什么关系?跟元数据什么关系?

    1. 首先我们看元数据,在 存储元数据 的数据库中有个表叫 DBS,很显然,存储数据库名

    我们看到了新建的 hive1 数据库

    2. 然后我们看 hdfs

    我们也看到了新建的 hive1 数据库

    这个路径在 hive-site 中可以找到对应配置

    在 hive 的数据库中创建数据表

    hive> use hive1;    # 切换到 hive1 数据库环境
    OK
    Time taken: 0.042 seconds
    hive> create table hive_01 (id int,name string);  # 创建数据表
    OK
    Time taken: 0.984 seconds
    hive> show tables;    #查询表
    OK
    hive_01   # 创建成功
    Time taken: 0.067 seconds, Fetched: 1 row(s)

    1. 同样我们看元数据,有一张 TBLS 表,很显然,存储表名

    2. 然后我们看 hdfs

    都找到了对应表

    结论:在 hive 中创建的数据库和表存储在 hdfs 中,元数据存储在 元数据库 中;

    hive-site.xml里的 hive.user.install.directory 参数,定义了HDFS的路径,默认/user

    异常记录

    1. 执行 schematool 命令时报错如下

    no dbType supplied原因是数据库没配好,且检查初始化 schematool 的命令,答案就是我上面的操作

    2. 执行 schematool 命令时报错如下

    org.apache.hadoop.hive.metastore.HiveMetaException: Failed to get schema version.
    Underlying cause: org.postgresql.util.PSQLException : The server does not support SSL.
    SQL Error code: 0
    Use --verbose for detailed stacktrace.
    *** schemaTool failed ***

    SSL 的问题,hive-site 的 javax.jdo.option.ConnectionURL 参数修改为 jdbc:postgresql://myhost/db?ssl=true;  true 改为 false 或者 删掉

    3. 启动 hive shell 报错如下

    Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException): Cannot create directory /tmp/hive/root/a71af7ab-060a-465e-91ba-124ba4b07e36. Nam
    e node is in safe mode.The reported blocks 200 has reached the threshold 0.9990 of total blocks 200. The number of live datanodes 1 has reached the minimum number 0. In safe mode extension. Safe mode will be tur
    ned off automatically in 15 seconds.

    原因:namenode 处于安全状态,关闭安全模式即可

    #关闭安全模式
     hadoop dfsadmin -safemode leave 

    4. 启动 hive shell 报错如下

    Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D

    解决方案如下:

    1.查看hive-site.xml配置,会看到配置值含有"system:java.io.tmpdir"的配置项

    2.新建文件夹/home/grid/hive-0.14.0-bin/iotmp,注意权限问题

    3.将含有"system:java.io.tmpdir"的配置项的值修改为如上地址

    5. 启动 hive shell 警告如下

    WARNING: 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.

    注意只是警告,因为 hive2.x 版本已经不支持 mr,解决方法就是换成 spark

    参考资料:

    https://blog.csdn.net/u013384984/article/details/80435531

    https://www.cnblogs.com/jiangnange/p/9460034.html  配置稍微多点

    https://blog.csdn.net/cjfeii/article/details/49423459

    https://www.cnblogs.com/dxxblog/p/8193967.html#top

    http://www.tianshouzhi.com/api/tutorials/hive/151

    https://blog.csdn.net/kongxx/article/details/79418977  hive postgres

    https://www.cnblogs.com/slymonkey/p/9967619.html    踩坑记录

    https://blog.csdn.net/pengjunlee/article/details/81737814   踩坑记录

    https://blog.csdn.net/lby0307/article/details/80309225  hive本地模式 schematool无法初始化mysql数据库

  • 相关阅读:
    SQL SERVER数据库优化相关资料
    京东面试题
    Jenkins部署资料
    [POI2005]Bank notes 【多重背包】
    [Usaco2004Feb]Cow Marathon 树的直径
    [ZJOI2008]骑士 基环树
    种树 反悔操作 【贪心】
    Poj2442 Sequence 贪心+堆优化
    洛谷div2【XR-4】歌唱比赛
    洛谷div2【XR-4】模拟赛
  • 原文地址:https://www.cnblogs.com/yanshw/p/11766343.html
Copyright © 2011-2022 走看看