码迷,mamicode.com
首页 > 其他好文 > 详细

记一个简单的序列化

时间:2018-06-27 17:33:28      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:反序列化   write   tput   反序   com   ack   throw   ide   this   

package com.atguigu.mapreduce.flowsum;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

public class FlowBean implements Writable{

private long upFlow; // 上行流量
private long downFlow; // 下行流量
private long sumFlow; // 总流量

// 必须要有空参构造,为了后续反射用
public FlowBean() {
super();
}

public FlowBean(long upFlow, long downFlow) {
super();
this.upFlow = upFlow;
this.downFlow = downFlow;
this.sumFlow = upFlow + downFlow;
}

public void set(long upFlow, long downFlow){
this.upFlow = upFlow;
this.downFlow = downFlow;
this.sumFlow = upFlow + downFlow;
}

// 序列化方法
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(upFlow);
out.writeLong(downFlow);
out.writeLong(sumFlow);
}

// 反序列化方法
// 注意:序列化顺序和反序列化顺序必须一致
@Override
public void readFields(DataInput in) throws IOException {
this.upFlow = in.readLong();
this.downFlow = in.readLong();
this.sumFlow = in.readLong();
}


@Override
public String toString() {
return upFlow + "\t" + downFlow + "\t" + sumFlow ;
}

public long getUpFlow() {
return upFlow;
}

public void setUpFlow(long upFlow) {
this.upFlow = upFlow;
}

public long getDownFlow() {
return downFlow;
}

public void setDownFlow(long downFlow) {
this.downFlow = downFlow;
}

public long getSumFlow() {
return sumFlow;
}

public void setSumFlow(long sumFlow) {
this.sumFlow = sumFlow;
}
}

记一个简单的序列化

标签:反序列化   write   tput   反序   com   ack   throw   ide   this   

原文地址:https://www.cnblogs.com/geek-sq/p/9234867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!