HadoopType

本文详细解释了Hadoop程序中Key/Value的类型及其应用,包括实现WritableComparable接口的示例代码,以及不同阶段输入输出的类型转换。同时介绍了Key和Value阵营的接口实现,并列举了常用的数据类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
下面列出常用的类型
[table]
|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
[/table]
基本类目都已经定义了,包括String[text],Null(NullWritable)都已经定义。除了复杂性业务外,只是简单的参数统计,使用hadoop已经定义的类型已经够了。
至于使用多复杂的对象,就看多复杂的业务了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值