Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1067783
  • 博文数量: 83
  • 博客积分: 159
  • 博客等级: 上尉
  • 技术积分: 2221
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 17:08
个人简介

……致我那曾经苦逼的岁月……

文章分类
文章存档

2018年(1)

2017年(7)

2016年(13)

2014年(1)

2013年(12)

2012年(27)

2011年(22)

分类: 服务器与存储

2012-07-20 17:02:12

本章节主要介绍两部分内容:
1、hive搭建元数据存放于本地文件系统。
2、hive搭建元数据存放于第三方开源数据库mysql。
两者的定义大致如下:
ingle User Mode:此模式连接到一个In-memory的数据库Derby,一般用于Unit Test。
Multi User Mode:通过网络连接到一个数据库中,是最经常使用到的模式。
区别在于如果你用本地文件系统存放hive元数据的话,那么将不支持多用户并发,简单来讲就是只允许一个用户登录hive查询,另外你再开启一个终端登录hive查询会报错,就如下图所示:
hive> show tables;
FAILED: Error in metadata: javax.jdo.JDOFatalDataStoreException: Failed to start database 'metastore_db', see the next exception for details.
NestedThrowables:
java.sql.SQLException: Failed to start database 'metastore_db', see the next exception for details.
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
下面我们开始搭建本地文件系统存放hive的元数据,步骤很简单!
说一下我的hadoop和hbase版本分别是1.0.3和0.92.1。
我要部署的hive版本是0.8.1-bin这个版本。
1、从网站下载适合你版本的hive。然后上传到服务器上。我这里就默认放到/root下了。
2、解压hive-0.8.1-bin.tar.gz
[root@hadoop1 ~]# tar zxvf hive-0.8.1-bin.tar.gz
3、在.bash_profile文件里添加hive环境变量
export HIVE_HOME=/root/hive-0.8.1-bin
export PATH=$HIVE_HOME/bin:$PATH
4、将hbase的jar包拷贝到hive的lib目录下,并且将hive原本的jar包移出,拷贝hbase-site.xml文件到hive的conf目录下
[root@hadoop1 ~]# cp /root/hbase/hbase-0.92.1.jar /root/hive-0.8.1-bin/lib/
[root@hadoop1 ~]# cp /root/hbase/hbase-0.92.1-tests.jar /root/hive-0.8.1-bin/lib/
[root@hadoop1 ~]# mv /root/hive-0.8.1-bin/lib/hbase-0.89.0-SNAPSHOT.jar /home
[root@hadoop1 ~]# mv /root/hive-0.8.1-bin/lib/hbase-0.89.0-SNAPSHOT-tests.jar /home
[root@hadoop1 ~]# cp /root/hbase/conf/hbase-site.xml /root/hive-0.8.1-bin/conf
5、将默认的hive-default.xml.template拷贝一份为hive-site.xml文件
[root@hadoop1 ~]# cp hive-0.8.1-bin/conf/hive-default.xml.template hive-0.8.1-bin/conf/hive-site.xml
6、启动hive,什么都不用配置哦!很简单的!
[root@hadoop1 ~]# hive
WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all the log4j.properties files.
Logging initialized using configuration in jar:file:/root/hive-0.8.1-bin/lib/hive-common-0.8.1.jar!/hive-log4j.properties
Hive history file=/tmp/root/hive_job_log_root_201207200921_612921620.txt
hive> show tables;
OK
Time taken: 4.656 seconds
7、测试hive能否正常工作?我们现在hbase中新建一张表,然后插入点数据,最后在hive中关联上hbase里面的表,看看数据是否一致哈!
hbase(main):005:0> create 'xyz','cf1'
0 row(s) in 1.0680 seconds
hbase(main):006:0> put 'xyz','10000','cf1:val','China'
0 row(s) in 0.1260 seconds
hbase(main):007:0> scan 'xyz'
ROW                  COLUMN CELL                                              
10000                column=cf1:val, timestamp=1342748087354, value=China     
1 row(s) in 0.0310 seconds
在hive的shell里执行下面命令来关联hbase中的表
hive> CREATE EXTERNAL TABLE hbase_table_1(key string, value string)
    > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
    > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
    > TBLPROPERTIES ("hbase.table.name" = "xyz");
