1

I am new to map-reduce. I want to know what is the use of readfields and write methods when we implement our custom data types in hadoop? For example,

     public class Point3D implements Writable {
     public float x;
     public float y;
     public float z;

  public Point3D(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  public Point3D() {
    this(0.0f, 0.0f, 0.0f);
  }

  public void write(DataOutput out) throws IOException {
    out.writeFloat(x);
    out.writeFloat(y);
    out.writeFloat(z);
  }

  public void readFields(DataInput in) throws IOException {
    x = in.readFloat();
    y = in.readFloat();
    z = in.readFloat();
  }

  public String toString() {
    return Float.toString(x) + ", "
    + Float.toString(y) + ", "
    + Float.toString(z);
  }
  public void set(float x, float y, float z)
 {
 this.x=x;
 this.y=y;
 this.z=z;
 }
}

In the above example the custom recordreader uses the set method to set the values of x, y and z. So we finally, we get these values in the mapper. But whats the need of readfealds and write() methods from writable?? Pls. Help

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
user2758378
  • 91
  • 11

1 Answers1

1

readFileds() and write() methods are used to read and write serialized data for transferring across the network.

following question explains the need for writables.

What is the reason for having Writable wrapper classes in Hadoop MapReduce for Java types?

Community
  • 1
  • 1
dpsdce
  • 5,290
  • 9
  • 45
  • 58