Hadoop中自定义Writable类型(FFT中复数)

 定义一个自己的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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值