2
나는 내 자신의 WritableComparable
을 구현했지만 단위 테스트 write
및 readFields
방법에 좋은 wasy를 찾을 수 있습니다.단위 테스트하는 방법 Hadoop Writable
아이디어가 있으십니까?
나는 내 자신의 WritableComparable
을 구현했지만 단위 테스트 write
및 readFields
방법에 좋은 wasy를 찾을 수 있습니다.단위 테스트하는 방법 Hadoop Writable
아이디어가 있으십니까?
아마 당신은 쓰기 쉽도록 테스트 할 수 있지만 수동으로 직렬화/직렬화를 수행하면됩니다. 예컨대 :
MyUtils.java : 테스트 클래스 다음
...
import org.apache.commons.io.IOUtils;
...
public static byte[] serialize(Writable writable) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = null;
try {
dataOut = new DataOutputStream(out);
writable.write(dataOut);
return out.toByteArray();
}
finally {
IOUtils.closeQuietly(dataOut);
}
}
public static <T extends Writable> T asWritable(byte[] bytes, Class<T> clazz)
throws IOException {
T result = null;
DataInputStream dataIn = null;
try {
result = clazz.newInstance();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
dataIn = new DataInputStream(in);
result.readFields(dataIn);
} catch (InstantiationException e) {
// should not happen
assert false;
} catch (IllegalAccessException e) {
// should not happen
assert false;
} finally {
IOUtils.closeQuietly(dataIn);
}
return result;
}
:
CustomWritable record = ... ; //your initialized Writable
byte[] serializedBytes = MyUtils.serialize(record);
CustomWritable deserialized =
MyUtils.asWritable(serializedBytes, CustomWritable.class);
assertEquals("Value mismatch!", record.getFieldA(), deserialized.getFieldA());
...