数据源

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. MongoDB-固定集合 capped collection 操作 介绍

    固定集合:capped collection 是性能出色的固定大小的集合,以LRU算法淘汰记录,自助维护集合中的对象的插入顺序,创建时预先制定大小,空间使用完,心对象取代旧的对象,保持最新的数据. 可 ...

  2. es6整理

    1.const和let let类似于var,不同的是let只在所在的代码段有效 for循环的计数器,就很合适使用let命令. let和var的区别: //变量i是var声明的,在全局范围内都有效.所以 ...

  3. URL加载系统----iOS工程师必须熟练掌握

    URL加载系统----iOS工程师必须熟练掌握     iOS根本离不开网络——不论是从服务端读写数据.向系统分发计算任务,还是从云端加载图片.音频.视频等.   当应用程序面临处理问题的抉择时,通常 ...

  4. PartialFunction(偏函数)

    val one:PartialFunction[Int,String]={ case 1 => "one" case 2 => "two" case ...

  5. 关于pthread里面一些函数的使用心得!

    第一次使用pthread,遇到的问题还真不少,现在我一一记录一下: 1.关于编译时出现 对‘pthread_create’未定义的引用 之类的错误的解决:由于pthread库不是Linux系统默认的库 ...

  6. java数据安全(一)

    在java开发中,常常需要考虑数据安全问题,然不仅仅需要考虑数据存储的安全,还需要考虑数据的传输安全.自从有了数据安全问题后,密码学也就出来了.了解数据安全,首先需要了解密码学 1.什么是密码学 密码 ...

  7. How to Install Linux, Apache, MySQL, PHP (LAMP) stack on CentOS 6 【Reliable】

    About LAMP LAMP stack is a group of open source software used to get web servers up and running. The ...

  8. HBase MemStoreFlusher

    HBase MemStore Flush由类org.apache.hadoop.hbase.regionserver.MemStoreFlusher实现,具体表现为HRegionServer中的一个实 ...

  9. lr 自带的例子,如何进行关联,通过代码的函数进行实现

    本篇主要介绍如何来进行把参数进行关联,首先对web tours进行设定 如下图 点击“administration”j进入跳转页面,如下图所示 勾选第三项,下拉下方,点击“update”按钮, 关闭浏 ...

  10. tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:

    错误 严重: Error configuring application listener of class org.springframework.web.context.ContextLoader ...