OK
Time taken: 1.186 seconds
hive> select * from hbase_table_1;
OK
10000   China
Time taken: 0.455 seconds
这样基本就测试可以了哈!
第二部分我会写怎么搭建hive,将它的元数据存放于本机的mysql中。配置就复杂了很多了。
1、解压hive-0.8.1-bin.tar.gz
tar zxvf hive-0.8.1-bin.tar.gz
2、在.bash_profile文件里添加hive环境变量并且让其生效
export HIVE_HOME=/root/hive-0.8.1-bin
export PATH=$HIVE_HOME/bin:$PATH
[root@hadoop1 ~]# source .bash_profile
3、把hbase的jar包copy到hive中
[root@hadoop1 ~]# cp -v hbase/hbase-0.92.1.jar hive-0.8.1-bin/lib/
`hbase/hbase-0.92.1.jar' -> `hive-0.8.1-bin/lib/hbase-0.92.1.jar'
[root@hadoop1 ~]# cp -v hbase/hbase-0.92.1-tests.jar hive-0.8.1-bin/lib/
`hbase/hbase-0.92.1-tests.jar' -> `hive-0.8.1-bin/lib/hbase-0.92.1-tests.jar'
[root@hadoop1 ~]# cp -v hbase/conf/hbase-site.xml hive-0.8.1-bin/conf
`hbase/conf/hbase-site.xml' -> `hive-0.8.1-bin/conf/hbase-site.xml'
[root@hadoop1 ~]# mv hive-0.8.1-bin/lib/hbase-0.89.0-SNAPSHOT.jar /home
[root@hadoop1 ~]# mv hive-0.8.1-bin/lib/hbase-0.89.0-SNAPSHOT-tests.jar /home/
4、修改hive环境hive-config.sh,增加hadoop的安装路径,复制配置文件:
export HADOOP_HOME=/root/hadoop
export HIVE_HOME=/root/hive-0.8.1-bin
[root@hadoop1 ~]# cp hive-0.8.1-bin/conf/hive-default.xml.template hive-0.8.1-bin/conf/hive-site.xml
5、安装mysql-5.5.24,这里相信很多朋友就很熟悉了
安装mysql依赖包,我这里安装系统是把所有的包都安装了,所有这步骤省略!

yum -y install bison gcc gcc-c autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel*

mysql需要cmake编译,所以先安装cmake

