HadoopType

Hadoop的类型。
在编写Hadoop的程序的时候,需要传递Key/Value.
比如从Map开始,会有输入对K1,V1.根据不同的输入类型会得到不同的输入对。
map结束后,也会输出K2,V2.
在Combiner间段,会有K2,V2作为输入,K3,V3作为输出,当然这个间段不是必须的.
在Reducer间段,会有K3,V3作为输入,K4,V4作为输出。
其中的K1,K2,K3,K4是作为Key的阵营,而V1,V2,V3,V4作为value的阵营。
其中K的阵营有其必须实现的接口 WritableComparable<T>
例子代码如下:
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

/**
 * define the type of Data.
 * @author yangchunlong
 *
 */
public class YclType implements WritableComparable<YclType>{
	Long userId;
	Long moduleId;
	int count;

	@Override
	public void readFields(DataInput in) throws IOException {
		userId = in.readLong();
		moduleId = in.readLong();
		count = in.readInt();

	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeLong(userId);
		out.writeLong(moduleId);
		out.writeInt(count);
	}

	@Override
	public int compareTo(YclType o) {
		return o.userId.compareTo(o.userId);
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getModuleId() {
		return moduleId;
	}

	public void setModuleId(Long moduleId) {
		this.moduleId = moduleId;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

}

有人就问了,为什么要实现这个接口啊。
首先作为输出的Key,其必须实现Writeable,而作为Combiner和Reducer的输入必须实现Comparable,在Combiner和Reducer之前会对key进行分组/排序.
当然排序和分组取决于Comparable的实现,你想以哪些属性进行分组统计,就以哪些分组作为key.[如果是动态分组,可以使用动态表达式,然后解析动态表达式来判断是否属于同一组(0),大于(1),小于(-1)].

V的阵营只需要实现Writable接口就可以了,但是呢,一般是实现WritableComparable.
即可以作为key,又可以作为value.
下面列出常用的类型
BooleanWritable Wrapper for a standard Boolean variable
ByteWritable   Wrapper for a single byte
DoubleWritable Wrapper for a Double
FloatWritable  Wrapper for a Float
IntWritable    Wrapper for a Integer
LongWritable   Wrapper for a Long
Text           Wrapper to store text using the UTF8 format
NullWritable   Placeholder when the key or value is not needed

基本类目都已经定义了,包括String[text],Null(NullWritable)都已经定义。除了复杂性业务外,只是简单的参数统计,使用hadoop已经定义的类型已经够了。
至于使用多复杂的对象,就看多复杂的业务了.

你可能感兴趣的:(hadoop)