【发布时间】:2013-06-21 05:41:20
【问题描述】:
这是实现 Writable 的类..
public class Test implements Writable {
List<AtomicWritable> atoms = new ArrayList<AtomicWritable>();
public void write(DataOutput out) throws IOException {
IntWritable size = new IntWritable(atoms.size());
size.write(out);
for (AtomicWritable atom : atoms)
atom.write(out);
}
public void readFields(DataInput in) throws IOException {
atoms.clear();
IntWritable size = new IntWritable();
size.readFields(in);
int n = size.get();
while(n-- > 0) {
AtomicWritable atom = new AtomicWritable();
atom.readFields(in);
atoms.add(atom);
}
}
}
如果有人能帮助我理解如何调用 write 和 readFields 方法,我将不胜感激。 基本上我无法理解在这种情况下如何构造 Test 对象。一旦对象被写入 DataOutput obj,我们如何在 DataInput 对象中恢复它。这可能听起来很傻,但我是 Hadoop 的新手,并且被分配了一个使用 Hadoop 的项目。请帮忙。
谢谢!!!
【问题讨论】:
-
你需要考虑它不是线程安全的。
-
专业小贴士:如果您要问很多 questions here,尤其是在 Hadoop 等不那么拥挤的主题上(比 C# 和 Java 不那么拥挤),请确保您投票并接受您previously asked 的问题的答案。如果你不这样做,你很快就会对社区失去同情。
标签: serialization hadoop writable