[root@hadoop1 ~]# wget
[root@hadoop1 ~]# tar zxvf cmake-2.8.6.tar.gz
[root@hadoop1 ~]# cd  cmake-2.8.6
[root@hadoop1 ~]# gmake
[root@hadoop1 ~]# make install
下载mysql的安装包,解压并安装
[root@hadoop1 ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.24.tar.gz/from/http://mysql.ntu.edu.tw/
建立mysql用户和组,并且建立mysql存放数据的目录/mysql
[root@hadoop1 ~]# /usr/sbin/groupadd mysql
[root@hadoop1 ~]# /usr/sbin/useradd -g mysql mysql
[root@hadoop1 ~]# mkdir /mysql
[root@hadoop1 ~]# chown -R mysql:mysql /mysql
安装mysql
[root@hadoop1 ~]# tar zxvf mysql-5.5.24.tar.gz
[root@hadoop1 ~]# cd mysql-5.5.24
[root@hadoop1 ~]# cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/mysql -DSYSCONFDIR=/etc/
[root@hadoop1 ~]# make && make install
拷贝mysql默认配置文件
[root@hadoop1 ~]# cp ./support-files/my-medium.cnf /etc/my.cnf
修改my.cnf文件
在[mysqld]段增加:
datadir = /mysql
wait-timeout = 30
max_connections = 512
default-storage-engine = MyISAM
在[mysqld]段修改:
max_allowed_packet = 16M
生产授权表:
[root@hadoop1 ~]# /opt/mysql/scripts/mysql_install_db --user=mysql
开启mysql:
[root@hadoop1 ~]# /opt/mysql/bin/mysqld_safe &
更改密码:
[root@hadoop1 ~]# /opt/mysql/bin/mysqladmin -u root password 123456
测试连接mysql及授权用户登录:
[root@hadoop1 ~]# /opt/mysql/bin/mysql -u root -p123456
mysql> create user
identified by 'hivepasswd';
mysql> grant all privileges on *.* to with grant option;
6、修改hive的配置文件hive-site.xml
修改为如下所示:
[root@hadoop1 ~]# more hive-0.8.1-bin/conf/hive-site.xml


    
        hive.metastore.local    
        true    
        controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM 
 
    
        javax.jdo.option.ConnectionURL    
        jdbc:mysql://192.168.3.65:3306/hive?createDatabaseIfNotExist=true    
        192.168.3.65 JDBC connect string for a JDBC metastore
 
    
        javax.jdo.option.ConnectionDriverName    
        com.mysql.jdbc.Driver    
        Driver class name for a JDBC metastore 
 
    
        javax.jdo.option.ConnectionUserName    
        hive    
        username to use against metastore database 
 
    
        javax.jdo.option.ConnectionPassword    
        hivepasswd    
        password to use against metastore database 
 
7、添加jdbc的jar包:

wget

tar   vxf  mysql-connector-java-5.1.18.tar.gz

chwon -R hadoop mysql-connector-java-5.1.18/

cp -v  mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar  hive/lib/

8、开启hive并测试
[root@hadoop1 ~]# hive
WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all the log4j.properties files.
Logging initialized using configuration in jar:file:/root/hive-0.8.1-bin/lib/hive-common-0.8.1.jar!/hive-log4j.properties
Hive history file=/tmp/root/hive_job_log_root_201207201723_1051702106.txt
hive> show tables;
OK
Time taken: 3.301 seconds
还是关联hbase中的表测试:
hbase(main):002:0> create 'xyz','cf1'
0 row(s) in 1.1120 seconds
hbase(main):003:0> put 'xyz','10000','cf1:val','China'
0 row(s) in 0.2080 seconds
hbase(main):004:0> put 'xyz','11000','cf1:val','Japanese'
0 row(s) in 0.0320 seconds
hbase(main):005:0> scan 'xyz'
ROW                   COLUMN+CELL                                              
 10000                column=cf1:val, timestamp=1342776914297, value=China     
 11000                column=cf1:val, timestamp=1342776933201, value=Japanese  
2 row(s) in 0.0340 seconds
hbase(main):006:0>
在hive中关联此表:
hive> CREATE EXTERNAL TABLE hbase_table_1(key string, value string)
    > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
    > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
    > TBLPROPERTIES ("hbase.table.name" = "xyz");
OK
Time taken: 1.155 seconds
hive> select * from hbase_table_1;
OK
10000   China
11000   Japanese
Time taken: 0.379 seconds
这样基本就OK了哈!顺便看看mysql中的hive库中存放哪些表?

mysql> use hive;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_hive  |
+-----------------+
| BUCKETING_COLS  |
| CDS             |
| COLUMNS_V2      |
| DATABASE_PARAMS |
| DBS             |
| PARTITION_KEYS  |
| SDS             |
| SD_PARAMS       |
| SEQUENCE_TABLE  |
| SERDES          |
| SERDE_PARAMS    |
| SORT_COLS       |
| TABLE_PARAMS    |
| TBLS            |
+-----------------+
14 rows in set (0.00 sec)


 
 
 
阅读(4344) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~