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

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());
}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值