安装Apache Hive的步骤

概要 – 概述

Apache Hadoop和Hive的安装步骤如下,将分别在不同的文章中进行说明。

目录

    1. 安装Apache Hadoop的步骤

 

    1. 安装Apache Hive的步骤

 

    尝试在Hadoop上进行WordCount任务

Apache Hive的安装步骤

以下是Apache Hadoop的安装步骤的继续。

Hive 是用于方便地使用 Hadoop 数据的软件,它支持类似 SQL 的 HiveQL 语法来操作数据。
以下是在伪独立模式(本地模式)下搭建的步骤。

环境

    • CentOS 7.1

 

    • Hadoop 2.8.0

 

    • MariaDB 5.5.52

 

    • java 1.8.0

 

    • Hive 2.3.0

 

    HBase 1.3.1

在此页面中,将进行粗体字的安装。

设置Apache Hive

请从以下页面下载并解压Apache Hive。

Apache下载镜像-蜂巢

$ wget http://ftp.jaist.ac.jp/pub/apache/hive/hive-2.3.0/apache-hive-2.3.0-bin.tar.gz
$ tar zxfv apache-hive-2.3.0-bin.tar.gz

将HIVE_HOME设置为环境变量。

$ echo "export HIVE_HOME=/home/`whoami`/apache-hive-2.3.0-bin" >> ~/.bashrc
$ source ~/.bashrc

HBase的安装

这是一个建立在Hadoop上的NoSQL数据库。

如果使用apache-hive-2.1.1、2.3.0,没有安装HBase,执行hive命令时将会出现以下错误。值得一提的是,在1.2.2版本中将不会出现此错误。因此,如果要安装2.1.1以上的版本,也需要安装HBase。

错误内容

$ $HIVE_HOME/bin/hive -H

which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin)

请从以下网页下载并解压Apache HBase:
http://ftp.jaist.ac.jp/pub/apache/hbase/

$ wget http://ftp.jaist.ac.jp/pub/apache/hbase/1.3.1/hbase-1.3.1-bin.tar.gz
$ tar zxfv hbase-1.3.1-bin.tar.gz

设置环境变量

将HBASE_HOME设为环境变量。

$ echo 'export HBASE_HOME=/home/`whoami`/hbase-1.3.1' >> ~/.bashrc
$ echo 'export PATH="$HBASE_HOME/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc

请确认安装

$ $HBASE_HOME/bin/hbase version

修改hbase-site.xml文件

编辑$HBASE_HOME/conf/hbase-site.xml文件,指定HBase和ZooKeeper数据写入的目录。由于是伪分布模式,所以指定本地目录。无需提前创建目录。
如果不指定目录,则默认在/tmp/hbase-root下创建。
参考:http://www.task-notes.com/entry/20160211/1455179303

$ vi $HBASE_HOME/conf/hbase-site.xml
<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>file:///opt/hbase</value>
  </property>
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/opt/zookeeper</value>
  </property>
</configuration>

启动HBase。

$ $HBASE_HOME/bin/start-hbase.sh

确认能否进入HBase Shell。

$ $HBASE_HOME/bin/hbase shell

hbase(main):001:0> exit

类路径中包含了多个SLF4J绑定。

如果使用apache-hive-2.1.1、2.3.0,在执行hive命令时会出现以下错误。在1.2.2版本中没有这个错误。
因此,如果要使用2.1.1以上的版本,需要进行以下处理。

错误内容

(chinese translation of “エラー内容”)

$ $HIVE_HOME/bin/hive -H

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/vagrant/apache-hive-2.3.0-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/vagrant/hadoop-2.8.0/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]

类路径中包含多个SLF4J绑定。

参考来源:https://stackoverflow.com/questions/27050820/running-hive-0-12-with-error-of-slf4j。

请参考以下链接:https://stackoverflow.com/questions/27050820/running-hive-0-12-with-error-of-slf4j。

你需要删除Hadoop和Hive之间的这些jar文件绑定。

rm lib/hive-jdbc-2.0.0-standalone.jar
rm lib/log4j-slf4j-impl-2.4.1.jar

因此,会搜寻相关的文件。

$ find ./ -name '*hive-jdbc-*-standalone.jar'
./apache-hive-2.3.0-bin/jdbc/hive-jdbc-2.3.0-standalone.jar

$ find ./ -name '*log4j-slf4j-impl-*.jar'
./apache-hive-2.3.0-bin/lib/log4j-slf4j-impl-2.6.2.jar

删除这两个文件。

$ rm -f ./apache-hive-2.3.0-bin/jdbc/hive-jdbc-2.3.0-standalone.jar
$ rm -f ./apache-hive-2.3.0-bin/lib/log4j-slf4j-impl-2.6.2.jar

通过执行这个Hive命令,错误消失了!

