我的目的:

示例:

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. mongodb副本集出现的错误 mongodb error: { MongoNetworkError: failed to connect to server [127.0.0.1:1010] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:1010]

    replset = pictureWorks 执行:mongo --port 1008  (查看1008是否是住数据库) rs.status() 执行: onfig={"_id": ...

  2. 数据库出现'\xF0\x9F\x98\xB8'

    https://www.cnblogs.com/jinTaylor/p/4607505.html https://blog.csdn.net/qq_40074764/article/details/7 ...

  3. fatal: You are not currently on a branch. 问题解决

    fatal: You are not currently on a branch. 解决 注:亲试,这种方法会把本地的修改给冲掉,所以事先一定要备份下文档,之后覆盖,重新上传或pull即可:解决方法: ...

  4. Python直接控制鼠标键盘

    Python直接控制鼠标键盘 之前因为期末的原因已经很久没写博客了,今天博主发现一个好玩的模块PyAutoGUI,借助它可以使用Python脚本直接控制键盘鼠标,感觉可以解决很多无聊的机械运动.这里记 ...

  5. 3_主流部署方式介绍-Django+gunicorn+nginx

    下载python wgethttps://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz 报错 Cannot find a valid baseurl ...

  6. Java第一章

    第一章 计算机程序:计算机为完成某些功能产生的一系列有序指令集合. Java技术包括:JavaSE(标准版)  JavaEE(企业版) ---JavaME(移动版) 开发Java程序步骤:1.编写 2 ...

  7. drawRect中抗锯齿

    在开始之前,我们需要创建一个DrawRectView 其初始代码为 // // DrawRectView.h // CGContextSetShouldAntialias // // Created ...

  8. mysql select into 不支持

    不支持的 select * into order_new from orders 改为 Create table order_new(select * from orders)  

  9. Python的ctypes 和pyinstaller

    这几天在学习python的爬虫, 无意中看到一篇博文 Python爬虫之自制英汉字典 发现里面的ctypes 和pyinstaller 还不了解,这边文章说白了就是你输入英文, python读取你的输 ...

  10. centos修改主机名 root@后面的名字

    阿里云买的新的ESC,名字都是一串字符,不利于平时使用.我们可以重命名主机来标记. centos6 [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6 ...