02Hadoop二次排序2
案例:
数据:
邮编 | 日期 |金额
ILMN,2013-12-05,97.65
GOOD,2013-12-09,1078.14
IBM,2013-12-09,177.46
ILMN,2013-12-09,101.33
ILMN,2013-12-06,99.25,
GOOD,2013-12-06,1069.87
IBM,2013-12-06,177.67
GOOD,2013-12-05,1057.34
GOOD,2013-12-05,10.23
GOOD,2013-12-05,11.43
GOOD,2013-12-05,17.34
要求:把同一个邮编的放在一起,然后根据日期和金额降序排列。
效果如下:

思路:在map阶段,构造的key(CompositeKey)是:(邮编,日期);value(NaturalValue)是(日期,价格)。然后key继承
WritableComparable,实现比较函数这样就可以保证一份数据出来是分区且区内有序的。
然后在shuffle过程中,指定一个key比较器(CompositeKeyComparator),使得在聚合过程后,对key按照先邮编,再时间,最后金额的顺序排序,key-value是键值对,key按照我们的意愿排好序了,
value也就排好了。
总的来说:降序什么的都是CompositeKeyComparator来决定的。
代码结构:

(1)key:组合键
package com.book.test1;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
/**
* 这个的作用就是要数据在分区里面有序
*/
/**
* 定义组合键:就是可以把自己要比较的字段写入
* @author Sxq
*
*/
//必须要时间这个WritableComparable这个类
public class CompositeKey implements Writable, WritableComparable<CompositeKey> { // 股票的名字
private Text stockSymbol;
// 日期
private LongWritable timestamp;
private DoubleWritable price; public DoubleWritable getPrice() {
return price;
}
public void setPrice(DoubleWritable price) {
this.price = price;
}
public CompositeKey()
{ }
public CompositeKey(Text _stockSymbol, LongWritable _timestamp,DoubleWritable _price) {
this.stockSymbol = _stockSymbol;
this.timestamp = _timestamp;
this.price=_price;
} public Text getStockSymbol() {
return stockSymbol;
} public void setStockSymbol(Text stockSymbol) {
this.stockSymbol = stockSymbol;
} public LongWritable getTimestamp() {
return timestamp;
} public void setTimestamp(LongWritable timestamp) {
this.timestamp = timestamp;
} //读出
public void readFields(DataInput input) throws IOException {
String value1=input.readUTF();
long value2=input.readLong();
this.stockSymbol=new Text( value1);
this.timestamp= new LongWritable(value2);
this.price=new DoubleWritable(input.readDouble());
} //写入 //@Override
public void write(DataOutput output) throws IOException {
output.writeUTF(this.stockSymbol.toString());
output.writeLong(this.timestamp.get());
output.writeDouble(this.price.get());
} public int compareTo(CompositeKey other) { int comparator=this.stockSymbol.compareTo(other.stockSymbol);
if(comparator==0)
{
comparator=this.timestamp.compareTo(other.timestamp);
} //升序
//return comparator; return -comparator;
} @Override
public String toString() {
return "CompositeKey [stockSymbol=" + stockSymbol + ", timestamp=" + timestamp + "]";
} }
(2)key对应的value:
package com.book.test1; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable; public class NaturalValue implements Writable {
private long timestamp;
private double privce; public long getTimestamp() {
return timestamp;
} public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
} public double getPrivce() {
return privce;
} public void setPrivce(double privce) {
this.privce = privce;
} public void readFields(DataInput input) throws IOException {
this.timestamp=input.readLong();
this.privce=input.readDouble(); } public void write(DataOutput output) throws IOException { output.writeLong(this.timestamp);
output.writeDouble(this.privce); } }
(3)分区器:
NaturalKeyPartitioner
package com.book.test1; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
/**
* 分区:按照邮编分,把邮编相同的放在一起
* @author Sxq
*/ public class NaturalKeyPartitioner extends Partitioner<CompositeKey, NaturalValue> { @Override
public int getPartition(CompositeKey key, NaturalValue value, int numPartitions) {
return Math.abs((int)(key.getStockSymbol().hashCode())%numPartitions);
} }
(4)把key排序的比较器:在shuffle过程中用到的
package com.book.test1; import javax.print.attribute.standard.MediaSize.Other; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 这个类的作用是把组合键排序,使得组合键也有顺序
* @author Sxq
*
*/
public class CompositeKeyComparator extends WritableComparator { public CompositeKeyComparator() {
super(CompositeKey.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
CompositeKey ck1 = (CompositeKey) a;
CompositeKey ck2 = (CompositeKey) b;
int comparison = ck1.getStockSymbol().compareTo(ck2.getStockSymbol());
//如果邮编相同,则根据日期进一步处理。
if (comparison == 0) { int comparison2=ck1.getTimestamp().compareTo(ck2.getTimestamp());
// 如果日期相同,则需要根据价格进一步处理
if (comparison2==0) {
//按照价格降序
return ck1.getPrice().compareTo(ck2.getPrice())>0?-1:1; } else {
//日期不同,就按照日期降序
return ck1.getTimestamp().compareTo(ck2.getTimestamp())>0?-1:1;
}
}
else {
return comparison;
}
}
static {
WritableComparator.define(CompositeKey.class, new CompositeKeyComparator());
} }
(5)reduce的分区器:
CompositeGroupingComparator
package com.book.test1; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 分组:就是在reduce阶段分到一个组;
* 就是邮编相同的放在一个组里面
* @author Sxq
*
*/
public class CompositeGroupingComparator extends WritableComparator{ public CompositeGroupingComparator() { super(CompositeKey.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
CompositeKey v1=(CompositeKey)a;
CompositeKey v2=(CompositeKey)b; return v1.getStockSymbol().compareTo(v2.getStockSymbol()); } }
(6)驱动类:
package com.book.test1; import java.io.IOException;
import java.util.Date;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
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; public class Cmain {
static class Map1 extends Mapper<LongWritable, Text, CompositeKey, NaturalValue> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, CompositeKey, NaturalValue>.Context context)
throws IOException, InterruptedException {
String line = value.toString().trim();
String[] lines = line.split(",");
Date date = DateUtil.getDate(lines[1]);
//long timestamp = date.getTime(); long timestamp=UtilsCmain.DataTranform(lines[1]);
CompositeKey compositeKey = new CompositeKey();
NaturalValue naturalValue = new NaturalValue();
naturalValue.setPrivce(Double.valueOf(lines[2]));
naturalValue.setTimestamp(timestamp);
compositeKey.setStockSymbol(new Text(lines[0]));
compositeKey.setPrice(new DoubleWritable(Double.valueOf(lines[2])));
compositeKey.setTimestamp(new LongWritable(timestamp));
context.write(compositeKey, naturalValue);
} } static class reduce1 extends Reducer<CompositeKey, NaturalValue, Text, Text> {
@Override
protected void reduce(CompositeKey key, Iterable<NaturalValue> vlaue,
Reducer<CompositeKey, NaturalValue, Text, Text>.Context context) throws IOException, InterruptedException { Iterator<NaturalValue> iterator = vlaue.iterator();
StringBuffer stringBuffer = new StringBuffer();
while (iterator.hasNext()) {
NaturalValue naturalValue=iterator.next();
stringBuffer.append("(");
stringBuffer.append(naturalValue.getTimestamp());
stringBuffer.append(","+naturalValue.getPrivce()+")");
} context.write(new Text(key.getStockSymbol()), new Text(stringBuffer.toString()));
}
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(Cmain.class); job.setMapperClass(Map1.class);
job.setReducerClass(reduce1.class); job.setMapOutputKeyClass(CompositeKey.class);
job.setMapOutputValueClass(NaturalValue.class); job.setOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class); job.setSortComparatorClass(CompositeKeyComparator.class);
// 在Reduce端设置分组,使得同一个邮编的在同一个组
job.setGroupingComparatorClass(CompositeGroupingComparator.class);
// 设置分区
job.setPartitionerClass(NaturalKeyPartitioner.class); // 指定输入的数据的目录
FileInputFormat.setInputPaths(job, new Path("/Users/mac/Desktop/stock.txt")); FileOutputFormat.setOutputPath(job, new Path("/Users/mac/Desktop/flowresort")); boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1); } }
(7)工具类:将2012-12-09转为20121209这种形式:
package com.book.test1;
public class UtilsCmain {
/**
* 时间
*/
public static long DataTranform(String vaule)
{
String[] args=vaule.split("-");
String datatime=args[0]+args[1]+args[2];
return Long.valueOf(datatime);
}
}
运行结果:

