structured object를 byte stream 형태로 네트웍으로 전달하기 위해 serialization 및 deserialization 작업을 진행함 ( compact / fast / extensible / interoperable )
1. serialization code
public static byte[] serialize(Writable writable) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
writable.write(dataOut);
dataOut.close();
return out.toByteArray();
}
byte[] bytes = serialize(writable);
assertThat(bytes.length, is(10));
2. deserialization code
public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
DataInputStream dataIn = new DataInputStream(in);
writable.readFields(dataIn);
dataIn.close();
return bytes;
}
IntWritable newWritable = new IntWritable();
deserialize(newWritable, bytes);
assertThat(newWritable.get(), is(163));
'NoSQL > Hadoop' 카테고리의 다른 글
sentry 보안 (0) | 2019.06.21 |
---|---|
hadoop text file 읽기 (0) | 2017.03.13 |
hadoop compression and decompression (0) | 2017.03.10 |
yarn 구조 (0) | 2017.03.08 |
hadoop read & write (0) | 2017.03.06 |