shuffle的關(guān)鍵階段sort(Map端和Reduce端)源碼分析
源碼中有這樣一段代碼
1. Map端排序獲取的比較器
public RawComparator getOutputKeyComparator() { // 獲取mapreduce.job.output.key.comparator.class,必須是RawComparator類型,如果沒設(shè)置,是null Class<? extends RawComparator> theClass = getClass( JobContext.KEY_COMPARATOR, null, RawComparator.class); // 如果用戶自定義了這個參數(shù),那么實例化用戶自定義的比較器 if (theClass != null) return ReflectionUtils.newInstance(theClass, this); // 默認情況,用戶是沒用自定義這個參數(shù) // 判斷Map輸出的key,是否是WritableComparable的子類 // 如果是,調(diào)用當前類的內(nèi)部的Comparator! return WritableComparator.get(getMapOutputKeyClass().asSubclass(WritableComparable.class), this); }
總結(jié): 如何對感興趣的數(shù)據(jù)進行排序?
① 數(shù)據(jù)必須作為key
② 排序是框架自動排序,我們提供基于key的比較器,也就是Comparator,必須是RawComparator類型
a) 自定義類,實現(xiàn)RawComparator,重寫compare()
指定mapreduce.job.output.key.comparator.class為自定義的比較器類型
b)key實現(xiàn)WritableComparable(推薦)
③ 實質(zhì)都是調(diào)用相關(guān)的comparaTo()方法,進行比較
2. Reduce端進行分組的比較器
RawComparator comparator = job.getOutputValueGroupingComparator(); // 獲取mapreduce.job.output.group.comparator.class,必須是RawComparator類型 // 如果沒用設(shè)置,直接獲取MapTask排序使用的比較器 // 也是比較key public RawComparator getOutputValueGroupingComparator() { Class<? extends RawComparator> theClass = getClass( JobContext.GROUP_COMPARATOR_CLASS, null, RawComparator.class); if (theClass == null) { return getOutputKeyComparator(); } // 如果設(shè)置了,就使用設(shè)置的比較器 return ReflectionUtils.newInstance(theClass, this); }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Javascript面試經(jīng)典套路reduce函數(shù)查重
- MapReduce核心思想圖文詳解
- 通用MapReduce程序復(fù)制HBase表數(shù)據(jù)
- Array數(shù)組對象中的forEach、map、filter及reduce詳析
- 對tf.reduce_sum tensorflow維度上的操作詳解
- js數(shù)組方法reduce經(jīng)典用法代碼分享
- MongoDB中MapReduce的使用方法詳解
- Java/Web調(diào)用Hadoop進行MapReduce示例代碼
- 詳解JS數(shù)組Reduce()方法詳解及高級技巧
- js中的reduce()函數(shù)講解
相關(guān)文章
Mybatis分頁插件PageHelper手寫實現(xiàn)示例
這篇文章主要為大家介紹了Mybatis分頁插件PageHelper手寫實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08idea使用easyCode生成代碼(根據(jù)mybatis-plus模板創(chuàng)建自己的模板)
本文主要介紹了idea使用easyCode生成代碼,easyCode代碼生成器可以減少低價值搬磚,具有一定的參考價值,感興趣的可以了解一下2023-10-10java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行
這篇文章主要介紹了java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行,需要的朋友可以參考下2017-02-02