$ $HIVE_HOME/bin/hive -H
usage: hive
 -d,--define <key=value>          Variable substitution to apply to Hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable substitution to apply to Hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the
                                  console)

Hive的安装现已完成。

JDBC驅動程式的配置

从mysql的网站上下载并解压jdbc驱动程序。
下载链接:http://dev.mysql.com/downloads/connector/j/。

$ wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.42.tar.gz
$ tar zxfv mysql-connector-java-5.1.42.tar.gz

将mysql-connector-java-5.1.42-bin.jar文件放置在$HIVE_HOME/lib/目录下。

$ cp mysql-connector-java-5.1.42/mysql-connector-java-5.1.42-bin.jar $HIVE_HOME/lib/ 

元数据存储设定

将hive-site.xml文件按照以下方式进行修改。

$ vi $HIVE_HOME/conf/hive-site.xml

请将※DB密码※设置为在MariaDB中设置的hive’@’localhost的密码。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value><![CDATA[jdbc:mysql://localhost/hive_metastore?autoReconnect=true&useSSL=false]]></value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>hive</value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>※DBパスワード※</value>
  </property>
  <property>
    <name>datanucleus.fixedDatastore</name>
    <value>false</value>
  </property>
  <property>
      <name>hive.exec.local.scratchdir</name>
      <value>/tmp/hive</value>
  </property>
  <property>
    <name>hive.downloaded.resources.dir</name>
    <value>/tmp/hive</value>
  </property>
  <property>
    <name>hive.querylog.location</name>
    <value>/tmp/hive</value>
  </property>
  <property>
    <name>hive.server2.enable.doAs</name>
    <value>false</value>
  </property>
</configuration>

初始化metastore。

$ $HIVE_HOME/bin/schematool -dbType mysql -initSchema

通过Hive命令进行连接

我将检查Hive是否正常运行。如果提示出现,那就没问题。

$ $HIVE_HOME/bin/hive

Logging initialized using configuration in jar:file:/home/vagrant/apache-hive-2.3.0-bin/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> exit

使用beeline连接到hiveserver2并启动。

启动HiveServer2。

$ $HIVE_HOME/bin/hiveserver2
2017-07-19 06:51:11: Starting HiveServer2

在hive服务2启动的情况下,通过beeline确认可以连接。

$ $HIVE_HOME/bin/beeline -u jdbc:hive2://localhost:10000/default -n `whoami`
Connecting to jdbc:hive2://localhost:10000/default
Connected to: Apache Hive (version 2.3.0)
Driver: Hive JDBC (version 2.3.0)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 2.3.0 by Apache Hive
0: jdbc:hive2://localhost:10000/default> !exit

验证Hive的运行

我們將使用HiveQL實際上儲存資料。

准备样本数据

我准备了如下的CSV文件:sample.csv。

りんご,100
バナナ,340
みかん,200
いちじく,400

创建数据库和表

我会创建一个名为testdb的数据库,并在其中创建一个名为items的表。

hive> create database testdb;
hive> use testdb;
hive> create table items(name string,price int) row format delimited fields terminated by ',';
hive> desc items;
OK
name                    string
price                   int
Time taken: 0.173 seconds, Fetched: 2 row(s)

将CSV数据插入表格。

hive> load data local inpath "/home/vagrant/sample.csv" into table items;
Loading data to table testdb.items
OK
Time taken: 1.585 seconds

我要检查一下桌子里面的东西。

hive> select * from items;
OK
りんご   100
バナナ   340
みかん   200
いちじく    400
Time taken: 0.339 seconds, Fetched: 4 row(s)

将处理结果输出到文件中

首先,您需要使用insert语句指定输出位置,然后执行select语句将表中内容输出到文件中。

hive> insert overwrite directory '/output/'
    > select * from sales;

在出力目录下创建了一个名为output的文件夹,并在其中生成了一个名为000000_0的文件。

$ $HADOOP_HOME/bin/hadoop fs -ls /output
Found 1 items
-rwxr-xr-x   1 vagrant supergroup         59 2017-07-28 03:26 /output/000000_0

我会检查文件的内容。

$ $HADOOP_HOME/bin/hadoop fs -cat /output/000000_0
りんご100
バナナ340
みかん200
いちじく400

更新,删除

HiveQL中不存在Update和Delete操作。

请参考

    • Hadoop(ハドゥープ)とは?:ビッグデータ処理を支える「基盤」

 

    • Hiveとは?:Hadoop上で稼動するデータベースマネージメントシステム(DBMS)

 

    • CentOS7 Apache Hadoop インストール

 

    • CentOSでHadoopとHiveを試してみる(2015年秋編)

 

    • CentOS7への擬似分散モードでのhadoop及び、hiveセットアップ手順

 

    • Hadoopのインストールと実行方法(スタンドアロンモード)

 

    • Hiveのインストールと実行方法

 

    HBaseのインストールと実行方法(スタンドアロンモード)
bannerAds