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

