有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv、uv 数据,然后为了实时查询的需求,或者一些 OLAP 的需求,我们需要
mapreduce 与 mySQL 进行数据的交互,而这些特性正是 HBase 或者 hive 目前亟待改进的地方。
好了言归正传,简单的说说背景、原理以及需要注意的地方:
1、为了方便 MapReduce
直接访问关系型数据库(Mysql,Oracle),Hadoop提供了DBInputFormat和DBOutputFormat两个类。通过
DBInputFormat类把数据库表数据读入到HDFS,根据DBOutputFormat类把MapReduce产生的结果集导入到数据库表中。
2、由于0.20版本对DBInputFormat和DBOutputFormat支持不是很好,该例用了0.19版本来说明这两个类的用法。
至少在我的 0.20.203 中的org.apache.hadoop.mapreduce.lib 下是没见到 db 包,所以本文也是以老版的 API 来为例说明的。
3、运行MapReduce时候报错:Java.io.IOException: com.mysql.jdbc.Driver,一般是由于程序找不到mysql驱动包。解决方法是让每个tasktracker运行MapReduce程序时都可以找到该驱动包。
添加包有两种方式:
(1)在每个节点下的${HADOOP_HOME}/lib下添加该包。重启集群,一般是比较原始的方法。
(2)a)把包传到集群上: hadoop fs -put mysql-connector-java-5.1.0- bin.jar /hdfsPath/
b)在mr程序提交job前,添加语句:DistributedCache.addFileToClassPath(new Path(“/hdfsPath/mysql- connector-java- 5.1.0-bin.jar”), conf);
(3)虽然API用的是0.19的,但是使用0.20的API一样可用,只是会提示方法已过时而已。
4、测试数据:
5、代码:
6、结果:
执行两次后,你可以看到mysql结果:
7、日志:
0113/07/29 02:33:03 WARN mapred.JobClient: Use GenericOptionsParserfor parsing the arguments. Applications should implement Toolfor the same.
0213/07/29 02:33:03 INFO filecache.TrackerDistributedCacheManager: Creating mysql-connector-java-5.0.8-bin.jarin /tmp/hadoop-june/mapred/local/archive/-8943686319031389138_-1232673160_640840668/192.168.1.101/tmp-work--8372797484204470322 with rwxr-xr-x
0313/07/29
02:33:03 INFO filecache.TrackerDistributedCacheManager: Cached
hdfs://192.168.1.101:9000/tmp/mysql-connector-java-5.0.8-bin.jar as
/tmp/hadoop-june/mapred/local/archive/-8943686319031389138_-1232673160_640840668/192.168.1.101/tmp/mysql-connector-java-5.0.8-bin.jar
0413/07/29
02:33:03 INFO filecache.TrackerDistributedCacheManager: Cached
hdfs://192.168.1.101:9000/tmp/mysql-connector-java-5.0.8-bin.jar as
/tmp/hadoop-june/mapred/local/archive/-8943686319031389138_-1232673160_640840668/192.168.1.101/tmp/mysql-connector-java-5.0.8-bin.jar
0513/07/29 02:33:03 INFO mapred.JobClient: Running job: job_local_0001
0613/07/29 02:33:03 INFO mapred.MapTask: numReduceTasks: 1
0713/07/29 02:33:03 INFO mapred.MapTask: io.sort.mb = 100
0813/07/29 02:33:03 INFO mapred.MapTask: data buffer = 79691776/99614720
0913/07/29 02:33:03 INFO mapred.MapTask: record buffer = 262144/327680
1013/07/29 02:33:03 INFO mapred.MapTask: Starting flush of map output
1113/07/29 02:33:03 INFO mapred.MapTask: Finished spill 0
1213/07/29 02:33:03 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 isdone. And isin the process of commiting
1313/07/29 02:33:04 INFO mapred.JobClient: map 0% reduce 0%
1413/07/29 02:33:06 INFO mapred.LocalJobRunner:
1513/07/29 02:33:06 INFO mapred.Task: Task'attempt_local_0001_m_000000_0' done.
1613/07/29 02:33:06 INFO mapred.LocalJobRunner:
1713/07/29 02:33:06 INFO mapred.Merger: Merging 1 sorted segments
1813/07/29 02:33:06 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 235 bytes
1913/07/29 02:33:06 INFO mapred.LocalJobRunner:
2013/07/29 02:33:06 INFO mapred.Task: Task:attempt_local_0001_r_000000_0 isdone. And isin the process of commiting
2113/07/29 02:33:07 INFO mapred.JobClient: map 100% reduce 0%
2213/07/29 02:33:09 INFO mapred.LocalJobRunner: reduce > reduce
2313/07/29 02:33:09 INFO mapred.Task: Task'attempt_local_0001_r_000000_0' done.
2413/07/29 02:33:09 WARN mapred.FileOutputCommitter: Output path is nullin cleanup
2513/07/29 02:33:10 INFO mapred.JobClient: map 100% reduce 100%
2613/07/29 02:33:10 INFO mapred.JobClient: Job complete: job_local_0001
2713/07/29 02:33:10 INFO mapred.JobClient: Counters: 18
2813/07/29 02:33:10 INFO mapred.JobClient: File Input Format Counters
2913/07/29 02:33:10 INFO mapred.JobClient: Bytes Read=0
3013/07/29 02:33:10 INFO mapred.JobClient: File Output Format Counters
3113/07/29 02:33:10 INFO mapred.JobClient: Bytes Written=0
3213/07/29 02:33:10 INFO mapred.JobClient: FileSystemCounters
3313/07/29 02:33:10 INFO mapred.JobClient: FILE_BYTES_READ=1211691
3413/07/29 02:33:10 INFO mapred.JobClient: HDFS_BYTES_READ=1081704
3513/07/29 02:33:10 INFO mapred.JobClient: FILE_BYTES_WRITTEN=2392844
3613/07/29 02:33:10 INFO mapred.JobClient: Map-Reduce Framework
3713/07/29 02:33:10 INFO mapred.JobClient: Map output materialized bytes=239
3813/07/29 02:33:10 INFO mapred.JobClient: Map input records=13
3913/07/29 02:33:10 INFO mapred.JobClient: Reduce shuffle bytes=0
4013/07/29 02:33:10 INFO mapred.JobClient: Spilled Records=26
4113/07/29 02:33:10 INFO mapred.JobClient: Map output bytes=207
4213/07/29 02:33:10 INFO mapred.JobClient: Map input bytes=13
4313/07/29 02:33:10 INFO mapred.JobClient: SPLIT_RAW_BYTES=75
4413/07/29 02:33:10 INFO mapred.JobClient: Combine input records=0
4513/07/29 02:33:10 INFO mapred.JobClient: Reduce input records=13
4613/07/29 02:33:10 INFO mapred.JobClient: Reduce inputgroups=13
4713/07/29 02:33:10 INFO mapred.JobClient: Combine output records=0
4813/07/29 02:33:10 INFO mapred.JobClient: Reduce output records=13
4913/07/29 02:33:10 INFO mapred.JobClient: Map output records=13
8、REF:
新版 API 写法:
http://superlxw1234.iteye.com/blog/1880712
老版:
http://blog.csdn.net/dajuezhao/article/details/5799371
http://www.zhengmenbb.com/archives/583.htm