首页 > 代码库 > Mapreduce实战:序列化与反序列化 int,int[],string[][]
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); } } }
Mapreduce实战:序列化与反序列化 int,int[],string[][]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。