zoukankan      html  css  js  c++  java
  • Hadoop 实现对Value倒序排序

    数据源

    A    2
    B    9
    C    4
    D    9
    Z    42

    要实现的输出

    Z    42
    D    9
    B    9
    C    4
    A    2

    看字符顺序,其实什么也没有,只是按照后面的数字进行一次倒序排序,实现思路,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("	");
            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);
        }
    }

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

  • 相关阅读:
    Windows Phone 8 Wallet 手机钱包 / 电子钱包
    Windows Phone 8 In app purchase 应用内购买 / 应用内支付
    Windows Phone 8 适应多屏分辨率
    Windows phone 8 基于定位的后台应用
    Windows Phone 8 Nokia地图控件
    Windows Phone 8 MDIL编译与代码混淆工具
    Windows Phone 8 近场通信 NFC / Bluetooth Proximity
    Windows Phone 8 镜头应用 Lenses for Windows Phone 8
    Windows Phone 8 与 windows 8 开发技术概览
    嵌入式成长轨迹54 【Zigbee项目】【CC2430基础实验】【系统睡眠工作状态】
  • 原文地址:https://www.cnblogs.com/booth/p/3274593.html
Copyright © 2011-2022 走看看