tonglin0325的个人主页

Hive学习笔记——安装和内部表CRUD

1.首先需要安装Hadoop和Hive

安装的时候参考 http://blog.csdn.net/jdplus/article/details/46493553

安装的版本是apache-hive-2.1.1-bin.tar.gz,解压到/usr/local目录下

然后在/etc/profile文件中添加

1
2
3
export HIVE_HOME=/usr/local/hive
export PATH=$PATH:$HIVE_HOME/bin

2.修改配置文件

在bin/hive-config.sh文件中添加

1
2
3
4
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_121
export HIVE_HOME=/usr/local/hive
export HADOOP_HOME=/usr/local/hadoop

添加hive-env.sh文件

1
2
cp hive-env.sh.template hive-env.sh

修改conf目录下的hive-site.xml的内容,该模式是本地模式,且使用JDBC连接元数据,本地模式可以查看Hive编程指南P24-27

实际数据还是存放在HDFS中,MySQL中存放的是元数据表,即schema信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive</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>root</value>
<description>Username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>XXXX</value>
<description>password to use against metastore database</description>
</property>
#如果不配置下面的部分会产生错误1.
<property>
<name>hive.exec.local.scratchdir</name>
<value>/usr/local/hive/tmp/local</value>
<description>Local scratch space for Hive jobs</description>
</property>
<property>
<name>hive.downloaded.resources.dir</name>
<value>/usr/local/hive/tmp/downloaded</value>
<description>Temporary local directory for added resources in the remote file system.</description>
</property>
<property>
<name>hive.querylog.location</name>
<value>/usr/local/hive/tmp/location</value>
<description>Location of Hive run time structured log file</description>
</property>
<property>
<name>hive.server2.logging.operation.log.location</name>
<value>/usr/local/hive/tmp/operation_logs</value>
<description>Top level directory where operation logs are stored if logging functionality is enabled</description>
</property>
</configuration>

注意/usr/local/hive/tmp/local , /usr/local/hive/tmp/downloaded , /usr/local/hive/tmp/location , /usr/local/hive/tmp/operation_logs 这四个文件夹需要自己创建

修改hive-log4j.properties

1
2
3
4
#cp hive-log4j.properties.template hive-log4j.properties
#vim hive-log4j.properties
hive.log.dir=自定义目录/log/

在HDFS上建立/tmp和/user/hive/warehouse目录,并赋予组用户写权限

注意这里面的/user/hive/warehouse是由hive-site.xml中的${hive.metastore.warehouse.dir}指定的数据仓库的目录

1
2
3
4
5
hadoop fs -mkdir       /tmp
hadoop fs -mkdir /user/hive/warehouse
hadoop fs -chmod g+w /tmp
hadoop fs -chmod g+w /user/hive/warehouse

 Mysql配置

1
2
3
4
5
6
7
8
#创建数据库
mysql> create database hive;
#赋予访问权限
mysql> grant all privileges on hive.* to root@localhost identified by '密码' with grant option;
mysql> flush privileges;
#将JDBC复制到Hive库目录用于java程序与mysql的连接
cp mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar /usr/local/apache-hive-1.1.0-bin/lib/

在hive的bin目录下,初始化元数据

1
2
./schematool -initSchema -dbType mysql

 

如果想使用hive的web界面的话,参考 http://blog.csdn.net/yyywyr/article/details/51416721

 

Hadoop和Hive的own和grp都是Hadoop

Hive的安装很简单,在清华镜像站下载Hive的二进制文件,然后解压到/usr/local目录下

修改own和grp就行

启动hive之前需要先启动Hadoop,启动后显示

1
2
hive> 

启动hive metastore

1
bin/hive --service metastore