Majid Azimi
Majid Azimi

Reputation: 5745

How to use toArray() method in ArrayWritable - Hadoop

There is a toArray() method in ArrayWritable class in hadoop which should mean: convert this ArrayWritable to an array. But the syntax of of it is:

public Object toArray()

So how should we use this function? There is no documentation about it on doc package.

Upvotes: 1

Views: 2454

Answers (1)

Praveen Sripati
Praveen Sripati

Reputation: 33495

Usually the ArrayWritable has to be extended as appropriate

public class TextArrayWritable extends ArrayWritable {
    public TextArrayWritable() {
        super(Text.class);
    }
}

Here is the code from the ArrayWritable

public ArrayWritable(Class<? extends Writable> valueClass) {
    if (valueClass == null) { 
        throw new IllegalArgumentException("null valueClass"); 
    }    
    this.valueClass = valueClass;
}

public Object toArray() {
    Object result = Array.newInstance(valueClass, values.length);
        for (int i = 0; i < values.length; i++) {
            Array.set(result, i, values[i]);
        }
       return result;
}

So, ArrayWritable#toArray would return a java.lang.reflect.Array of the type specified in the super(Text.class); of the TextArrayWritable or the appropriate class.

Upvotes: 3

Related Questions