MapReduce实现倒序排序的两种方法

目录

一、外部比较器实现

        二、自定义序列化比较器实现


一、外部比较器实现

需要注意,这里的比较是对MapReduce中key类型的比较,并且key的类型为InWritable类型。

1、自定义比较器继承WritableComparator类

package com.gxwz.utils;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * TODO 	 IntWritable类的倒叙排序
 * @author   administrator
 * @Date	 2019年10月7日 	 
 */
public class IntWritableComparator extends WritableComparator {

	/*
	 * 重写构造方法,定义比较类 IntWritable
	 */
	public IntWritableComparator() {
		super(IntWritable.class, true);
	}
	/*
	 * 重写compare方法,自定义比较规则
	 */
	@Override
	public int compare(WritableComparable a, WritableComparable b) {
		//向下转型
		IntWritable ia = (IntWritable) a;
		IntWritable ib = (IntWritable) b;
		return ib.compareTo(ia);
	}
}

2、job设置比较器

//Job job = Job.getInstance(conf, "DescSort");
job.setSortComparatorClass(IntWritableComparator.class);

二、自定义序列化比较器实现

1、自定义序列化类,实现WritableComparable接口

package com.gxwz.entity;

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

import org.apache.hadoop.io.WritableComparable;
/**
 * TODO 各地区年收入信息
 * @author   com
 * @Date	 2019年10月3日 	 
 */
public class AnnualIncome implements WritableComparable<AnnualIncome>{

	private String workerId = "";	//id
	private String area = "";		//地区
	private String date = "";		//时间
	private String amount = "";		//收入
	public String getWorkerId() {
		return workerId;
	}
	public void setWorkerId(String workerId) {
		this.workerId = workerId;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		this.amount = amount;
	}
	
	@Override
	public String toString() {
		return workerId + "\t" + area + "\t" + date + "\t" + amount;
	}
	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(workerId);
		out.writeUTF(area);
		out.writeUTF(date);
		out.writeUTF(amount);
	}
	@Override
	public void readFields(DataInput in) throws IOException {
		this.workerId = in.readUTF();
		this.area = in.readUTF();
		this.date = in.readUTF();
		this.amount = in.readUTF();
	}
	@Override
	public int compareTo(AnnualIncome o) {
		return this.workerId.compareTo(o.workerId);
	}
}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MapReduce是一种分布式计算框架,可以用于对大规模数据进行排序。以下是MapReduce实现数据排序的步骤: 1.编写Map函数和Reduce函数,其中Map函数将输入数据转换为键值对,Reduce函数将键值对按照键进行排序并输出。 2.设置MapReduce作业的输入路径和输出路径。 3.设置MapReduce作业的Mapper类和Reducer类。 4.设置MapReduce作业的输出键值对类型。 5.设置MapReduce作业的排序方式,可以选择部分排序、全排序、辅助排序或二次排序等方式。 6.运行MapReduce作业并等待作业完成。 以下是一个简单的MapReduce排序示例: ```java // 设置Map函数 public static class Map extends Mapper<LongWritable, Text, IntWritable, IntWritable> { private final static IntWritable one = new IntWritable(1); private IntWritable data = new IntWritable(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); data.set(Integer.parseInt(line)); context.write(data, one); } } // 设置Reduce函数 public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { private IntWritable lineNumber = new IntWritable(1); public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for (IntWritable val : values) { context.write(lineNumber, key); lineNumber = new IntWritable(lineNumber.get() + 1); } } } // 设置MapReduce作业 Job job = new Job(conf, "sort"); job.setJarByClass(Sort.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); // 运行MapReduce作业 job.waitForCompletion(true); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日月星辰TEL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值