定义一个自己的Writable类型,需要继承Hadoop中的WritableComparable接口,而且必须实现接口中的方法hashCode(),equals(),和toString()方法。
除此之外,必须根据自己定义类型的类型重写write(DataOutput output)和readFields(DataInput in)方法,用来将数据从Map中写入Reduce函数中,如果不重写这两个函数,reduce中的会调用这个类的默认构造函数。
下面给出一个例子,这个例子是我自己定义的FFT中复数的Writable类型。
package gzz.job3;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.WritableComparable;
/**
* Complex type class for storing data from job2. contains a real part and image
* part.
*
* @author ryan
*
*/
public final class Complex implements WritableComparable<Complex>
{
private double r; // distance
private double theta; // angle
//default constructor
public Complex()
{
r=0;
theta=0;
}
// constructor that takes in rectangular coordinates
public Complex(double re, double im)
{
r = Math.sqrt(re * re + im * im);
theta = Math.atan2(im, re);
}
// accessor methods
public double re()
{
return r * Math.cos(theta);
}
public double im()
{
return r * Math.sin(theta);
}
// return a string representation of this complex number
@Override
public String toString()
{
return re() + " + " + im() + "i";
}
// return this Complex number plus b
public Complex plus(Complex b)
{
Complex a = this;
double re = a.r * Math.cos(a.theta) + b.r * Math.cos(b.theta);
double im = a.r * Math.sin(a.theta) + b.r * Math.sin(b.theta);
return new Complex(re, im);
}
// return this Complex number minus b
public Complex minus(Complex b)
{
Complex a = this;
double re = a.r * Math.cos(a.theta) - b.r * Math.cos(b.theta);
double im = a.r * Math.sin(a.theta) - b.r * Math.sin(b.theta);
return new Complex(re, im);
}
// return this Complex number times b
public Complex times(Complex b)
{
Complex a = this;
Complex c = new Complex(0, 0);
c.r = a.r * b.r; // can't make r and theta final
c.theta = a.theta + b.theta; // because of these two statements
return c;
}
// return the magnitude / absolute value of this complex number
public double abs()
{
return r;
}
@Override
public void readFields(DataInput in) throws IOException
{
r=in.readDouble();
theta=in.readDouble();
}
@Override
public void write(DataOutput out) throws IOException
{
out.writeDouble(r);
out.writeDouble(theta);
}
@Override
public int compareTo(Complex tp)
{
if(r>tp.r)
return 1;
else if(r<tp.r)
return -1;
else
return 0;
}
@Override
public int hashCode()
{
return new DoubleWritable(r).hashCode()*18+new DoubleWritable(theta).hashCode();
}
@Override
public boolean equals(Object obj)
{
Complex cpx=(Complex)obj;
if(r==cpx.r&&theta==cpx.theta)
return true;
else
return false;
}
}