Details
-
Improvement
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
1.0.3
-
None
-
None
Description
ArrayWritable is just painful to use in practice, it would be nice if we had a typed version of ArrayWritable that had all the features of an ArrayList.
It wasn't hard to write, and it doesn't cost more in terms of storage or CPU than an ArrayWritable.
So I wonder why not include a more usable ArrayListWritable class with Hadoop? Code pasted below, unless there's a reason that this is a bad idea I'm happy to provide the unit test for it as well.
@SuppressWarnings("serial")
public class ArrayListWritable<E extends Writable> extends ArrayList<E> implements Writable {
Class<E> type = null;
public ArrayListWritable(Class<E> type)
{ super(); this.type = type; }public ArrayListWritable()
{ super(); }public void setArrayClassType(Class<E> clazz)
{ this.type = clazz; } @Override
public void write(DataOutput out) throws IOException {
if(type==null)
;
out.writeUTF(type.getCanonicalName());
out.writeInt(size());
for(E writable : this) writable.write(out);
}
@SuppressWarnings("unchecked")
@Override
public void readFields(DataInput in) throws IOException {
clear();
//Read the class name
try
catch (ClassNotFoundException e)
{ throw new IOException("Invalid connonical name read from input bytes", e); } //Read the size & set capacity of the arraylist
int size = in.readInt();
ensureCapacity(size);
//Read in individual writables
for (int i = 0; i < size; i++)
}
}