本文环境如下:
操作系统:CentOS 6 32位
Hive版本:2.0.0
JDK版本:1.8.0_77 32位
Hadoop版本:2.6.4


1. 所需要的环境

Hive 2.0需要以下运行环境:
Java 1.7以上(强烈建议使用Java 1.8)
Hadoop 2.X

2. 下载、解压Hive安装包

Hive官网地址: http://hive.apache.org/
例如:

wget "http://mirrors.cnnic.cn/apache/hive/hive-2.0.0/apache-hive-2.0.0-bin.tar.gz"
tar -xzvf apache-hive-2.0.0-bin.tar.gz
mv apache-hive-2.0.0-bin /opt/hive-2.0.0
3. 配置环境变量(可选)

将hive-2.0.0/bin添加到path,以方便访问

vi /etc/profile
在末尾添加:

HIVE_HOME=/opt/hive-2.0.0
PATH=$PATH:$HIVE_HOME/bin
4. 启动单机模式

Hive和Hadoop一样,有3种启动模式,分别是单机模式,伪分布模式,分布模式。这里先来说一下单机模式的启动方式。

4.1 修改配置文件

cd /opt/hive-2.0.0/conf
vi hive-site.xml //也可以用hive-default.xml.template去改,不过这个文件中的配置项太多了
输入以下内容后保存:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/opt/hive-2.0.0/warehouse</value>
    <description>location of default database for the warehouse</description>
</property>
<property>
   <name>javax.jdo.option.ConnectionURL</name>
   <value>jdbc:derby:/opt/hive-2.0.0/metastore_db;create=true</value>
   <description>JDBC connect string for a JDBC metastore</description>
</property>
</configuration>
4.2 初始化数据库

schematool -initSchema -dbType derby
出现以下几行说明初始化成功:

Starting metastore schema initialization to 2.0.0
Initialization script hive-schema-2.0.0.derby.sql
Initialization script completed
schemaTool completed



4.3 启动程序

mkdir -p /opt/hive-2.0.0/warehouse       // 创建元数据存储文件夹
chmod a+rwx /opt/hive-2.0.0/warehouse    // 修改文件权限
hive
如果出现hive>提示符则说明启动成功

5. 常见错误

5.1 运行hive时出现

Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed, don't forget to include the option to auto-create the underlying database in your JDBC connection string (e.g. ?createDatabaseIfNotExist=true for mysql)
错误原因:  数据库没有初始化,请参照4.2

5.2 使用schematool初始化数据库时出现

Initialization script hive-schema-2.0.0.derby.sql
Error: FUNCTION 'NUCLEUS_ASCII' already exists. (state=X0Y68,code=30000)
org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !!


*** schemaTool failed ***
错误原因:数据库文件夹中已经存在一些文件,解决方法就是清空数据库文件夹(也就是前面配置的/opt/hive-2.0.0/metastore_db文件夹)