Thursday, March 18, 2010

Hadoop中Writable接口的序列化

一种常用的序列化方法如下:
public static class Key implements WritableComparable { public String id = ""; public short weight; public Key() { } public Key(String i, short j) { id = i; weight = j; } @Override public void readFields(DataInput in) throws IOException { //先写字符串的长度信息 int length = in.readInt(); byte[] buf = new byte[length]; in.readFully(buf, 0, length); //得到id号 id = new String(buf); //得到权值 weight = in.readShort(); } @Override public void write(DataOutput out) throws IOException { String str = id.toString(); int length = str.length(); byte[] buf = str.getBytes(); //先写字符串长度 //WritableUtils.writeVInt(out, length); out.writeInt(length); //再写字符串数据 out.write(buf, 0, length); //接着是权值 out.writeShort(weight); } @Override public int hashCode() { return id.hashCode(); } @Override public String toString() { return id; } @Override public boolean equals(Object right) { //只要id相等就认为两个key相等 if (right instanceof Key) { Key r = (Key) right; return r.id.equals(id); } else { return false; } } @Override public int compareTo(Key k) { System.out.println("in compareTo, key=" + k.toString()); //先比较value id int cmp = id.compareTo(k.id); if (cmp != 0) { return cmp; } //如果value id相等,再比较权值 if (weight > k.weight) return -1; else if (weight < k.weight) return 1; else return 0; } }

No comments: