十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
mapreduce中怎么實(shí)現(xiàn)二次排序,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)建站專注于都蘭網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供都蘭營銷型網(wǎng)站建設(shè),都蘭網(wǎng)站制作、都蘭網(wǎng)頁設(shè)計(jì)、都蘭網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造都蘭網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供都蘭網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
二次排序的原理是將key自定義為一個其他的Bean對象,該對象中存儲兩個變量,一個為正常需要排序的key,第二個為需要作為二次排序的key,并為這個對象提供比較方法
/** * @ClassName IntPair * @Description * 定義IntPair對象,該對象實(shí)現(xiàn)WritableComparable接口,描述第一列和第二列數(shù)據(jù),同時完成兩列數(shù)據(jù)的相關(guān)操作 * ,這里是對二者進(jìn)行比較 * @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 // 反序列化,從流中的二進(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{ @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中怎么實(shí)現(xiàn)二次排序問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。