MapReduce- 数据的排序处理

package com.huhu.day02;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.WritableComparable; /**
* 6 9
* 3 8
* 4 8
* 1 0
* 3 0
* 8 8
* 6 7
* 第一列升序,第二列降序
* @author huhu_k
*
*/
public class Number implements WritableComparable<Number> { private int first;
private int second; // private int third;
public Number() {
super();
} public Number(int first, int second) {
super();
this.first = first;
this.second = second;
} public int getFirst() {
return first;
} public void setFirst(int first) {
this.first = first;
} public int getSecond() {
return second;
} public void setSecond(int second) {
this.second = second;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + first;
result = prime * result + second;
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Number other = (Number) obj;
if (first != other.first)
return false;
if (second != other.second)
return false;
return true;
} @Override
public String toString() {
return "Number [first=" + first + ", second=" + second + "]";
} @Override
public void readFields(DataInput in) throws IOException {
this.first = in.readInt();
this.second = in.readInt();
} @Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.first);
out.writeInt(this.second);
} @Override
public int compareTo(Number o) {
if (this.first== o.first) {
//第二行数据降序
return o.second - this.second;
}
//第一行升序
return this.first - o.first;
} }
package com.huhu.day02;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class NumericSorting extends ToolRunner implements Tool { public static class MyMapper extends Mapper<LongWritable, Text, Number, NullWritable> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split(" ");
Number number = null;
if (line.length == 2) {
number = new Number(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
}
context.write(number, NullWritable.get());
}
} public static class MyReduce extends Reducer<Number, NullWritable, Number, Text> {
@Override
protected void reduce(Number key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException {
for (NullWritable n : values) {
context.write(key, new Text("---"));
}
}
} @Override
public Configuration getConf() {
return new Configuration();
} @Override
public void setConf(Configuration arg0) { } @Override
public int run(String[] other) throws Exception { Job job = Job.getInstance(getConf(), "NumbericSorting");
job.setJarByClass(NumericSorting.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Number.class);
job.setMapOutputValueClass(NullWritable.class); job.setReducerClass(MyReduce.class);
job.setOutputKeyClass(Number.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(other[0]));
FileOutputFormat.setOutputPath(job, new Path(other[1])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] other = new GenericOptionsParser(conf, args).getRemainingArgs();
if (other.length != 2) {
System.out.println("your input args number is fail,you need input <in> and <out>");
System.exit(0);
}
ToolRunner.run(conf, new NumericSorting(), other);
}
}

运行结果:

MapReduce- 数据的排序处理的更多相关文章

  1. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

  2. MapReduce二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  3. (转)MapReduce二次排序

    一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...

  4. Hadoop MapReduce 二次排序原理及其应用

    关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...

  5. 关于MapReduce二次排序的一点解答

    上一篇博客说明了怎么自定义Key,而且用了二次排序的例子来做测试,但没有详细的说明二次排序,这一篇说详细的说明二次排序,为了说明曾经一个思想的误区,特地做了一个3个字段的二次排序来说明.后面称其为“三 ...

  6. mapreduce 实现数子排序

    设计思路: 使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String ...

  7. 详细讲解MapReduce二次排序过程

    我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...

  8. MapReduce 二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  9. Spark 颠覆 MapReduce 保持的排序记录

    在过去几年,Apache Spark的採用以惊人的速度添加着,通常被作为MapReduce后继,能够支撑数千节点规模的集群部署. 在内存中数 据处理上,Apache Spark比MapReduce更加 ...

  10. mapreduce数据处理——统计排序

    接上篇https://www.cnblogs.com/sengzhao666/p/11850849.html 2.数据处理: ·统计最受欢迎的视频/文章的Top10访问次数 (id) ·按照地市统计最 ...

随机推荐

  1. 传输SO10 (SO10 Transport)

    传输SO10 (SO10 Transport) 方法一.   手工添加到请求里面,格式为: R3TR TEXT text object, name, ID, language   方法二.使用程序:R ...

  2. HDU 4315 Climbing the Hill(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=4315 题意:由上至下有多个格子,最顶端的是山顶,有多个球,其中有一个球是king,每次可以将球向上移动任意个格子 ...

  3. ImgQuoteUIWindow

    using System;using UnityEngine;using UnityEngine.UI;using UnityEditor;using System.Collections;using ...

  4. “AI”项目日记

    前言:为了更好的以“实践”巩固“学习”,利用空余时间,打造一个属于自己的项目 项目目标: 1.将学习的知识用项目实践,在实践过程中去领悟新的知识 2.高度自由,根据不同时期的学习目标,融入项目中去用代 ...

  5. scala函数式编程(一)

    scala函数编程特点: 1.Scala函数使用命名参数: 即函数参数传递的实参与函数名相对应,与函数位置不对应. object Test { def main(args: Array[String] ...

  6. typeScript入门基础 (1)

    1.ts是js的超集,可使用es5,es6的代码 2. ts的安装与编译: a.  首先需要Node.js环境 .  相信都有,略过. 不会的请百度,或者留言. b.  npm  install  - ...

  7. cookie被禁用,如何使用session

    转载自:https://blog.csdn.net/ai_shuyingzhixia/article/details/80778183 1.禁止使用cookie response.encodeURL( ...

  8. openstack环境搭建常用命令

    1,编辑/etc/selinux/config文件,关闭selinux SELINUX=disabled 2,清空iptables规则并保存 # iptables -F # service iptab ...

  9. python Django 创建应用

    如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”,如下图 创建视图 在 ...

  10. Python 编程快速上手 第七章总结

    前言 在 Word 软件中,我们总是习惯使用 CMD+F 用来查找和替换文本,但是,正如作者所说: 虽然计算机可以很快地查找文本,但你必须精确地告诉它要找什么. 我们往往想要查找一类文本,比如一段文本 ...