這篇文章主要介紹了shuffle中關鍵階段sort的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
源碼中有這樣一段代碼
1. Map端排序獲取的比較器
public RawComparator getOutputKeyComparator() { // 獲取mapreduce.job.output.key.comparator.class,必須是RawComparator類型,如果沒設置,是null Class<? extends RawComparator> theClass = getClass( JobContext.KEY_COMPARATOR, null, RawComparator.class); // 如果用戶自定義了這個參數,那么實例化用戶自定義的比較器 if (theClass != null) return ReflectionUtils.newInstance(theClass, this); // 默認情況,用戶是沒用自定義這個參數 // 判斷Map輸出的key,是否是WritableComparable的子類 // 如果是,調用當前類的內部的Comparator! return WritableComparator.get(getMapOutputKeyClass().asSubclass(WritableComparable.class), this); }
總結: 如何對感興趣的數據進行排序?
① 數據必須作為key
② 排序是框架自動排序,我們提供基于key的比較器,也就是Comparator,必須是RawComparator類型
a) 自定義類,實現RawComparator,重寫compare()
指定mapreduce.job.output.key.comparator.class為自定義的比較器類型
b)key實現WritableComparable(推薦)
③ 實質都是調用相關的comparaTo()方法,進行比較
2. Reduce端進行分組的比較器
RawComparator comparator = job.getOutputValueGroupingComparator(); // 獲取mapreduce.job.output.group.comparator.class,必須是RawComparator類型 // 如果沒用設置,直接獲取MapTask排序使用的比較器 // 也是比較key public RawComparator getOutputValueGroupingComparator() { Class<? extends RawComparator> theClass = getClass( JobContext.GROUP_COMPARATOR_CLASS, null, RawComparator.class); if (theClass == null) { return getOutputKeyComparator(); } // 如果設置了,就使用設置的比較器 return ReflectionUtils.newInstance(theClass, this); }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“shuffle中關鍵階段sort的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。