排序

图解

在这里插入图片描述

准备工作

1、准备好一个sort.txt文件
在这里插入图片描述

代码段

SortMapper.java


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


public class SortMapper extends Mapper<LongWritable, Text,PairWritable,Text> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //1、对每一行数据进行拆分,然后封装到PairWritable对象中,作为k2
        String[] split = value.toString().split("\t");
        PairWritable pairWritable = new PairWritable();
        pairWritable.setFirst(split[0]);
        pairWritable.setSecond(Integer.parseInt(split[1]));

        //2、将k2,v2写入到上下文中
        context.write(pairWritable,value);

    }
}

SortReducer.java

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SortReducer extends Reducer<PairWritable, Text,PairWritable, NullWritable>{
    @Override
    protected void reduce(PairWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        //因为一样的k可能会形成这样的V <a 1,a 1>,所以要遍历一次写一次k,这样就不会漏了
        for (Text value : values) {
            context.write(key,NullWritable.get());
        }

    }
}

PairWritable.java

import org.apache.hadoop.io.WritableComparable;

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

public class PairWritable implements WritableComparable<PairWritable> {

    //first是左边的单词     string a  b  a  b  a
    //second是右边的单词    int    1  1  9  2  3
    private String first;
    private int second;

    //实现排序规则
    @Override
    public int compareTo(PairWritable other) {

        //先比较first,如果first相同,在比较second
        int result = this.first.compareTo(other.first);   //单词比较,如果a 大于 b就返回大于0,否则返回小于0,
        // 用奥斯卡码计算

        //如果first单词相等则比较second的单词
        if (result == 0){
            //因为second是int值所以可以直接相减返回int值给比较器
            return this.second - other.second;
        }

        //如果result不是0那就返回int值给比较强
        return result;
    }

    //实现序列化
    @Override
    public void write(DataOutput dataOutput) throws IOException {

        dataOutput.writeUTF(first);
        dataOutput.writeInt(second);


    }

    //实现反序列化
    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.first = dataInput.readUTF();
        this.second = dataInput.readInt();
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    @Override
    public String toString() {
        return  first + '\t' + second ;
    }
}

结果

排好了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值