189 8069 5689

mapreduce中怎么实现二次排序

mapreduce中怎么实现二次排序,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联建站专注于都兰网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供都兰营销型网站建设,都兰网站制作、都兰网页设计、都兰网站官网定制、成都微信小程序服务,打造都兰网络公司原创品牌,更为您提供都兰网站排名全网营销落地服务。

    二次排序的原理是将key自定义为一个其他的Bean对象,该对象中存储两个变量,一个为正常需要排序的key,第二个为需要作为二次排序的key,并为这个对象提供比较方法

        /**
     * @ClassName IntPair
     * @Description 
     *              定义IntPair对象,该对象实现WritableComparable接口,描述第一列和第二列数据,同时完成两列数据的相关操作
     *              ,这里是对二者进行比较
     * @date 2014年11月10日 上午10:15:34
     * 
     */
    public static class IntPair implements WritableComparable {
        String first;
        int second;
      
        /**
         * Set the left and right values.
         */
        public void set(String left, int right) {
            first = left;
            second = right;
        }

        public String getFirst() {
            return first;
        }

        public int getSecond() {
            return second;
        }
        
        
        public int getFileName() {
            return fileName;
        }

        public void setFileName(int fileName) {
            this.fileName = fileName;
        }

        @Override
        // 反序列化,从流中的二进制转换成IntPair
        public void readFields(DataInput in) throws IOException {
            first = in.readUTF();
            second = in.readInt();
            fileName = in.readInt();
        }
        
        @Override
        // 序列化,将IntPair转化成使用流传送的二进制
        public void write(DataOutput out) throws IOException {
            out.writeUTF(first);
            out.writeInt(second);
            out.writeInt(fileName);
        }

        @Override
        // key的比较
        public int compareTo(IntPair o) {
            if (!first.equals(o.first)) {
                return o.first.compareTo(first);
            } else if (second != o.second) {
                return second > o.second ? 1 : -1;
            } else {
                return 0;
            }
        }


        @Override
        public boolean equals(Object right) {
            if (right == null)
                return false;
            if (this == right)
                return true;
            if (right instanceof IntPair) {
                IntPair r = (IntPair) right;
                return r.first.equals(first) && r.second == second;
            } else {
                return false;
            }
        }
    }

为了能让第一次排序的正常排序需要使用Partitioner和

    /**
     * 分区函数类。根据first确定Partition。
     */
    public static class FirstPartitioner extends
            Partitioner {
        @Override
        public int getPartition(IntPair key, Text value, int numPartitions) {
            
            return key.first.hashCode()%numPartitions;
        }
    }
/**
     * 分组函数类。只要first相同就属于同一个组。
     */

    // 第二种方法,继承WritableComparator
    public static class GroupingComparator extends WritableComparator {
        protected GroupingComparator() {
            super(IntPair.class, true);
        }

        @SuppressWarnings("rawtypes")
        @Override
        // Compare two WritableComparables.
        public int compare(WritableComparable w1, WritableComparable w2) {
            IntPair ip1 = (IntPair) w1;
            IntPair ip2 = (IntPair) w2;
            String l = ip1.getFirst();
            String r = ip2.getFirst();
            return r.compareTo(l);
        }
    }

  然后在main函数中的job中加入

job.setMapOutputKeyClass(IntPair.class);
job.setGroupingComparatorClass(GroupingComparator.class);
job.setPartitionerClass(FirstPartitioner.class);

关于mapreduce中怎么实现二次排序问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站标题:mapreduce中怎么实现二次排序
本文地址:http://cdxtjz.cn/article/gsciip.html

其他资讯