作者:empty 出版社:empty |
hadoop与mysql数据库相连读出数据
用0.20.2版本 有些类已经过时 但必须要用 因为新版本对数据库连接支持不够
运行mysql创建数据库School,建立teacher表,并自行填写值
view plaincopy to clipboardprint?
1.DROP TABLE IF EXISTS `school`.`teacher`;
2.
3.CREATE TABLE `school`.`teacher` (
4.
5. `id` int(11) default NULL,
6.
7. `name` char(20) default NULL,
8.
9. `age` int(11) default NULL,
10.
11. `departmentID` int(11) default NULL
12.
13.) ENGINE=InnoDB DEFAULT CHARSET=latin1;
在eclipse中运行编译通过 但要加入必须的库 以及 hadoop0.20.2的eclipse的插件
view plaincopy to clipboardprint?
1.import java.io.IOException;
2.
3.import org.apache.hadoop.fs.Path;
4.import org.apache.hadoop.io.LongWritable;
5.import org.apache.hadoop.io.Text;
6.import org.apache.hadoop.mapred.FileOutputFormat;
7.import org.apache.hadoop.mapred.JobClient;
8.import org.apache.hadoop.mapred.JobConf;
9.import org.apache.hadoop.mapred.lib.IdentityReducer;
10.import org.apache.hadoop.mapred.lib.db.DBConfiguration;
11.import org.apache.hadoop.mapred.lib.db.DBInputFormat;
12.
13.public class DBAccess2 {
14.
15.
16.
17. public static void main(String[] args) throws IOException {
18.
19. JobConf conf = new JobConf(DBAccess2.class);
20.
21. conf.setOutputKeyClass(LongWritable.class);
22.
23. conf.setOutputValueClass(Text.class);
24.
25.
26.
27. conf.setInputFormat(DBInputFormat.class);
28.
29. FileOutputFormat.setOutputPath(conf, new Path( hdfs://localhost:9000/dbout ));
30.
31.
32.
33. DBConfiguration.configureDB(conf, com.mysql.jdbc.Driver ,
34.
35. jdbc:mysql://localhost:3306/school , root , zxcvbnm );
36.
37.
38.
39. String [] fields = { id , name , age , departmentID };
40.
41. DBInputFormat.setInput(conf, TeacherRecord.class, teacher ,
42.
43. null, id , fields);
44.
45.
46.
47. conf.setMapperClass(DBAccessMapper.class);
48.
49. conf.setReducerClass(IdentityReducer.class);
50.
51.
52.
53. JobClient.runJob(conf);
54.
55. }
56.
57.
58.
59.}
注:请自行修改数据库连接语句 用户名 密码 等等。
view plaincopy to clipboardprint?
1.import java.io.IOException;
2.
3.import org.apache.hadoop.io.LongWritable;
4.import org.apache.hadoop.io.Text;
5.import org.apache.hadoop.mapred.MapReduceBase;
6.import org.apache.hadoop.mapred.Mapper;
7.import org.apache.hadoop.mapred.OutputCollector;
8.import org.apache.hadoop.mapred.Reporter;
9.
10.public class DBAccessMapper extends MapReduceBase implements
11.
12.Mapper LongWritable, TeacherRecord, LongWritable, Text> {
13.
14.
15.
16. @Override
17.
18. public void map(LongWritable key, TeacherRecord value,
19.
20. OutputCollector LongWritable, Text> collector, Reporter reporter)
21.
22. throws IOException {
23.
24. // TODO Auto-generated method stub
25.
26.collector.collect(new LongWritable(value.id),
27.
28. new Text(value.toString()));
29.
30. }
31.
32.
33.
34.}
view plaincopy to clipboardprint?
1.import java.io.DataInput;
2.import java.io.DataOutput;
3.import java.io.IOException;
4.import java.sql.PreparedStatement;
5.import java.sql.ResultSet;
6.import java.sql.SQLException;
7.
8.import org.apache.hadoop.io.Text;
9.import org.apache.hadoop.io.Writable;
10.import org.apache.hadoop.mapred.lib.db.DBWritable;
11.
12.public class TeacherRecord implements Writable, DBWritable{
13.
14.
15.
16. int id;
17.
18. String name;
19.
20. int age;
21.
22. int departmentID;
23.
24.
25.
26. @Override
27.
28. public void readFields(DataInput in) throws IOException {
29.
30. // TODO Auto-generated method stub
31.
32. this.id = in.readInt();
33.
34. this.name = Text.readString(in);
35.
36. this.age = in.readInt();
37.
38. this.departmentID = in.readInt();
39.
40. }
41.
42.
43.
44. @Override
45.
46. public void write(DataOutput out) throws IOException {
47.
48. // TODO Auto-generated method stub
49.
50. out.writeInt(this.id);
51.
52. Text.writeString(out, this.name);
53.
54. out.writeInt(this.age);
55.
56. out.writeInt(this.departmentID);
57.
58. }
59.
60.
61.
62. @Override
63.
64. public void readFields(ResultSet result) throws SQLException {
65.
66. // TODO Auto-generated method stub
67.
68. this.id = result.getInt(1);
69.
70. this.name = result.getString(2);
71.
72. this.age = result.getInt(3);
73.
74. this.departmentID = result.getInt(4);
75.
76. }
77.
78.
79.
80. @Override
81.
82. public void write(PreparedStatement stmt) throws SQLException {
83.
84. // TODO Auto-generated method stub
85.
86. stmt.setInt(1, this.id);
87.
88. stmt.setString(2, this.name);
89.
90. stmt.setInt(3, this.age);
91.
92. stmt.setInt(4, this.departmentID);
93.
94. }
95.
96.
97.
98. @Override
99.
100. public String toString() {
101.
102. // TODO Auto-generated method stub
103.
104. return new String(this.name + + this.age + + this.departmentID);
105.
106. }
107.
108.
109.
110.}