zoukankan      html  css  js  c++  java
  • Mapreduce实战:序列化与反序列化 int,int[],string[][]

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


  • 相关阅读:
    0455分发饼干 Marathon
    0078子集 Marathon
    python 实现JWT Marathon
    0376摆动序列 Marathon
    0216.组合总和 III Marathon
    028实现strStr() Marathon
    0738单调递增的数字 Marathon
    0051N皇后 Marathon
    0047全排列II Marathon
    0037解数独 Marathon
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7207557.html
Copyright © 2011-2022 走看看