02Hadoop二次排序2的更多相关文章
- MapReduce二次排序
默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...
- Hadoop Mapreduce分区、分组、二次排序过程详解[转]
原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2) ...
- Hadoop.2.x_高级应用_二次排序及MapReduce端join
一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...
- Hadoop学习笔记: MapReduce二次排序
本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...
- Spark基础排序+二次排序(java+scala)
1.基础排序算法 sc.textFile()).reduceByKey(_+_,).map(pair=>(pair._2,pair._1)).sortByKey(false).map(pair= ...
- (转)MapReduce二次排序
一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...
- MapReduce自定义二次排序流程
每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...
- Hadoop MapReduce 二次排序原理及其应用
关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...
- hadoop2.2编程:mapreduce编程之二次排序
mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...
随机推荐
- ionic2 (真正)修改应用图标和启动画面
今天在用ionic2 的ionic resources生成新的icon和splash,生成后安装,应用图标和启动画面依然没变化... 不知道大家有没有被坑过,今天被坑了一下午,终于找到了办法: 解决方 ...
- bootstrap常见的面试题
1. 如果让一个元素在pc端显示而在手机端隐藏,下列选项正确的是(b). A. visible-xs-8 hidden-md B. visible-md-8 hidden-xs C. visibl ...
- MediaInfo代码阅读
MediaInfo是一个用来分析媒体文件的开源工具. 支持的文件非常全面,基本上支持所有的媒体文件. 最近是在做HEVC开发,所以比较关注MediaInfo中关于HEVC的分析与处理. 从Meid ...
- 制作chrome插件/扩展程序,禁止谷歌浏览器访问某些网站
简单地说,浏览器插件,可以大大的扩展你的浏览器的功能.包括但不仅限于这些功能: 捕捉特定网页的内容 捕捉HTTP报文 捕捉用户浏览动作,改变浏览器地址栏/起始页/书签/Tab等界面元素的行为 与别的站 ...
- 关于EOF的使用的好文章
Linux shell脚本EOF妙用 https://blog.csdn.net/zongshi1992/article/details/71693045
- Android 获取Activity当前view
View cv = getWindow().getDecorView(); 来自为知笔记(Wiz)
- TensorFlow实战Google深度学习框架1-4章学习笔记
目录 第1章 深度学习简介 第2章 TensorFlow环境搭建 第3章 TensorFlow入门 第4章 深层神经网络 第1章 深度学习简介 对于许多机器学习问题来说,特征提取不是一件简单的事情 ...
- html学习笔记之2——多媒体
一:插件 插件可以通过 <object> 标签或者 <embed> 标签添加在页面中. <object width="400" height=&quo ...
- 读取mysql数据库的数据,转为json格式
# coding=utf-8 ''' Created on 2016-10-26 @author: Jennifer Project:读取mysql数据库的数据,转为json格式 ''' import ...
- 单片机成长之路(51基础篇) - 004 STC89C52MCU 软件实现系统复位
用户应用程序在运行过程中,有时会有特殊需求,需要实现单片机系统复位(热启动之一),传统的8051单片机由于硬件上未支持此功能,用户必须用软件模拟实现,实现起来较麻烦.STC单片机增加了相应的硬件功能, ...