按照k2排序,要求k2必须是可以比较的,即必须实现WritableComparable接口。

但是如果还想让别的字段(比如v2中的一些字段)参与排序怎么办?

需要重新定义k2....把需要参与排序的字段都放到k2中.

这块用代码实现:

假如数据现在的结构是

3       3

3       2

3       1

2       2

2       1

1       1

看代码:

 import java.io.DataInput;
import java.io.DataOutput;
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.io.WritableComparable;
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 TwoIntSortApp { public static void main(String[] args) throws Exception {
Job job = Job.getInstance(new Configuration(), TwoIntSortApp.class.getSimpleName());
job.setJarByClass(TwoIntSortApp.class);
FileInputFormat.setInputPaths(job, args[0]); job.setMapperClass(TwoIntSortMapper.class);
job.setMapOutputKeyClass(TwoInt.class);
job.setMapOutputValueClass(NullWritable.class); job.setReducerClass(TwoIntSortReducer.class);
job.setOutputKeyClass(TwoInt.class);
job.setOutputValueClass(NullWritable.class); FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
} public static class TwoIntSortMapper extends Mapper<LongWritable, Text, TwoInt, NullWritable>{
TwoInt k2 = new TwoInt();
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, TwoInt, NullWritable>.Context context)
throws IOException, InterruptedException {
String[] splited = value.toString().split("\t");
k2.set(splited[0],splited[1]);
context.write(k2, NullWritable.get());
System.out.println("Mapper-----第一个数:"+k2.first+" 第二个数:"+k2.second);
}
} public static class TwoIntSortReducer extends Reducer<TwoInt, NullWritable, TwoInt, NullWritable>{
int i=1;
@Override
protected void reduce(TwoInt k2, Iterable<NullWritable> arg1,
Reducer<TwoInt, NullWritable, TwoInt, NullWritable>.Context context)
throws IOException, InterruptedException {
context.write(k2,NullWritable.get());
System.out.println("调用次数"+(i++));
System.out.println("Reducer-----第一个数:"+k2.first+" 第二个数:"+k2.second);
}
} public static class TwoInt implements WritableComparable<TwoInt>{
int first;
int second;
public void write(DataOutput out) throws IOException {
out.writeInt(first);
out.writeInt(second);
} public void set(String s1,String s2){
this.first = Integer.parseInt(s1);
this.second = Integer.parseInt(s2);
} public void readFields(DataInput in) throws IOException {
this.first = in.readInt();
this.second = in.readInt(); } public int compareTo(TwoInt o) {
int r1 = this.first - o.first;
if(r1 < 0){
return -1;
}else if(r1 > 0){
return 1;
}
int r2 = this.second - o.second;
return (r2 < 0 ? -1 : (r2 > 0 ? 1 : 0));
} @Override
public String toString() {
return this.first+"\t"+this.second;
}
}
}

//==============================================================

在job上设置Combiner类...

        job.setCombinerClass(TwoIntSortReducer.class);//设置Combiner类
job.setGroupingComparatorClass(MyGroupingCompartor.class);//设置自定义的分组类
     public static class MyGroupingCompartor extends WritableComparator{
@Override
public int compare(WritableComparable a, WritableComparable b) {
TwoInt aa = (TwoInt)a;
TwoInt bb = (TwoInt)b;
return aa.first-bb.first<0?-1:(aa.first-bb.first>0?1:0);//只要是第一列相同的就认为是一个分组.
/*
* 1 1
* 2 1
* 2 2
* 3 1
* 3 2
* 3 3
* 这样就分成了三组
*/
}
}

MapReduce按照两个字段对数据进行排序的更多相关文章

  1. mysql的if用法解决同一张数据表里面两个字段是否相等统计数据量。

    MySQL的使用用法如下所示:格式:if(Condition,A,B)意义:当Condition为true时,返回A:当Condition为false时,返回B.作用:作为条件语句使用.mysql的i ...

  2. 连接两个点云中的字段或数据形成新点云以及Opennni Grabber初识

    (1)学习如何连接两个不同点云为一个点云,进行操作前要确保两个数据集中字段的类型相同和维度相等,同时了解如何连接两个不同点云的字段(例如颜色 法线)这种操作的强制约束条件是两个数据集中点的数目必须一样 ...

  3. sql server中如何将两个字段数据合并成一个字段显示(字段与字段添加特殊符号)

    之前,我在做统计数据时,需要一个字段显示某月的订单数量和订单金额,要求组合成一个字段,用括号组合. 统计出来的结果大概是这样的,首先我们来创建一些模拟数据 ---创建订单表--- create tab ...

  4. mysql如何让两个字段数据都不能重复?

    目录 场景 任务(需求) 行动(解决方案) 方案1:从代码层面解决(正确方案) 方案2:设置成两个唯一索引(正确方案) 方案3:删掉中间表,把从表的主键作为主表的外键,并将外键设置成唯一索引(正确方案 ...

  5. 一个表的两个字段具有相同的类型。如何仅用SQL语句交换这两列的数据?

    --假设为A B两个字段--查询Select A As B, B As A From TableName --更新Update TableName Set A = B, B = A

  6. Python实现MapReduce,wordcount实例,MapReduce实现两表的Join

    Python实现MapReduce 下面使用mapreduce模式实现了一个简单的统计日志中单词出现次数的程序: from functools import reduce from multiproc ...

  7. MapReduce实现两表的Join--原理及python和java代码实现

    用Hive一句话搞定的,可是有时必需要用mapreduce 方法介绍 1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是很常见且很耗时的.而在HADOOP中进行JOIN操作.相同常见且耗时, ...

  8. MySQL为数据表的指定字段插入数据

    username not null 没有默认值/有默认值   insert不插入username字段 均不报错 2014年07月23日21:05    百科369 MySQL为数据表的指定字段插入数据 ...

  9. 选择两个字段时distinct位置的影响

    当选择两个字段时,例如:"select XX1, XX2 from tb; ",那么将distinct放在前一个字段XX1之前和放在后一个字段XX2之前,结果有什么不同呢? 先说结 ...

随机推荐

  1. POJ 1797 Heavy Transportation (dijkstra 最小边最大)

    Heavy Transportation 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Backgro ...

  2. codeforces 624B Making a String

    Making a String time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. Java 8 正式发布,新特性全搜罗

    经过2年半的努力.屡次的延期和9个里程碑版本,甲骨文的Java开发团队终于发布了Java 8正式版本. Java 8版本最大的改进就是Lambda表达式,其目的是使Java更易于为多核处理器编写代码: ...

  4. Listener 监听器

    Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listener ...

  5. spring事务注解

    @Transactional只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能. Spring使用声明式事务处理,默认 ...

  6. SSL协议与数字证书原理

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  7. flash的dragonbone插件导入cocos2d的注意事项

    一:Flash版本号应该为CS 6.0,低版本号不提供支持 二:新建flash项目的时候应该选择ActionScript3.0 三:动画中仅仅有两种元素,一个是"元件",还有一个是 ...

  8. 参数对象Struts2中Action的属性接收参数

    题记:写这篇博客要主是加深自己对参数对象的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. Action中三种传递并接受参数: 1.  在Action添加成员属性接受参数 例如请求的 ...

  9. spring中的ResourceBundleMessageSource

    1 首先创建两个资源文件    messages_en_US.properties customer.name=Yong Mook Kim, age : {0}, URL : {1} messages ...

  10. [Ramda] Simple log function for debugging Compose function

    const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...