Mapreduce实战:序列化与反序列化 int,int[],string[][]

来源:互联网 发布:baselight调色软件 编辑:程序博客网 时间:2024/05/20 06:53
最新一期《中国IT产业发展报告》在2016中国(深圳)IT领袖峰会上正式公布,数字中国联合会常务理事李颖称,中国IT产业完成了从要素驱动向效率驱动的过渡,目前正在由效率驱动向创新驱动发展。

//定义要序列化的类型  protected int[] splits;//int 数组   protected String[][] splitss;// 二维数组  protected int n1; //int      public void cFPoints1(Vector<Text2> sample, int n) {    String[]strs = sample.toString().split (",");    int numSplits = strs.length/3;    //对数组进行长度设定    this.splits = new int[numSplits];     this.splitss = new String[numSplits][3];     //赋值    this.n1 = n;    for (int i =0; i < sample.size();i++){    String  string = sample.get(i).toString();     String[]strs1 = string.toString().split (",");    this.splits[i] = Integer.parseInt(strs1[0]);    this.splitss[i][0] = strs1[0]; this.splitss[i][1] = strs1[1];this.splitss[i][2] = strs1[2];    }        }   
<span style="font-family: Arial, Helvetica, sans-serif;">//注意序列化和反序列化的顺序要一致</span>
  @Override  public void write(DataOutput out) throws IOException {     //序列化int类型</span>    out.writeInt(n1);     //序列化int数组</span>    out.writeInt(splits.length);    ByteBuffer bbuffer = ByteBuffer.allocate(splits.length * 4);    for (int split : splits)      bbuffer.putInt(split);    out.write(bbuffer.array(), bbuffer.arrayOffset(), bbuffer.position());     //序列化string数组</span>    out.writeInt(splitss.length);           for (String[] is2 : splitss) {for (String i : is2) {Text.writeString(out, i);}    }        }  @Override  public void readFields(DataInput in) throws IOException {     //反序列化int类型</span>    n1 = in.readInt();      //反序列化int数组</span>    splits = new int[in.readInt()];    byte[] buffer = new byte[splits.length * 4];    in.readFully(buffer);    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);    for (int i = 0; i < splits.length; i++)      splits[i] = bbuffer.getInt();    //反序列化string数组</span>    splitss = new String[in.readInt()][3];      for (int i = 0; i < splitss.length; i++)     for (int k = 0; k < splitss[0].length; k++) {       splitss[i][k] = Text.readString(in);      }  }  }


0 0
原创粉丝点击