Hadoop中已經定義了很多Writable的實現,基本上可以符合我們日常使用,但是在一些特殊的場景我們可能還是需要自己去實現Writable,下面主要說明如何去實現自己的Writeable,及使用自定義的Writable作為map/reduce中的key值時遇到的一些問題。
首先需要實現org.apache.hadoop.io.Writable這個接口,該接口有write和readFields這兩個方法,write用於寫數據,readFields用於讀取數據,具體如下:
private MultipleObject multipleObject;
@Override
public void readFields(DataInput dataInput) throws IOException {
length = dataInput.readInt();
bytes = new byte[length];
dataInput.readFully(bytes);
if (multipleObject == null) {
multipleObject = new MultipleObject();
}
multipleObject = SerializeUtil.deserialize(bytes, length,
multipleObject.getClass());
}
@Override
public void write(DataOutput dataOutput) throws IOException {
if (multipleObject == null) {
throw new IOException("Inner multiple object is null");
}
DataOutputBuffer out = SerializeUtil.serialize(multipleObject);
if (out != null) {
bytes = out.getData();
length = out.getData().length;
dataOutput.writeInt(length);
dataOutput.write(bytes);
}
}