我的目的:

示例:

2012,01,01,35
2011,12,23,-4
2012,01,01,43
2012,01,01,23
2011,12,23,5
2011,4,1,2
2011,4,1,56

结果:

201112 -4,5
20114 2,56
201201 23,35,43



正式实现:

代码结构:

分为以下的步骤:

(1)编写封装类,把上述的字段分装进去。

package com.book.test;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable; public class DataTemperaturePair implements Writable,WritableComparable<DataTemperaturePair> {
//年-月
private Text yearMoth=new Text();
//温度
private IntWritable temperature=new IntWritable();
//日期
private Text day=new Text(); public DataTemperaturePair()
{
}
public Text getYearMoth() {
return yearMoth;
}
public Text getDay() {
return day;
}
public void setDay(Text day) {
this.day = day;
}
public void setYearMoth(Text yearMoth) {
this.yearMoth = yearMoth;
}
public IntWritable getTemperature() {
return temperature;
}
public void setTemperature(IntWritable temperature) {
this.temperature = temperature;
}
//这俩个函数是必须要写的,不然在reduce端,这个分装类拿不到
public void readFields(DataInput input) throws IOException {
String readuf=input.readUTF(); int readuf3=input.readInt();
String readuf2=input.readUTF();
this.yearMoth=new Text(readuf); this.temperature=new IntWritable(readuf3);
this.day=new Text(readuf2); }
//这俩个函数是必须要写的,不然在reduce端,这个分装类拿不到
public void write(DataOutput output) throws IOException 
{ output.writeUTF(yearMoth.toString()); output.writeInt(temperature.get()); output.writeUTF(day.toString()); } public int compareTo(DataTemperaturePair that) {
int compareValue=this.yearMoth.compareTo(that.yearMoth); if(compareValue==0) {
compareValue=temperature.compareTo(that.temperature);
} //升序
return compareValue;
}

(2)编写分区器

为什么要自定义这个分区器呢?

因为我们的key是自己写的一个对象,我们想按照这个对象里面的Yearmoth来分到一个区。

package com.book.test;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner; /**
* 自定义的分区器
* @author Sxq
*
*/
public class DataTemperaturePartition extends Partitioner<DataTemperaturePair, NullWritable> { @Override
public int getPartition(DataTemperaturePair pair, NullWritable text, int numberOfPartotions) {
return Math.abs(pair.getYearMoth().hashCode()%numberOfPartotions);
} }

(3)编写比较器

决定数据分入到哪个分组

package com.book.test;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class DataTemperatureGroupingComparator extends WritableComparator { public DataTemperatureGroupingComparator() {
super(DataTemperaturePair.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) { DataTemperaturePair v1=(DataTemperaturePair)a;
DataTemperaturePair v2=(DataTemperaturePair)b;
return v1.getYearMoth().compareTo(v2.getYearMoth());
} }

(4)写驱动类

package com.book.test;

import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 com.guigu.shen.flowsun.FlowCountSort;
public class Cmain {
static class mapper1 extends Mapper<LongWritable,Text, DataTemperaturePair, IntWritable>
{
DataTemperaturePair dataTemperaturePair=new DataTemperaturePair();
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, DataTemperaturePair, IntWritable>.Context context)
throws IOException, InterruptedException {
String valuestring=value.toString();
String[] lines=valuestring.split(",");
String yymm=lines[0]+lines[1]; dataTemperaturePair.setYearMoth(new Text(yymm)); IntWritable temparature=new IntWritable(Integer.valueOf(lines[3]));
dataTemperaturePair.setTemperature(temparature);
dataTemperaturePair.setDay(new Text(lines[2])); context.write(dataTemperaturePair, temparature);
} } static class reduce1 extends Reducer<DataTemperaturePair, IntWritable, Text, Text>
{ @Override
protected void reduce(DataTemperaturePair KEY, Iterable<IntWritable> VALUE,
Context context)
throws IOException, InterruptedException {
StringBuffer sortedTemperaturelist=new StringBuffer(); Iterator<IntWritable> iterator=VALUE.iterator(); while(iterator.hasNext())
{ sortedTemperaturelist.append(iterator.next());
sortedTemperaturelist.append(",");
}
context.write(KEY.getYearMoth(), new Text(sortedTemperaturelist.toString())); }
} public static void main(String[] args) throws Exception { Configuration conf=new Configuration();
Job job=Job.getInstance(conf);
job.setJarByClass(Cmain.class);
job.setMapperClass(mapper1.class);
job.setReducerClass(reduce1.class); job.setMapOutputKeyClass(DataTemperaturePair.class);
job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setGroupingComparatorClass(DataTemperatureGroupingComparator.class);
job.setPartitionerClass(DataTemperaturePartition.class); //指定输入的数据的目录
FileInputFormat.setInputPaths(job, new Path("/Users/mac/Desktop/temperature.txt")); FileOutputFormat.setOutputPath(job, new Path("/Users/mac/Desktop/flowresort")); boolean result=job.waitForCompletion(true);
System.exit(result?0:1);
} }

结果:

成功了

01Hadoop二次排序的更多相关文章

  1. MapReduce二次排序

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

  2. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  3. Hadoop.2.x_高级应用_二次排序及MapReduce端join

    一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...

  4. Hadoop学习笔记: MapReduce二次排序

    本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...

  5. Spark基础排序+二次排序(java+scala)

    1.基础排序算法 sc.textFile()).reduceByKey(_+_,).map(pair=>(pair._2,pair._1)).sortByKey(false).map(pair= ...

  6. (转)MapReduce二次排序

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

  7. MapReduce自定义二次排序流程

    每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...

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

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

  9. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

随机推荐

  1. JN5139 zigbee 资料

    JN5139模块是一系列可以使使用者在最短的时间内在最低的成本下实现IEEE802.15.4或ZigBee兼容系统的表贴模块.此款模块减少了用户对于RF板设计和测试框架的昂贵漫长的开发时间.这些模块利 ...

  2. STM32串口usart发送数据

    主函数请直接关注41行到47行代码!! #include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h> #include ...

  3. BZOJ3022 : [Balkan2012]The Best Teams

    将选手和询问按照年龄排序,即可去掉年龄的限制. 将所有选手按水平排序后维护线段树,显然最优解一定是从大到小贪心选择. 线段树上每个节点维护: $g[0/1]:r+1$不选/选的时候,$l$选不选. $ ...

  4. [P2058][NOIP2015]海港 (模拟)

    %%%ADMAN #include<cstdio> using namespace std; int n,tot,now,ans,h; ],k[],a[],sum[]; int main( ...

  5. 关于git分支管理,推送拉取等等

    git推送本地分支到远程分支 场景 有时候我们开发需要开一个分支,这样可以有效的并行开发. 开分支有两种方式: 一种是在远程开好分支,本地直接拉下来; 一种是本地开好分支,推送到远程. 远程先开好分支 ...

  6. js 基本包装类型 String

    为了操作基本类型值,ECMAScript提供了三个特殊的引用类型: Boolean , Number , String 举例: var s1 = "some text"; var ...

  7. Spring使用原生JDBC

    Spring使用原生JDBC 为加深对Spring解耦的理解,本次实验学习用Spring连接JDBC 一.POM配置文件 pom.xml <project xmlns="http:// ...

  8. 三种进程和线程数据共享模块方法Queue》Pipe》manager

    >>>>线程中的queue import threading import queue def f(qq): print("in child",qq.qsi ...

  9. fixed和sticky

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>f ...

  10. pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始

    游戏的虚拟世界中,最让人happy的一个因素就是主角挂了,而且重来,只要restart就行了,不象现实中人的生命只有1次.回顾上节的效果,如果方块向下落时,挡板没接住,整个游戏就跪了: 如果我们希望方 ...