數據類型(都實現了Writable接口)
BooleanWritable 布爾類型
ByteWritable 單字節數值
DoubleWritable 雙字節數值
FloatWritable 浮點數
IntWritable 整型數
LongWritable 長整型
Text UTF-8格式存儲的文本
NullWritable 空類型
因為shuffle中排序依據是key,若定義的數據類型為Key,必須實現writable和comparable接口,即WritableComparable接口
Writable
write()把每個對象序列化到輸出流
readFilds()把輸入流字節反序列化到輸入流
自定義數據類型實例
1.定義私有變量
2.setter,getter方法
3.無參有參構造器
4.set()方法,幫助構造器初始化數據(Hadoop偏愛)
5.hashCode()方法和equals()方法
6.toString()方法
7.implement Writable並實現write()方法readFilds()方法
8.implement WritableComparable並實現compareTo()方法
package com.cenzhongman.io;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
public class UserWritable implements WritableComparable<UserWritable> {
private int ip;
private String name;
public UserWritable() {
}
public UserWritable(int ip, String name) {
this.set(ip, name);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ip;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public String toString() {
return ip + "\t" + name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserWritable other = (UserWritable) obj;
if (ip != other.ip)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public void set(int ip, String name) {
this.setIp(ip);
this.setName(name);
}
public int getIp() {
return ip;
}
public void setIp(int ip) {
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// read和write方法元素的順序必須一致
@Override
public void readFields(DataInput arg0) throws IOException {
this.ip = arg0.readInt();
this.name = arg0.readUTF();
}
@Override
public void write(DataOutput arg0) throws IOException {
arg0.writeInt(ip);
arg0.writeUTF(name);
}
@Override
public int compareTo(UserWritable o) {
int comp = Integer.valueOf(this.getIp()).compareTo(o.getIp());
if (comp != 0) {
return comp;
}
return this.getName().compareTo(o.getName());
}
}