前面講過了怎么通過mapreduce把mysql的一張表的數據放到另外一張表中,這次講的是把mysql的數據讀取到hdfs里面去
具體怎么搭建環境我這里就不多說了。參考
通過mapreduce把mysql的一張表的數據導到另外一張表中
也在eclipse里面創建一個mapreduce工程
具體的實現代碼
package com.gong.mrmysql; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import org.apache.hadoop.filecache.DistributedCache; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; import org.apache.hadoop.mapred.FileOutputFormat; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.MapReduceBase; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.lib.IdentityReducer; import org.apache.hadoop.mapred.lib.db.DBConfiguration; import org.apache.hadoop.mapred.lib.db.DBInputFormat; import org.apache.hadoop.mapred.lib.db.DBOutputFormat; import org.apache.hadoop.mapred.lib.db.DBWritable; /** * Function: 測試 mr 與 mysql 的數據交互,此測試用例將一個表中的數據復制到另一張表中 * 實際當中,可能只需要從 mysql 讀,或者寫到 mysql 中。 * date: 2013-7-29 上午2:34:04 <br/> * @author june */ public class Mysql2Mr { // DROP TABLE IF EXISTS `hadoop`.`studentinfo`; // CREATE TABLE studentinfo ( // id INTEGER NOT NULL PRIMARY KEY, // name VARCHAR(32) NOT NULL); public static class StudentinfoRecord implements Writable, DBWritable { int id; String name; //構造方法 public StudentinfoRecord() { } //Writable接口是對數據流進行操作的,所以輸入是DataInput類對象 public void readFields(DataInput in) throws IOException { this.id = in.readInt(); //輸入流中的讀取下一個整數,並返回 this.name = Text.readString(in); } public String toString() { return new String(this.id + " " + this.name); } //DBWritable負責對數據庫進行操作,所以輸出格式是PreparedStatement //PreparedStatement接口繼承並擴展了Statement接口,用來執行動態的SQL語句,即包含參數的SQL語句 @Override public void write(PreparedStatement stmt) throws SQLException { stmt.setInt(1, this.id); stmt.setString(2, this.name); } //DBWritable負責對數據庫進行操作,輸入格式是ResultSet // ResultSet接口類似於一張數據表,用來暫時存放從數據庫查詢操作所獲得的結果集 @Override public void readFields(ResultSet result) throws SQLException { this.id = result.getInt(1); this.name = result.getString(2); } //Writable接口是對數據流進行操作的,所以輸出是DataOutput類對象 @Override public void write(DataOutput out) throws IOException { out.writeInt(this.id); Text.writeString(out, this.name); } } // 記住此處是靜態內部類,要不然你自己實現無參構造器,或者等着拋異常: // Caused by: java.lang.NoSuchMethodException: DBInputMapper.<init>() // http://stackoverflow.com/questions/7154125/custom-mapreduce-input-format-cant-find-constructor // 網上腦殘式的轉帖,沒見到一個寫對的。。。 public static class DBInputMapper extends MapReduceBase implements Mapper<LongWritable, StudentinfoRecord, LongWritable, Text> { public void map(LongWritable key, StudentinfoRecord value, OutputCollector<LongWritable, Text> collector, Reporter reporter) throws IOException { collector.collect(new LongWritable(value.id), new Text(value.toString())); } } public static class MyReducer extends MapReduceBase implements Reducer<LongWritable, Text, StudentinfoRecord, Text> { @Override public void reduce(LongWritable key, Iterator<Text> values, OutputCollector<StudentinfoRecord, Text> output, Reporter reporter) throws IOException { String[] splits = values.next().toString().split(" "); StudentinfoRecord r = new StudentinfoRecord(); r.id = Integer.parseInt(splits[0]); r.name = splits[1]; output.collect(r, new Text(r.name)); } } public static void main(String[] args) throws IOException { JobConf conf = new JobConf(Mysql2Mr.class); DistributedCache.addFileToClassPath(new Path("hdfs://192.168.241.13:9000/mysqlconnector/mysql-connector-java-5.1.38-bin.jar"), conf); conf.setMapOutputKeyClass(LongWritable.class); conf.setMapOutputValueClass(Text.class); conf.setOutputKeyClass(LongWritable.class); conf.setOutputValueClass(Text.class); // conf.setOutputFormat(DBOutputFormat.class); conf.setInputFormat(DBInputFormat.class); // mysql to hdfs conf.set("fs.defaultFS", "hdfs://192.168.241.13:9000");//在配置文件conf中指定所用的文件系統---HDFS conf.setReducerClass(IdentityReducer.class); Path outPath = new Path("hdfs://192.168.241.13:9000/student/out1"); FileSystem.get(conf).delete(outPath, true); FileOutputFormat.setOutputPath(conf, outPath); DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver", "jdbc:mysql://192.168.241.13:3306/mrtest", "root", "543116"); String[] fields = { "id", "name" }; // 從 t 表讀數據 DBInputFormat.setInput(conf, StudentinfoRecord.class, "t", null, "id", fields); // mapreduce 將數據輸出到 t2 表 //DBOutputFormat.setOutput(conf, "t2", "id", "name"); // FileOutputFormat.setOutputPath(conf, new Path("hdfs://192.168.241.13:9000/student/out1")); conf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class); conf.setMapperClass(DBInputMapper.class); // conf.setReducerClass(MyReducer.class); JobClient.runJob(conf); } }
特別要主要的是在主函數里面添加這么一句話
如果不添加這句話的話就不能識別你的hdfs路徑了,除了這個方法之外還,不想添加這句話的話還可以把集群的core-site.xml文件直接拷貝一份放到工程的src目錄下
這樣也是可以的
運行程序
可以看到hdfs的文件上面已經有mysql數據庫表的內容了