数据源

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. JS小技巧大本事(持续更新)

    1. 复制N个字符 String.prototype.repeat = function(num){ return (new Array(++num)).join(this); } var a = ' ...

  2. border做箭头的例子

    <style> .test{ margin:0 auto; width:100px; height:100px; background:lightskyblue; position:rel ...

  3. C:\WINDOWS\system32\config\systemprofile\Desktop引用了一个不可用的位置

    使用迅雷下载压缩文件到桌面时,下载完毕后,如果直接点击"打开文件",则迅雷会报错: C:\WINDOWS\system32\config\systemprofile\Desktop ...

  4. Java中抽象类和接口的用法和区别

    一.抽象类 1.抽象类 包含一个抽象方法的类就是抽象类 2.抽象方法 声明而未被实现的方法,抽象方法必须使用abstract关键词字声明 public abstract class People { ...

  5. C语言学习--可变数组

    #ifndef _ARRAY_ #define _ARRAY_ typedef struct { int *array; int size; } Array; //此处定义结构数据类型 Array,虽 ...

  6. java安全HTTPS工具类

    import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; im ...

  7. jquery validate 自定义验证方法 日期验证

    jquery validate有很多验证规则,但是更多的时候,需要根据特定的情况进行自定义验证规则. 这里就来聊一聊jquery validate的自定义验证. jquery validate有一个方 ...

  8. Hadoop Compression

    文件压缩主要有两方面的好处:一方面节省文件存储空间:另一方面加速网络数据传输或磁盘读写.当处理大规模的数据时这些效果提升更加明显,因此我们需要仔细斟酌压缩在Hadoop环境下的使用.   目前已经存在 ...

  9. 【转】HP(惠普)大中华区总裁孙振耀退休感言

    一.关于工作与生活我有个有趣的观察,外企公司多的是25-35岁的白领,40岁以上的员工很少,二三十岁的外企员工是意气风发的,但外企公司40岁附近的经理人是很尴尬的.我见过的40岁附近的外企经理人大多在 ...

  10. Centos6.x/Oracle11G 自动化静默安装配置脚本

    部分脚本截图如下,要想玩转联系Ruiy哥提供脚本下载路径,附件在本博客的文件栏中维护,为了避免懒人一味的索取别人的劳动成果特此如此; 想玩的Ruiy mail to you! 快6.1了,6.1娃子们 ...