溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mapreduce中怎么實現(xiàn)二次排序

發(fā)布時間:2021-08-12 14:46:03 來源:億速云 閱讀:152 作者:Leah 欄目:云計算

mapreduce中怎么實現(xiàn)二次排序,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

    二次排序的原理是將key自定義為一個其他的Bean對象,該對象中存儲兩個變量,一個為正常需要排序的key,第二個為需要作為二次排序的key,并為這個對象提供比較方法

        /**
     * @ClassName IntPair
     * @Description 
     *              定義IntPair對象,該對象實現(xiàn)WritableComparable接口,描述第一列和第二列數(shù)據(jù),同時完成兩列數(shù)據(jù)的相關(guān)操作
     *              ,這里是對二者進(jìn)行比較
     * @date 2014年11月10日 上午10:15:34
     * 
     */
    public static class IntPair implements WritableComparable<IntPair> {
        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
        // 反序列化,從流中的二進(jìn)制轉(zhuǎn)換成IntPair
        public void readFields(DataInput in) throws IOException {
            first = in.readUTF();
            second = in.readInt();
            fileName = in.readInt();
        }
        
        @Override
        // 序列化,將IntPair轉(zhuǎn)化成使用流傳送的二進(jìn)制
        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和

    /**
     * 分區(qū)函數(shù)類。根據(jù)first確定Partition。
     */
    public static class FirstPartitioner extends
            Partitioner<IntPair, Text> {
        @Override
        public int getPartition(IntPair key, Text value, int numPartitions) {
            
            return key.first.hashCode()%numPartitions;
        }
    }
/**
     * 分組函數(shù)類。只要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函數(shù)中的job中加入

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

關(guān)于mapreduce中怎么實現(xiàn)二次排序問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI