版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86482963
1 MapRedce框架数据类型
- 数据类型都实现
Writable
接口,以便用这些类型定义的数据可以被序列化进行网络传输和文件存储;
1.1 基本数据类型
BooleanWritable
ByteWritable
DoubleWritable
FloatWritable
IntWritable
LongWritable
Text
:使用 UTF-8 格式存储的文本NullWritable
: 当 <key,value> 中的 key 或者 value 为空是使用;
1.2 自定义数据类型
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;
public class PairWritable implements WritableComparable<PairWritable> {
private int id;
private String name;
public PairWritable() {
}
public PairWritable(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return id + "\t" + name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PairWritable that = (PairWritable) o;
return id == that.id &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
public void write(DataOutput out) throws IOException {
out.write(id);
out.writeUTF(name);
}
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = in.readUTF();
}
@Override
public int compareTo(PairWritable o) {
int comp = Integer.valueOf(this.getId()).compareTo(Integer.valueOf(o.getId()));
if( 0 != comp){
return comp;
}
return this.getName().compareTo(o.getName());
}
}