Hadoop 中利用 mapreduce 读写 mysql 数据

释放双眼,带上耳机,听听看~!

有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 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、测试数据:

01 CREATE TABLE 
1
1t

(

1
1
02
1
1id

 int DEFAULT NULL,

1
1
03
1
1name

 varchar(10) DEFAULT NULL

1
1
04 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1
1
05  

1
1
06 CREATE TABLE 
1
1t2

(

1
1
07
1
1id

 int DEFAULT NULL,

1
1
08
1
1name

 varchar(10) DEFAULT NULL

1
1
09 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1
1
10  

1
1
11 insert into t values (1,"june"),(2,"decli"),(3,"hello"),

1
1
12     (4,"june"),(5,"decli"),(6,"hello"),(7,"june"),

1
1
13     (8,"decli"),(9,"hello"),(10,"june"),

1
1
14     (11,"june"),(12,"decli"),(13,"hello");

1
1

5、代码:

001 import java.io.DataInput;

1
1
002 import java.io.DataOutput;

1
1
003 import java.io.IOException;

1
1
004 import java.sql.PreparedStatement;

1
1
005 import java.sql.ResultSet;

1
1
006 import java.sql.SQLException;

1
1
007 import java.util.Iterator;

1
1
008  

1
1
009 import org.apache.hadoop.filecache.DistributedCache;

1
1
010 import org.apache.hadoop.fs.Path;

1
1
011 import org.apache.hadoop.io.LongWritable;

1
1
012 import org.apache.hadoop.io.Text;

1
1
013 import org.apache.hadoop.io.Writable;

1
1
014 import org.apache.hadoop.mapred.JobClient;

1
1
015 import org.apache.hadoop.mapred.JobConf;

1
1
016 import org.apache.hadoop.mapred.MapReduceBase;

1
1
017 import org.apache.hadoop.mapred.Mapper;

1
1
018 import org.apache.hadoop.mapred.OutputCollector;

1
1
019 import org.apache.hadoop.mapred.Reducer;

1
1
020 import org.apache.hadoop.mapred.Reporter;

1
1
021 import org.apache.hadoop.mapred.lib.IdentityReducer;

1
1
022 import org.apache.hadoop.mapred.lib.db.DBConfiguration;

1
1
023 import org.apache.hadoop.mapred.lib.db.DBInputFormat;

1
1
024 import org.apache.hadoop.mapred.lib.db.DBOutputFormat;

1
1
025 import org.apache.hadoop.mapred.lib.db.DBWritable;

1
1
026  

1
1
027 /**

1
1
028  * Function: 测试 mr 与 mysql 的数据交互,此测试用例将一个表中的数据复制到另一张表中

1
1
029  *           实际当中,可能只需要从 mysql 读,或者写到 mysql 中。

1
1
030  * date: 2013-7-29 上午2:34:04 <br/>

1
1
031  * @author june

1
1
032  */

1
1
033 public class Mysql2Mr {

1
1
034     // DROP TABLE IF EXISTS
1
1hadoop

.

1
1studentinfo

;

1
1
035     // CREATE TABLE studentinfo (

1
1
036     // id INTEGER NOT NULL PRIMARY KEY,

1
1
037     // name VARCHAR(32) NOT NULL);

1
1
038  

1
1
039     public static class StudentinfoRecord implements Writable, DBWritable {

1
1
040         int id;

1
1
041         String name;

1
1
042  

1
1
043         public StudentinfoRecord() {

1
1
044  

1
1
045         }

1
1
046  

1
1
047         public void readFields(DataInput in) throws IOException {

1
1
048             this.id = in.readInt();

1
1
049             this.name = Text.readString(in);

1
1
050         }

1
1
051  

1
1
052         public String toString() {

1
1
053             return new String(this.id + " " + this.name);

1
1
054         }

1
1
055  

1
1
056         @Override

1
1
057         public void write(PreparedStatement stmt) throws SQLException {

1
1
058             stmt.setInt(1, this.id);

1
1
059             stmt.setString(2, this.name);

1
1
060         }

1
1
061  

1
1
062         @Override

1
1
063         public void readFields(ResultSet result) throws SQLException {

1
1
064             this.id = result.getInt(1);

1
1
065             this.name = result.getString(2);

1
1
066         }

1
1
067  

1
1
068         @Override

1
1
069         public void write(DataOutput out) throws IOException {

1
1
070             out.writeInt(this.id);

1
1
071             Text.writeString(out, this.name);

1
1
072         }

1
1
073     }

1
1
074  

1
1
075     // 记住此处是静态内部类,要不然你自己实现无参构造器,或者等着抛异常:

1
1
076     // Caused by: java.lang.NoSuchMethodException: DBInputMapper.<init>()

1
1
077     // http://stackoverflow.com/questions/7154125/custom-mapreduce-input-format-cant-find-constructor

1
1
078     // 网上脑残式的转帖,没见到一个写对的。。。

1
1
079     public static class DBInputMapper extends MapReduceBase implements

1
1
080             Mapper<LongWritable, StudentinfoRecord, LongWritable, Text> {

1
1
081         public void map(LongWritable key, StudentinfoRecord value,

1
1
082                 OutputCollector<LongWritable, Text> collector, Reporter reporter) throws IOException {

1
1
083             collector.collect(new LongWritable(value.id), new Text(value.toString()));

1
1
084         }

1
1
085     }

1
1
086  

1
1
087     public static class MyReducer extends MapReduceBase implements

1
1
088             Reducer<LongWritable, Text, StudentinfoRecord, Text> {

1
1
089         @Override

1
1
090         public void reduce(LongWritable key, Iterator<Text> values,

1
1
091                 OutputCollector<StudentinfoRecord, Text> output, Reporter reporter) throws IOException {

1
1
092             String[] splits = values.next().toString().split(" ");

1
1
093             StudentinfoRecord r = new StudentinfoRecord();

1
1
094             r.id = Integer.parseInt(splits[0]);

1
1
095             r.name = splits[1];

1
1
096             output.collect(r, new Text(r.name));

1
1
097         }

1
1
098     }

1
1
099  

1
1
100     public static void main(String[] args) throws IOException {

1
1
101         JobConf conf = new JobConf(Mysql2Mr.class);

1
1
102         DistributedCache.addFileToClassPath(new Path("/tmp/mysql-connector-java-5.0.8-bin.jar"), conf);

1
1
103  

1
1
104         conf.setMapOutputKeyClass(LongWritable.class);

1
1
105         conf.setMapOutputValueClass(Text.class);

1
1
106         conf.setOutputKeyClass(LongWritable.class);

1
1
107         conf.setOutputValueClass(Text.class);

1
1
108  

1
1
109         conf.setOutputFormat(DBOutputFormat.class);

1
1
110         conf.setInputFormat(DBInputFormat.class);

1
1
111         // // mysql to hdfs

1
1
112         // conf.setReducerClass(IdentityReducer.class);

1
1
113         // Path outPath = new Path("/tmp/1");

1
1
114         // FileSystem.get(conf).delete(outPath, true);

1
1
115         // FileOutputFormat.setOutputPath(conf, outPath);

1
1
116  

1
1
117         DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver", "jdbc:mysql://192.168.1.101:3306/test",

1
1
118                 "root", "root");

1
1
119         String[] fields = { "id", "name" };

1
1
120         // 从 t 表读数据

1
1
121         DBInputFormat.setInput(conf, StudentinfoRecord.class, "t", null, "id", fields);

1
1
122         // mapreduce 将数据输出到 t2 表

1
1
123         DBOutputFormat.setOutput(conf, "t2", "id", "name");

1
1
124         // conf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);

1
1
125         conf.setMapperClass(DBInputMapper.class);

1
1
126         conf.setReducerClass(MyReducer.class);

1
1
127  

1
1
128         JobClient.runJob(conf);

1
1
129     }

1
1
130 }

1
1

6、结果:

执行两次后,你可以看到mysql结果:

01 mysql> select * from t2;

1
1
02 +——+——-+

1
1
03 | id   | name  |

1
1
04 +——+——-+

1
1
05 |    1 | june  |

1
1
06 |    2 | decli |

1
1
07 |    3 | hello |

1
1
08 |    4 | june  |

1
1
09 |    5 | decli |

1
1
10 |    6 | hello |

1
1
11 |    7 | june  |

1
1
12 |    8 | decli |

1
1
13 |    9 | hello |

1
1
14 |   10 | june  |

1
1
15 |   11 | june  |

1
1
16 |   12 | decli |

1
1
17 |   13 | hello |

1
1
18 |    1 | june  |

1
1
19 |    2 | decli |

1
1
20 |    3 | hello |

1
1
21 |    4 | june  |

1
1
22 |    5 | decli |

1
1
23 |    6 | hello |

1
1
24 |    7 | june  |

1
1
25 |    8 | decli |

1
1
26 |    9 | hello |

1
1
27 |   10 | june  |

1
1
28 |   11 | june  |

1
1
29 |   12 | decli |

1
1
30 |   13 | hello |

1
1
31 +——+——-+

1
1
32 26 rows in set (0.00 sec)

1
1
33  

1
1
34 mysql>

1
1

7、日志:

01 13/07/29 02:33:03 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.

1
1
02 13/07/29 02:33:03 INFO filecache.TrackerDistributedCacheManager: Creating mysql-connector-java-5.0.8-bin.jar in /tmp/hadoop-june/mapred/local/archive/-8943686319031389138_-1232673160_640840668/192.168.1.101/tmp-work–8372797484204470322 with rwxr-xr-x

1
1
03 13/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

1
1
04 13/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

1
1
05 13/07/29 02:33:03 INFO mapred.JobClient: Running job: job_local_0001

1
1
06 13/07/29 02:33:03 INFO mapred.MapTask: numReduceTasks: 1

1
1
07 13/07/29 02:33:03 INFO mapred.MapTask: io.sort.mb = 100

1
1
08 13/07/29 02:33:03 INFO mapred.MapTask: data buffer = 79691776/99614720

1
1
09 13/07/29 02:33:03 INFO mapred.MapTask: record buffer = 262144/327680

1
1
10 13/07/29 02:33:03 INFO mapred.MapTask: Starting flush of map output

1
1
11 13/07/29 02:33:03 INFO mapred.MapTask: Finished spill 0

1
1
12 13/07/29 02:33:03 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting

1
1
13 13/07/29 02:33:04 INFO mapred.JobClient:  map 0% reduce 0%

1
1
14 13/07/29 02:33:06 INFO mapred.LocalJobRunner:

1
1
15 13/07/29 02:33:06 INFO mapred.Task: Task 'attempt_local_0001_m_000000_0' done.

1
1
16 13/07/29 02:33:06 INFO mapred.LocalJobRunner:

1
1
17 13/07/29 02:33:06 INFO mapred.Merger: Merging 1 sorted segments

1
1
18 13/07/29 02:33:06 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 235 bytes

1
1
19 13/07/29 02:33:06 INFO mapred.LocalJobRunner:

1
1
20 13/07/29 02:33:06 INFO mapred.Task: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting

1
1
21 13/07/29 02:33:07 INFO mapred.JobClient:  map 100% reduce 0%

1
1
22 13/07/29 02:33:09 INFO mapred.LocalJobRunner: reduce > reduce

1
1
23 13/07/29 02:33:09 INFO mapred.Task: Task 'attempt_local_0001_r_000000_0' done.

1
1
24 13/07/29 02:33:09 WARN mapred.FileOutputCommitter: Output path is null in cleanup

1
1
25 13/07/29 02:33:10 INFO mapred.JobClient:  map 100% reduce 100%

1
1
26 13/07/29 02:33:10 INFO mapred.JobClient: Job complete: job_local_0001

1
1
27 13/07/29 02:33:10 INFO mapred.JobClient: Counters: 18

1
1
28 13/07/29 02:33:10 INFO mapred.JobClient:   File Input Format Counters

1
1
29 13/07/29 02:33:10 INFO mapred.JobClient:     Bytes Read=0

1
1
30 13/07/29 02:33:10 INFO mapred.JobClient:   File Output Format Counters

1
1
31 13/07/29 02:33:10 INFO mapred.JobClient:     Bytes Written=0

1
1
32 13/07/29 02:33:10 INFO mapred.JobClient:   FileSystemCounters

1
1
33 13/07/29 02:33:10 INFO mapred.JobClient:     FILE_BYTES_READ=1211691

1
1
34 13/07/29 02:33:10 INFO mapred.JobClient:     HDFS_BYTES_READ=1081704

1
1
35 13/07/29 02:33:10 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=2392844

1
1
36 13/07/29 02:33:10 INFO mapred.JobClient:   Map-Reduce Framework

1
1
37 13/07/29 02:33:10 INFO mapred.JobClient:     Map output materialized bytes=239

1
1
38 13/07/29 02:33:10 INFO mapred.JobClient:     Map input records=13

1
1
39 13/07/29 02:33:10 INFO mapred.JobClient:     Reduce shuffle bytes=0

1
1
40 13/07/29 02:33:10 INFO mapred.JobClient:     Spilled Records=26

1
1
41 13/07/29 02:33:10 INFO mapred.JobClient:     Map output bytes=207

1
1
42 13/07/29 02:33:10 INFO mapred.JobClient:     Map input bytes=13

1
1
43 13/07/29 02:33:10 INFO mapred.JobClient:     SPLIT_RAW_BYTES=75

1
1
44 13/07/29 02:33:10 INFO mapred.JobClient:     Combine input records=0

1
1
45 13/07/29 02:33:10 INFO mapred.JobClient:     Reduce input records=13

1
1
46 13/07/29 02:33:10 INFO mapred.JobClient:     Reduce input groups=13

1
1
47 13/07/29 02:33:10 INFO mapred.JobClient:     Combine output records=0

1
1
48 13/07/29 02:33:10 INFO mapred.JobClient:     Reduce output records=13

1
1
49 13/07/29 02:33:10 INFO mapred.JobClient:     Map output records=13

1
1

给TA打赏
共{{data.count}}人
人已打赏
安全运维

MongoDB数据建模小案例:朋友圈评论内容管理

2021-12-11 11:36:11

安全运维

Ubuntu上NFS的安装配置

2021-12-19 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索