Hive 2.1 安装

安装Hive2.1
1. 准备工作:安装JDK、Hadoop

2. 下载并解压Hive,设置环境变量 HIVE_HOME、PATH

3. 设置Hadoop环境变量

./hadoop fs -mkdir /tmp
./hadoop fs -mkdir /usr/hive/warehouse
./hadoop fs -chmod g+w /tmp
./hadoop fs -chmod g+w /usr/hive/warehouse

4. 修改Hive的配置文件

conf/hive-default.xml.template -> hive-site.xml
conf/hive-log4j.properties.template -> hive-log4j.properties
conf/hive-exec-log4j.properties.template -> hive-exec-log4j.properties

5. 修改 hive-site.xml

替换${system:java.io.tmpdir} 和 ${system:user.name}

:%s@\${system:java.io.tmpdir}@/home/c3/hive_tmp@g    
:%s@\${system:user.name}@/c3@g

 启动Hive时报错

Caused by: MetaException(message: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))

这是由于没有初始化Hive元数据的数据库,默认情况下,Hive的元数据保存在了内嵌的derby数据库里

schematool -initSchema -dbType derby

元数据放入MySQL

1、将mysql-connector-java-5.1.30-bin.jar 放入 $HIVE_HOME/lib下
2、修改 $HIVE_HOME/conf/hive-site.xml里的 数据库连接串、驱动、用户和密码

<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://10.1.195.50:3306/hivedb?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</value>
    <description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
</property>
<property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>umobile</value>
    <description>username to use against metastore database</description>
</property>
<property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>umobile</value>
    <description>password to use against metastore database</description>
</property>

3、初始化Hive在mysql里的脚本 $HIVE_HOME/scripts

schematool -initSchema -dbType mysql

 测试Hive

在Hive里创建的表和数据,都保存在了Hadoop里的hdfs上面,hive-site.xml里的 hive.user.install.directory 参数,定义了HDFS的路径,默认/user

hive 回车

hive> create database hello;

$ hadoop fs -ls /usr/hive/
warehouse
Found 1 items
drwxrwxr-x   - c3 supergroup          0 2016-06-30 17:18 /user/hive/warehouse/hello.db



创建库表并插入数据

create database test;
use test;
create table test_table (id int,name string,no int) row format delimited fields terminated by ',' stored as textfile
select * from test_table;
insert into test_table values (1, 'test', 1);

报如下错误:

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.

我太激进了,不该用这么新的Hive版本。。

将Hive的版本降到1.2再装

schematool -initSchema -dbType mysql 报错:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected

解决方法:

将hive下的新版本jline的JAR包拷贝到hadoop下
hive/lib/jline-2.12.jar 拷贝到 hadoop/share/hadoop/yarn/lib/
hadoop下的jline-0.9.94.jar 重命名为 jline-0.9.94.jar.bak

hive> CREATE TABLE student(id STRING, name String) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

bash> echo 1,wang >> student.txt
bash> echo 2,wang >> student.txt
bash> echo 3,wang >> student.txt

hive> load data local inpath '/home/c3/student.txt' into table student; 要用绝对路径
hive> select * from student;

注意:不能使用insert into values 语句

以hiveserver方式启动hive

bash> hive --service hiveserver

报错:Exception in thread "main" java.lang.ClassNotFoundException: org.apache.hadoop.hive.service.HiveServer

HiveServer本身存在很多问题(比如:安全性、并发性等),针对这些问题,Hive0.11.0版本提供了一个全新的服务:HiveServer2,这个很好的解决HiveServer存在的安全性、并发性等问题。这个服务启动程序在${HIVE_HOME}/bin/hiveserver2里面,你可以通过下面的方式来启动HiveServer2服务。

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

启动成功之后,就可以用DBVisualizer访问Hive了,就像访问MySQL

Tools > DriverManager... > 新建Hive

 

创建好Hive驱动,就可以创建Hive库的链接了

 

Database Type: Generic

Driver:上面创建的驱动

userid/password:随意输入

 

数据准备,先在HDFS上准备文本文件,逗号分割,并上传到/1目录,然后在Hive里创建table,表名和文件名要相同,表的存储路径也是目录/1

bash> vim table_test

d1,user1,1000
d1,user2,2000
d1,user3,3000
d2,user4,4000
d2,user5,5000

bash> hadoop fs -mkdir /1
bash> hadoop fs -put table_test /1

hive> CREATE EXTERNAL TABLE table_test (
dept STRING,
userid string,
sal INT
) ROW FORMAT DELIMITED 
FIELDS TERMINATED BY ',' 
stored as textfile location '/1';

hive> select * from table_test;

另外,SparkSQL也可以作为 JDBC Server,这种方式与上面的Hive作为server的区别在于,SparkSQL使用Spark引擎来执行SQL,而Hive使用MR来执行SQL。

1、hive-site.xml拷贝将hive-site.xml拷贝到¥SPARK_HOME/conf下

2、需要在$SPARK_HOME/conf/spark-env.sh中的SPARK_CLASSPATH添加jdbc驱动的jar包

export SPARK_CLASSPATH=.:/home/Hadoop/software/mysql-connector-java-5.1.27-bin.jar

3、启动:./start-thriftserver.sh  --hiveconf hive.server2.thrift.port=10001 --hiveconf hive.server2.thrift.bind.host=rti9

4、查看日志:tail -f  /home/c3/apps/spark-1.6.1-bin-hadoop2.6/logs/spark-c3-org.apache.spark.sql.hive.thriftserver.HiveThriftServer2-1-rti9.out

 

posted on 2016-07-01 15:55  码虫虫  阅读(9122)  评论(2编辑  收藏  举报