数据源

A
B
C
D
Z

要实现的输出

Z
D
B
C
A

看字符顺序,其实什么也没有,只是按照后面的数字进行一次倒序排序,实现思路,1利用hadoop自带的排序功能,2.KV互换

实现代码

public class SVJob {
public static void main(String[] args) throws IOException,
InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.9.181:9001");
String[] ars = new String[] {
"hdfs://192.168.9.181:9000/user/hadoop/input/examples/SortByValue/",
"hdfs://192.168.9.181:9000/user/hadoop/output/examples/SortByValue" };
String[] otherArgs = new GenericOptionsParser(conf, ars)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("SortByValue: <in> <out>");
System.exit(2);
} Job job = new Job(conf, "SortByValue");
job.setJarByClass(SVJob.class);
job.setMapperClass(SVMapper.class);
job.setReducerClass(SVReducer.class); job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setSortComparatorClass(IntWritableDecreasingComparator.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
public class SVMapper extends Mapper<Object, Text, IntWritable, Text> {
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] keyValueStrings = line.split("\t");
if(keyValueStrings.length != 2)
{
//新手,不知道怎么记录日志,也不清楚怎么退出 各位大神如果知道请通知我,谢谢
System.err.println("string format error!!!!!");
return;
}
int outkey = Integer.parseInt(keyValueStrings[1]);
String outvalue = keyValueStrings[0];
context.write(new IntWritable(outkey), new Text(outvalue));
}
}
public class SVReducer extends Reducer<IntWritable, Text, Text, IntWritable> {
protected void reduce(IntWritable key, Iterable<Text> values,Context context)throws IOException, InterruptedException {
for(Text value : values){
context.write(value, key);
}
}
}

因为我们要实现倒序排序要有自定义的排序方法

public class IntWritableDecreasingComparator extends Comparator {
@SuppressWarnings("rawtypes")
public int compare( WritableComparable a,WritableComparable b){
return -super.compare(a, b);
}
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return -super.compare(b1, s1, l1, b2, s2, l2);
}
}

这样就完成了,可以自定义排序了

Hadoop 实现对Value倒序排序的更多相关文章

  1. 用sort实现对struct的排序

    用sort 排序 struct +++ //method 1 struct node{ int k,s; }p[5005]; bool cmp1(node x,node y){ return x.s& ...

  2. C#代码实现对HTTP POST参数进行排序

    private static string GetSortedParas(Dictionary<string, string> dic) { dic = dic.OrderBy(key = ...

  3. C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客

    C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客 C++中实现对map按照value值进行排序 2012-03-15 15:32:36 标签:map 职场 休闲 排 ...

  4. 使用泛型实现对int数组或者String数组进行排序

    因为是使用的泛型,我们并不确定数据类型, 对于数据的比较就不能用平时的大于或者小于. 我们需要比较对象实现Comparable接口,该接口下的compareTo()方法可以用来比大小 定义Sort类: ...

  5. 使用代理实现对C# list distinct操作

    范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...

  6. 实现对DataGird控件的绑定操作

    //实现对DataGird控件的绑定操作 function InitGrid(queryData) { $('#grid').datagrid({ //定位到Table标签,Table标签的ID是gr ...

  7. 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理

    http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...

  8. 在VS2015中用C++创建DLL并用C#调用且同时实现对DLL的调试

    from:http://m.blog.csdn.net/article/details?id=51075023 在VS2015中先创建C#项目,然后再创建要编写的动态库DLL项目,这样做的好处是整个解 ...

  9. 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】

    一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...

随机推荐

  1. DCI

    理论: 某个data,在一个特定的场景中,以某个角色role,来与该场景中的其他角色进行交互.这个过程要以代码的方式表达出来,他要求data本身不具备交互行为, 有交互行为的是角色,当一个data没有 ...

  2. MVC中的过滤器

    authour: chenboyi updatetime: 2015-05-09 09:30:30 friendly link:   目录: 1,思维导图   2,过滤器种类(图示) 3,全局过滤器 ...

  3. iTunes 安装终极解决方案

    近日手贱升级了Itunes,升级过程即报失败,然后卸载所有相关东西,再重装,Itunes安装成功,但是报告无法使用iphone,经过几天摸索,发现是Apple Mobile Device Suppor ...

  4. java.util.concurrent并发包诸类概览

    java.util.concurrent包的类都来自于JSR-166:Concurrent Utilities,官方的描述叫做“The JSR proposes a set of medium-lev ...

  5. keil C51绝对地址访问

    在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,如DA AD 采样 ,LCD 液晶操作,打印操作.等等.C51提供了三种访问绝对地址的方法: 1. 绝对宏 在 ...

  6. 7.1 Backup and Recovery Types 备份和恢复类型

    7.1 Backup and Recovery Types 备份和恢复类型 这个章节描述 不同备份类型的特点: 物理(raw)与逻辑备份 物理备份有raw 副本组成,存储数据库内容,这种类型的备份是适 ...

  7. 【HDOJ】2809 God of War

    状态DP. /* 2809 */ #include <iostream> #include <queue> #include <cstdio> #include & ...

  8. Codeforces Round #312 (Div. 2) C.Amr and Chemistry

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  9. 关于fork函数

    这篇文章说得非常好.做个记录: 链接:http://coolshell.cn/articles/7965.html

  10. gcc编译命令

    g++ demo.cpp -o demo.exe