转眼间已经接触了hadoop两周了,从之前的极力排斥到如今的有点喜欢,刚开始被搭建hadoop开发环境搞得几乎要放弃,如今学会了编写小程序,每天都在成长一点挺好的,好好努力,为自己的装备库再填一件武器挺好的,学习在于坚持不懈,加油!!!
马上就要过年了,在最后一天的上班时间内完成了hadoop如何去连接mysql数据库,自己感到很满足,下面就把自己编写的源码贡献给大家,希望能够帮到你们,如存在优化的地方还请大牛们指出,也希望有hadoop的大牛能够给点学习建议,一个来个HA初学者的心声。第一次发布竟然被退回,也不知道为什么,瞬间心情都不好了,但我还是坚持写自己的博客...
StudentRecord类:
package com.simope.mr.db; 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 org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; import org.apache.hadoop.mapred.lib.db.DBWritable; public class StudentRecord implements Writable, DBWritable{ int id; String name; int age; int departmentID; @Override public void readFields(DataInput in) throws IOException { this.id = in.readInt(); this.name = Text.readString(in); this.age = in.readInt(); this.departmentID = in.readInt(); } @Override public void write(DataOutput out) throws IOException { out.write(this.id); Text.writeString(out, this.name); out.write(this.age); out.write(this.departmentID); } public void readFields(ResultSet rs) throws SQLException { this.id = rs.getInt(1); this.name = rs.getString(2); this.age = rs.getInt(3); this.departmentID = rs.getInt(4); } public void write(PreparedStatement ps) throws SQLException { ps.setInt(1, this.id); ps.setString(2, this.name); ps.setInt(3, this.age); ps.setInt(4, this.departmentID); } @Override public String toString() { return new String(this.name + "\t" + this.age + "\t" + this.departmentID); } } |
TeacherRecord类:
package com.simope.mr.db; 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 org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; import org.apache.hadoop.mapred.lib.db.DBWritable; public class TeacherRecord implements Writable, DBWritable{ int id; String name; int age; int departmentID; @Override public void readFields(DataInput in) throws IOException { this.id = in.readInt(); this.name = Text.readString(in); this.age = in.readInt(); this.departmentID = in.readInt(); } @Override public void write(DataOutput out) throws IOException { out.write(this.id); Text.writeString(out, this.name); out.write(this.age); out.write(this.departmentID); } public void readFields(ResultSet rs) throws SQLException { this.id = rs.getInt(1); this.name = rs.getString(2); this.age = rs.getInt(3); this.departmentID = rs.getInt(4); } public void write(PreparedStatement ps) throws SQLException { ps.setInt(1, this.id); ps.setString(2, this.name); ps.setInt(3, this.age); ps.setInt(4, this.departmentID); } @Override public String toString() { return new String(this.name + "\t" + this.age + "\t" + this.departmentID); } } |