HADOOP-输出数据实体类承载

新建一个bean包:

  1.实现Writerable

  2.有一个空的构造方法

 

代码实现:

 1 import java.io.DataInput;
 2 import java.io.DataOutput;
 3 import java.io.IOException;
 4 
 5 import org.apache.hadoop.io.Writable;
 6 //实现 hadoop中 的 Writable 接口
 7 public class Phone implements Writable{
 8     private int up;
 9     private int down;
10     public Phone(int up, int down) {
11         this.up = up;
12         this.down = down;
13     }
14     public Phone() { //保留空构造器
15     }
16     
17     @Override//自定义 格式 tostring
18     public String toString() {
19         return up + "\t" + down + "\t"+(up+down);
20     }
21     public int getUp() {
22         return up;
23     }
24     public void setUp(int up) {
25         this.up = up;
26     }
27     public int getDown() {
28         return down;
29     }
30     public void setDown(int down) {
31         this.down = down;
32     }
33     @Override  //重写 两个方法, 按顺序 写和读。
34     public void readFields(DataInput input) throws IOException {
35         this.up=input.readInt();
36         this.down= input.readInt();
37     }
38     @Override
39     public void write(DataOutput output) throws IOException {
40         output.writeInt(up);
41         output.writeInt(down);
42     }    
43 }
View Code

 

posted @ 2018-09-10 21:54  小平哥Keeping  阅读(141)  评论(0编辑  收藏  举报