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

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

时间:2016-03-28 07:09:22      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

最新一期《中国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); 
    	 }
  }  
}


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

标签:

原文地址:http://blog.csdn.net/yaoxiaochuang/article/details/50995191

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