先按月份进行排序,如果月相同按照业绩排序

profit3.txt
2 tom 345
1 rose 235
1 tom 234
2 jim 572
3 rose 123
1 jim 321
2 tom 573
3 jim 876
3 tom 648
1.Profit.java 定义对象并进行排序
package cn.tedu.sortprofit;

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

import org.apache.hadoop.io.WritableComparable;

public class Profit implements WritableComparable {

private int month;
private String name;
private int profit;

public int getMonth() {
	return month;
}

public void setMonth(int month) {
	this.month = month;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getProfit() {
	return profit;
}

public void setProfit(int profit) {
	this.profit = profit;
}

@Override
public void write(DataOutput out) throws IOException {
	out.writeInt(month);
	out.writeUTF(name);
	out.writeInt(profit);
}

@Override
public void readFields(DataInput in) throws IOException {
	this.month = in.readInt();
	this.name = in.readUTF();
	this.profit = in.readInt();
}

// 按照月份进行升序排序
// 如果是同一个月,那么需要按照业绩来进行降序排序
@Override
public int compareTo(Profit o) {

	int r1 = this.month - o.month;
	if (r1 == 0) {
		int r2 = o.profit - this.profit;
		return r2 == 0 ? 1 : r2;
	}

	return r1;

}

@Override
public String toString() {
	return "Profit [month=" + month + ", name=" + name + ", profit=" + profit + "]";
}

}
2.SortProfitMapper.java 数据写入对象
package cn.tedu.sortprofit;

import java.io.IOException;

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

public class SortProfitMapper extends Mapper<LongWritable, Text, Profit, NullWritable> {

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

	String[] arr = value.toString().split(" ");
	Profit p = new Profit();
	p.setMonth(Integer.parseInt(arr[0]));
	p.setName(arr[1]);
	p.setProfit(Integer.parseInt(arr[2]));
	context.write(p, NullWritable.get());

}

}
3.SortProfitReducer.java 合并输出
package cn.tedu.sortprofit;

import java.io.IOException;

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

public class SortProfitReducer extends Reducer<Profit, NullWritable, Profit, NullWritable> {

public void reduce(Profit key, Iterable<NullWritable> values, Context context)
		throws IOException, InterruptedException {
	context.write(key, NullWritable.get());
}

}

4.SortProfitDriver.java 执行
package cn.tedu.sortprofit;

import java.io.IOException;

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

public class SortProfitReducer extends Reducer<Profit, NullWritable, Profit, NullWritable> {

public void reduce(Profit key, Iterable<NullWritable> values, Context context)
		throws IOException, InterruptedException {
	context.write(key, NullWritable.get());
}

}

    原文作者:weixin_44617428
    原文地址: https://blog.csdn.net/weixin_44617428/article/details/101786137
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