Hadoop系列之Hive(数据仓库)安装配置
1.在NameNode安装
cd /root/soft
tar zxvf apache-hive-0.13.1-bin.tar.gz
mv apache-hive-0.13.1-bin /usr/local/hadoop/hive
2. 配置环境变量(每个节点都需要增加)
打开/etc/profile
#添加以下内容:
export HIVE_HOME=/usr/local/hadoop/hive
export PATH=$HIVE_HOME/bin:$PATH
#环境变量生效
source /etc/profile
3.安装mysql数据库环境
请参照http://azhuang.blog.51cto.com/9176790/1551549
数据库安装成功后,一定要建立号权限及创建hive数据库。操作如下
grant all privileges on hive.* to [email protected]‘192.168.3.%‘ identified by ‘123‘; grant all privileges on *.* to [email protected]‘192.168.3.%‘ identified by ‘123‘;
create database hive character set latin1; #UTF-8编码hive会报错,所以需要修改编码为latin1
4. 配置Hive
cd /usr/local/hadoop/hive/conf/
cp hive-default.xml.template hive-site.xml
#vim hive-site.xml(修改<configuration>之间配置)
<configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.3.10:3306/hive?characterEncoding=latin1</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>123</value> </property> </configuration>
#以上四项分别是:
数据库连接,数据库驱动名,用户名,密码。
5.把mySQL的JDBC驱动包复制到Hive的lib目录下
cp /root/soft/mysql-connector-java-commercial-5.1.30-bin.jar /usr/local/hadoop/hive/lib/
6.复制Hive到所有DataNode节点
scp -r /usr/local/hadoop/hive [email protected]:/usr/local/hadoop/
scp -r /usr/local/hadoop/hive [email protected]:/usr/local/hadoop/
7.简单测试
//查看当前数据表
hive> show tables;
OK
Time taken: 1.069 seconds
//从本地文件系统中导入数据到Hive表
#创建student.txt测试文本,字段之间用tab键分割
[[email protected] soft]# cat /root/soft/student.txt
1 aa 10 121221
2 bb 20 0990
3 cc 30 120120
#创建student测试表
hive> create table student(id int, name string, age int, tel string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t‘ STORED AS TEXTFILE;
OK
Time taken: 0.043 seconds
#再次查看当前数据表及结构
hive> show tables;
OK
student
hive> desc student;
OK
id int
name string
age int
tel string
Time taken: 0.103 seconds, Fetched: 4 row(s)
#把/root/soft/student.txt本地记录导入到student表
hive> load data local inpath ‘/root/soft/student.txt‘ into table student;
Copying data from file:/root/soft/student.txt
Copying file: file:/root/soft/student.txt
Loading data to table default.student
Table default.student stats: [numFiles=1, numRows=0, totalSize=43, rawDataSize=0]
OK
Time taken: 0.376 seconds
#查看student表,如果有记录表示本地插入数据成功.
hive> select * from student;
OK
1 aa 10 121221
2 bb 20 0990
3 cc 30 120120
Time taken: 0.066 seconds, Fetched: 3 row(s)
//HDFS上导入数据到Hive表
#上传本地文件到hdfs
[[email protected] soft]# hdfs dfs -put /root/soft/student.txt /hive
[[email protected] soft]# hdfs dfs -cat /hive/student.txt
1 aa 10 121221
2 bb 20 0990
3 cc 30 120120
#从hdfs导入到hive数据
hive> load data inpath ‘/hive/student.txt‘ into table student;
Loading data to table default.student
Table default.student stats: [numFiles=2, numRows=0, totalSize=86, rawDataSize=0]
OK
Time taken: 1.389 seconds
hive> select * from student;
OK
1 aa 10 121221
2 bb 20 0990
3 cc 30 120120
1 aa 10 121221
2 bb 20 0990
3 cc 30 120120
Time taken: 0.049 seconds, Fetched: 6 row(s)