hadoopMR自定义输入类型

hadoop中的输入输出数据类型:

BooleanWritable:标准布尔型数值

ByteWritable:单字节数值

DoubleWritable:双字节数值

FloatWritable:浮点数

常用的:

IntWritable:整型数

LongWritable:长整型数

Text:使用UTF8格式存储的文本

NullWritable:当<key, value>中的key或value为空时使用

hadoop中的数据类型都实现了writable接口,以便这些类型的数据可以在网络传输和文件存储

自定义数据类型:

①实现WritableComparable接口,实现write和readFields方法,完成可序列化和反序列化

②如果该数据类型需要有比较顺序时,实现CompareTo()方法

③必须声明一个无参构造方法,为了方便反射,创建对象

package com.neworigin.temperature;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class Weather implements WritableComparable <Weather> {
private int year;
private int month;
private int day;
private int wd;

public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getWd() {
return wd;
}
public void setWd(int wd) {
this.wd = wd;
}
public void write(DataOutput out) throws IOException {
out.writeInt(year);
out.writeInt(month);
out.writeInt(day);
out.writeInt(wd);

}
public void readFields(DataInput in) throws IOException {
year=in.readInt();
month=in.readInt();
day=in.readInt();
wd=in.readInt();
}
//将年月日按升序排序
public int compareTo(Weather w) {
int c1 = Integer.compare(this.year,w.getYear());
if (c1==0)
{
int c2 = Integer.compare(this.month,w.getMonth());
if (c2==0)
{
return Integer.compare(this.wd,w.getWd());
}
return c2;
}
return c1;
}

}

 

转载于:https://www.cnblogs.com/chengdonghui/p/7831127.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值