MapReduce之WritableComparable类介绍
介绍 MapReduce
2023-09-14 09:13:16 时间
WritableComparable
类介绍
1.源码
package org.apache.hadoop.io;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
@InterfaceAudience.Public
@InterfaceStability.Stable
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
2.简介
A Writable which is also Comparable.
#译:一个既可比较又可序列化的【类】
WritableComparables can be compared to each other, typically via Comparators.
Any type which is to be used as a key in the Hadoop Map-Reduce framework
should implement this interface.
#译:WritableComparables能够借助比较器实现比较,在Hadoop MapReduce 框架下的被作为key的任何类型都必须
实现这个接口。
Note that hashCode() is frequently used in Hadoop to partition keys.
It's important that your implementation of hashCode() returns the same
result across different instances of the JVM. Note also that the default
hashCode() implementation in Object does not satisfy this property.
#译:注意hashCode()经常被用于Hadoop的分区键中。重要的是在实现hashcode()时,在不同的JVM实例中仍需要返回相同的结果【这句话什么意思??】。同时需要注意Object类中默认的hashCode()并不满足这个特性。
虽然Hadoop MapReduce框架为我们提供了很多key类型,但是有些时候,MapReduce过程中的Key很复杂,需要考虑到多个维度的变化,从而导致key并不完全符合Hadoop提供的原生类型,比如说Text(对应Java中的String,)、LongWritable(对应Java中的long)等。所以我们就需要自定义Hadoop MapReduce
过程中的key
类型。根据这个基类去比较key,我们所需要定义的类必须实现WritableComparable
这个类。
- 01.key是用来进行相互比较,从而达到排序的目的。
- 02.WritableComparable类实现了Writable(实现的类可序列化) 和Comparable(实现的类可比较)接口。
3.示例
Example:
public class MyWritableComparable implements WritableComparable<MyWritableComparable> {
// Some data
private int counter;
private long timestamp;
public void write(DataOutput out) throws IOException {
out.writeInt(counter);
out.writeLong(timestamp);
}
public void readFields(DataInput in) throws IOException {
counter = in.readInt();
timestamp = in.readLong();
}
public int compareTo(MyWritableComparable o) {
int thisValue = this.value;
int thatValue = o.value;
return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + counter;
result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
return result
}
}
相关文章
- Redis实战+理论 大纲介绍篇
- 【说站】java静态方法和非静态方法的介绍
- 【说站】css怪异盒模型的介绍
- 如何在Dreamweaver里面添加音乐播放器【详细介绍】--附各版本安装包
- 新能源电池产业发展,供应链系统采购合同核心业务场景介绍
- 【Elasticsearch】介绍、使用、配置、中文分词器、Kibana
- MapReduce(五) mapreduce的shuffle机制 与 Yarn详解大数据
- Spring的cache缓存介绍详解编程语言
- Go 编译器介绍
- Linux下的UUCP络用技术介绍(linuxuucp)
- Oracle补丁库介绍及使用方法(oracle补丁库)
- MySQL中的CHAR字段类型介绍(char在mysql)
- 介绍Oracle中取较小值函数的应用(oracle两者取小函数)
- JavaScript高级语法介绍