1. 问题背景
E-MapReduce中支持的Hive,会默认在Master节点的Mysql数据库中记录元数据信息。通常,用户会将数据存储在E-MapReduce的HDFS中,使用Hive处理HDFS中的数据。当集群释放时,节点的所有数据包括HDFS数据和Hive元数据都会被删除。前面我撰文说过,我们鼓励用户将数据存储在OSS中,这样可以实现存储和计算的分离,享受到OSS的弹性高可用。更多细节你可以看一下这篇文章。除此之外,我们可能有多个集群,很自然地需要多个集群共享一个Hive元数据仓。总结来说,我们希望在E-MapReduce集群外部创建Hive元数据仓。那么怎么才能做得到呢?了解阿里云生态产品的人会很自然地想到,是否可以用RDS来做Hive元数据仓?答案是肯定的,下面将演示如何在E-MapReduce上使用RDS创建Hive元数据仓。
2. Hive+RDS元数据仓
2.1 创建RDS实例
这里不赘述如何在RDS上创建数据库,如有需要请查看RDS相关文档。创建完数据库,我们需要以下这三个信息:
数据库帐号:hive
数据库密码:Hive001
数据库内网地址:rm-bp************735.mysql.rds.aliyuncs.com
2.2 创建Hive元数据库
创建Hive元数据库hivemeta,字符集选择 latin1,授权账户hive读写权限。
2.3 准备自定义配置文件
前面我已经说过,E-MapReduce默认使用Master节点的Mysql作为元数据仓。为了使用RDS来作为元数据仓,我们要修改默认的Hive配置文件。这里我们需要准备一个自定义的配置文件。关于自定义配置文件格式,我们可以看E-MapeReduce官方文档。下面是我的配置文件hive-site.json:
{
"configurations": [
{
"classification": "hive-site",
"properties": {
"javax.jdo.option.ConnectionUserName": "hive",
"javax.jdo.option.ConnectionPassword": "Hive001",
"javax.jdo.option.ConnectionURL": "jdbc:mysql://rm-bp************735.mysql.rds.aliyuncs.com:3306/hivemeta?createDatabaseIfNotExist=true",
"hive.metastore.uris": "thrift://localhost:9083"
}
}
]
}
将上面的文件上传到OSS任意目录,下一步会用到这个配置文件。
2.4 E-MapReduce上创建集群
这里不赘述集群创建过程,如有需要请查看E-MapReduce相关文档。需要注意的是,在第三步“软件配置”中,我们需要在“软件配置(可选)”这一项选择OSS中的hive-site.json文件。
2.5 配置RDS白名单
将上一步创建的集群机器内网IP配置到RDS白名单中。
- 集群详情页找到所有机器的内网IP,如下:
- 将机器内网IP加入到白名单中
2.6 登录集群使用hive
- 使用Master节点root账户和密码登录Master节点
Last login: Thu May 5 10:02:12 2016 from 42.120.74.97
Welcome to aliyun Elastic Compute Service!
[root@emr-header-1 ~]#
- 切换到hadoop账户
[root@emr-header-1 ~]# su hadoop
[hadoop@emr-header-1 root]$
- [可选]更新JDBC驱动包:我创建RDS实例时选择的是Mysql5.6,使用Hive时会碰到“com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1”的错误,这个错是因为jdbc驱动包版本过低导致的。下载最新的jdbc驱动包,替换/opt/apps/apache-hive-2.0.0-bin/lib目录下的“mysql-connector-java-3.1.14-bin.jar”为最新包(测试可用)。 这一步也可以放到创建集群的引导操作做 。
- 初始化Hive元数据库: /opt/apps/apache-hive-2.0.0-bin/bin/schematool -initSchema -dbType mysql
- 启动metastore服务: hive --service metastore
- 开始Hive查询
- 启动Hive
[hadoop@emr-header-1 ~]$ hive
Logging initialized using configuration in file:/etc/emr/hive-conf-1.0.1/hive-log4j.properties
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/apps/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/apps/hbase-1.1.1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/apps/apache-hive-1.0.1-bin/lib/hive-jdbc-1.0.1-standalone.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
hive>
- 创建表
hive> CREATE EXTERNAL TABLE emrusers (
> userid INT,
> movieid INT,
> rating INT,
> unixtime STRING )
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> LOCATION 'oss://y***********n:m************************4@xxx.oss-cn-hangzhou-internal.aliyuncs.com/tmp/hive';
- 统计条数
hive> select count(*) from emrusers;
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. tez, spark) or using Hive 1.X releases.
Query ID = hadoop_20160505102931_a476ce8d-7c4e-45f8-a953-4e8e37c91354
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1462363452366_0004, Tracking URL = http://xxxxxxxxxx:20888/proxy/application_1462363452366_0004/
Kill Command = /usr/lib/hadoop-current/bin/hadoop job -kill job_1462363452366_0004
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2016-05-05 10:35:06,061 Stage-1 map = 0%, reduce = 0%
2016-05-05 10:35:14,163 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 3.59 sec
2016-05-05 10:35:20,453 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 5.1 sec
MapReduce Total cumulative CPU time: 5 seconds 100 msec
Ended Job = job_1462363452366_0004
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 5.1 sec HDFS Read: 8168 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 5 seconds 100 msec
OK
100000
Time taken: 36.085 seconds, Fetched: 1 row(s)
3. 说明
- 2.6中的测试脚本和数据可以在开源Demo项目中找到。
- E-MapReduce将会很快支持Hue和Zeppline,到时候就可以进行交互式使用Hive和Spark了。