数据源

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. Testlink接口使用方法-python语言远程调用

    deepin@deepin-pc:~/test$ cat libclienttestlink.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- #! ...

  2. sql server 调优----索引缺失

    SELECT mig.index_group_handle, mid.index_handle, CONVERT (decimal (28,1), migs.avg_total_user_cost * ...

  3. 在win8.1 64位环境下有关Oracle的安装和卸载

    1,Oracle安装 3 注意:在win8.1环境下安装64位的oracle客户端,注意配置是1g的 2.Oracle的卸载:http://jingyan.baidu.com/article/f7ff ...

  4. Python里的拷贝=====》很容易错误的

    不能直接用 = 复制: import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy.copy(a) #对象拷贝,浅 ...

  5. poi简单案例

    //poi api 操作 public static void main(String[] args) {  // TODO Auto-generated method stub  // 创建一个工作 ...

  6. C#泛型总结

    泛型方法 在C#2.0中,方法可以定义特定于其执行范围的泛型参数,如下所示: public class MyClass<T>     {         //指定MyMethod方法用以执 ...

  7. 001Spring4.2基本环境搭建

    1:工程目录以及依赖jar包如下,如果缺少某些jar包在weblogic控制台下面会有提示 2:applicationContext.xml配置文件 <?xml version="1. ...

  8. Intra Chroma Prediction

    帧内预测依赖于当前宏块的相邻宏块,如果任何一个相邻宏块不可用,那么会直接影响到当前宏块的预测方式. 那么宏块怎么才谓之可用? 满足以下几个条件的相邻宏块为不可用: 相邻宏块超出边界,即(x<0 ...

  9. flex中validateall()方法, 多Item验证 ,结果统一提示

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  10. BZOJ 1003 [ZJOI2006]物流运输trans

    1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4242  Solved: 1765[Submit] ...