hadoop 自定义TextPair和使用原理

1.hadoop TextPair组合键定义

package Temperature;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

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

public class TextPair implements WritableComparable<TextPair> {
    private Text first;
    private Text second;
    public TextPair() {
        set(new Text(),new Text());//基本类型int,long等可以不用初始化,但是对象类型变量一定要new,因为反序列化是要读取数据到first和second,会出现空指针引用的问题。
    }
    public TextPair(String first, String second) {
        set(new Text(first),new Text(second));
    }
    public TextPair(Text first, Text second) {
        set(first, second);
    }
    public void set(Text first, Text second) {
        this.first = first;
        this.second = second;
    }
    public Text getFirst() {
        return first;
    }
    public Text getSecond() {
        return second;
    }
    @Override
    public void write(DataOutput out)throws IOException {
        first.write(out);
        second.write(out);
    }
    @Override
    public void readFields(DataInput in)throws IOException {
        first.readFields(in);
        second.readFields(in);
    }
    @Override
    public int hashCode() {
        return first.hashCode() *163+ second.hashCode();
    }
    @Override
    public boolean equals(Object o) {
        if(o instanceof TextPair) {
            TextPair tp = (TextPair) o;
            return first.equals(tp.first) && second.equals(tp.second);
        }
        return false;
    }
    @Override
    public String toString() {
        return first +"\t"+ second;
    }

    public int compareTo(TextPair tp) {
        int cmp = first.compareTo(tp.first);
        if(cmp !=0) {
            return cmp;
        }
        return second.compareTo(tp.second);
    }
}

具体使用实例见我的下一篇博客

https://www.cnblogs.com/bclshuai/p/12319490.html

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

https://www.cnblogs.com/bclshuai/p/11380657.html

相关推荐