{"id":7951,"date":"2024-06-29T08:01:01","date_gmt":"2024-06-29T00:01:01","guid":{"rendered":""},"modified":"2024-06-29T08:01:01","modified_gmt":"2024-06-29T00:01:01","slug":"map\u9636\u6bb5\u7531\u4e00\u7cfb\u5217map\u4efb\u52a1\u7ec4\u6210_process\u6a21\u578b4\u7ed3\u679c\u89e3\u8bfb","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7951.html","title":{"rendered":"map\u9636\u6bb5\u7531\u4e00\u7cfb\u5217map\u4efb\u52a1\u7ec4\u6210_process\u6a21\u578b4\u7ed3\u679c\u89e3\u8bfb"},"content":{"rendered":"
\u6807\u7b7e\uff1a<\/p>\n
\u5982\u4e0a\u6b21\u5206\u6790\uff0c\u5176\u5b9emap\u51fd\u6570\u4e2d\u7684context.write()\u8c03\u7528\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n <\/p>\n \u68b3\u7406\u4e0b\u8c03\u7528\u8fc7\u7a0b\uff0ccontext\u7684write\u65b9\u6cd5\u5176\u5b9e\u662f\u8c03\u7528\u4e86TaskInputOutputContext\u7c7b\u7684write\u65b9\u6cd5\uff0c\u800c\u5728\u8fd9\u4e2awrite\u65b9\u6cd5\u5185\u90e8\u53c8\u8c03\u7528\u4e86output\u5b57\u6bb5\u7684write\u65b9\u6cd5\uff0c\u8fd9\u4e2aoutput\u5b57\u6bb5\u662fNewOutputCollector\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u81ea\u7136\u5c31\u56de\u5230\u4e86NewOutputCollector<\/strong>(reduce\u6570\u91cf\u4e0d\u662f0)\u8fd9\u4e2a\u7c7b\u7684write\u65b9\u6cd5\uff0c\u800c\u8fd9\u4e2a\u65b9\u6cd5\u5185\u90e8\u53c8\u8c03\u7528\u4e86\u672c\u7c7b\u7684\u4e00\u4e2a\u5b57\u6bb5collector\u7684collector\u65b9\u6cd5\uff0c\u800ccollector\u5b57\u6bb5\u662fMapOutputBuffer<\/strong>\u7c7b\u578b\uff0c\u63a5\u4e0b\u6765\u5c31\u4e3b\u8981\u5206\u6790\u8fd9\u4e24\u4e2a\u7c7b\u3002<\/span><\/p>\n 1\uff09NewOutputCollector<\/span><\/p>\n \u5728\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e09\u4e2a\u5b57\u6bb5\uff0ccollector\u521d\u59cb\u5316MapOutputBuffer\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0cpartitions\u5c31\u662f\u8fd9\u6b21\u8fd0\u884cjob\u7684reduce\u6570\u91cf\uff0cpartitioner\u5c31\u662f\u4f7f\u7528\u7684\u5206\u533a\u5668\uff0c\u5229\u7528partitioner\u5bf9\u8c61\u7684getPartitioner(K,V,partitions)\u5c31\u53ef\u4ee5\u5f97\u5230\u952e\u503c\u5bf9\u5bf9\u5e94\u7684\u5206\u533a\u53f7\uff0c\u7136\u540e\u5c06\u8fd9\u4e09\u4e2a\u53c2\u6570\u4f20\u7ed9collector\u7684collect\u65b9\u6cd5\u3002MapOutputBuffer\u624d\u662f\u91cd\u70b9\uff0c\u63a5\u4e0b\u6765\u5bf9\u5176\u5206\u6790\u3002<\/span><\/p>\n 2\uff09MapOutputBuffer<\/span><\/p>\n map\u7684\u7ed3\u679c\u5e76\u4e0d\u662f\u76f4\u63a5\u8f93\u5165\u5230\u786c\u76d8\u7684\uff0c\u800c\u662f\u5148\u5199\u5165\u5185\u5b58\u7f13\u51b2\u533a\uff0c\u8fd9\u4e2a\u5185\u5b58\u7f13\u51b2\u533a\u662f\u901a\u8fc7\u4e09\u4e2a\u73af\u5f62\u7ed3\u6784\u7684\u6570\u7ec4\u7ec4\u6210\u7684\uff0c\u8fd9\u4e09\u4e2a\u73af\u5f62\u6570\u7ec4\u5206\u522b\u662fkvoffsets\uff0ckvindices\uff0ckvbuffer\u3002\u8fd9\u4e2a\u4e09\u4e2a\u6570\u7ec4\u53c8\u6709\u5bf9\u5e94\u7684\u6307\u793a\u5668\u53d8\u91cf\uff0c\u9996\u5148\u7ed9\u51fa\u4e09\u4e2a\u7684\u5173\u7cfb\u56fe\uff1a<\/span><\/p>\n <\/p>\n kvoffsets\u548ckvindices\u90fd\u662fint[]\u578b\u6570\u7ec4\uff0c\u800ckvbuffer\u662fbyte[]\u7c7b\u578b\u3002kvoffset\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u5c31\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u5bf9\u5e94\u7684\u5206\u533a\u53f7\u5728kvindices\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u800ckvindices\u6bcf\u6b21\u4f7f\u7528\u4e09\u4e2a\u5143\u7d20\u6765\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u5148\u5173\u4fe1\u606f\uff1a\u5728kvbuffer\u4e2dkey\u7684\u8d77\u59cb\u5b58\u50a8\u4f4d\u7f6e\u3001\u5728kvbuffer\u4e2dvalue\u7684\u8d77\u59cb\u5b58\u50a8\u4f4d\u7f6e\u3001\u4ee5\u53ca\u6b64\u952e\u503c\u5bf9\u5bf9\u5e94\u7684\u5206\u533a\u53f7partition\u3002\u800ckvbuffer\u5c31\u8d1f\u8d23\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u4e0a\u56fe\u6240\u793a\uff0ckvoffsets\u548ckvindices\u5b58\u50a8\u4fe1\u606f\u7684\u5927\u5c0f\u90fd\u662f\u786e\u5b9a\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u4e00\u4e2aint\u578b\u7684\u6b63\u6570\u5b58\u50a8\u7d22\u5f15\u503c\u548c\u5206\u533a\u53f7\uff0c\u4f46\u662fkvbuffer\u4e2d\u5b58\u50a8\u7684key\u3001value\u5927\u5c0f\u5374\u4e0d\u662f\u786e\u5b9a\u7684\u3002\u6240\u4ee5\u6211\u4eec\u5728kvindices\u4e2d\u53ea\u662f\u5b58\u50a8\u4e86\u8fd9\u4e9bkey\u3001value\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u6211\u4eec\u4e0d\u80fd\u53d1\u73b0\uff0c\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u4f1a\u5e26\u676516\u4e2a\u5b57\u8282\u7684\u989d\u5916\u5f00\u9500\uff08\u4e00\u4e2aint\u578b\u53d8\u91cf\u662f4\u4e2a\u5b57\u8282\uff09\uff0c\u5206\u522b\u662fkvoffsets\u4e2d\u76841\u4e2aint\u53d8\u91cf+kvindices\u4e2d\u76843\u4e2aint\u53d8\u91cf\u3002<\/span><\/p>\n \u5176\u5b9e\uff0c\u8fd9\u4e09\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u7531\"io.sort.mb\"\u6307\u5b9a\uff0c\u9ed8\u8ba4io.sort.mb=100,\u4e5f\u5c31\u662fsortmb=100\uff0c\u90a3\u4e48\u6709maxmemUsage=sortmb<<20,\u4e5f\u5c31\u662f100M\u3002\u9ed8\u8ba4\u8fd9100M\u4e2dkvoffsets\u548ckvindices\u53605M(\u7531\"io.sort.record.percent\"\u6307\u5b9a\uff0c\u9ed8\u8ba4\u503c\u662f0.05)\u5176\u4e2d\uff0ckvoffsets\u4e0ekvindices\u7684\u6bd4\u4f8b\u662f1\uff1a3(kvoffset\u4e2d\u4e00\u4e2a\u7d22\u5f15\u503c\u5bf9\u5e94kvindices\u4e2d\u7684\u4e09\u4e2a\u5143\u7d20)\u3002<\/span><\/p>\n \u5728kvbuffer\u4e2d\u5982\u679c\u5bb9\u91cf\u8fbe\u5230\u4e00\u4e2a\u7684\u6bd4\u4f8b\u5c31\u4f1a\u89e6\u53d1spill(\u6ea2\u5199)\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6bd4\u4f8b\u7531\"io.sort.spill.percent\"\u6307\u5b9a\uff0c\u9ed8\u8ba4\u503c\u662f0.8\u3002\u540c\u6837\u7684kvoffsets\u5bb9\u91cf\u8fbe\u5230\u4e00\u5b9a\u7a0b\u5ea6\u4e5f\u4f1a\u89e6\u53d1spill\u64cd\u4f5c\uff0c\u4e5f\u662f\u7531\"io.sort.spill.percent\"\u6307\u5b9a\u3002\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u8fbe\u5230\u4e00\u5b9a\u7684\u6bd4\u4f8b\u5c31spill\uff0c\u800c\u4e0d\u662f\u5199\u6ee1\u5728spill\uff1f\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u5982\u679c\u662f\u5199\u6ee1\u7f13\u5b58\u624dspill\uff0c\u90a3\u4e48\u5728\u5237\u5199\u78c1\u76d8\u7684\u65f6\u5019\u5c31\u4e0d\u80fd\u5199\u5165(\u56e0\u4e3a\u6211\u8981\u8bfb\u53d6\u8fd9\u4e2a\u7f13\u5b58\u533a\u57df\uff0c\u5199\u4e4b\u524d\u201c\u8fd9\u5757\u533a\u57df\u201d\u5c31\u662f\u5f52\u5199\u7ebf\u7a0b\u6240\u6709\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u80fd\u8bbf\u95ee)\uff0c\u90a3\u4e48\u5199\u7ebf\u7a0b\u5c31\u4f1a\u963b\u585e\uff0cmap\u7ed3\u679c\u5c31\u8981\u7b49\u5f85\u5199\u5165\u7f13\u5b58\uff0c\u5982\u679c\u8fbe\u5230\u4e00\u5b9a\u6bd4\u4f8b\u5c31spill\uff0c\u5237\u5199\u78c1\u76d8\u7684\u65f6\u5019\u5c31\u53ea\u662f\u7f13\u5b58\u4e2d\u4e00\u5b9a\u6bd4\u4f8b\u7684\u533a\u57df\u5f52\u5199\u7ebf\u7a0b\u6240\u6709\uff0c\u5176\u4ed6\u7684\u90e8\u5206\u5c31\u53ef\u4ee5\u901a\u8fc7\u5199\u7ebf\u7a0b\u5199\u5165map\u7684\u8f93\u51fa\uff0c\u63d0\u9ad8\u4e86\u541e\u5410\u91cf\u3002\u8fd9\u6837\u5c31\u53c8\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u5199\u7ebf\u7a0b\u6bd4spill\u7684\u8fc7\u7a0b\u5757\uff0c\u5199\u7ebf\u7a0b\u7684\u90a3\u5757\u513f\u533a\u57df\u5df2\u7ecf\u5199\u6ee1\u4e86\uff0c\u4f46\u662fspill\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u4e5f\u8981\u7b49\u5f85\uff0c\u867d\u7136spill\u53ef\u80fd\u5df2\u7ecf\u8fdb\u884c\u4e86\u4e00\u5927\u534a\uff0cspill\u533a\u57df\u7684\u524d\u534a\u90e8\u5206\u5df2\u7ecf\u8bfb\u53d6\u5230\u78c1\u76d8\u3002\u6240\u4ee5\u624d\u628akvoffsets<\/span>\u8bbe\u8ba1\u4e3a\u903b\u8f91\u4e0a\u73af\u5f62\u6570\u7ec4\uff0c\u5199\u5230\u672b\u5c3e\u7684\u65f6\u5019\u901a\u8fc7\u67e5\u770b\u4e0b\u4e00\u4e2a\u53ef\u5199\u7684\u4f4d\u7f6e(kvindex)\u6765\u51b3\u5b9a\u662f\u5426\u53ef\u4ee5\u5199\u5165\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/span> <\/span><\/p>\n \u9996\u5148\u7ed9\u51fakvoffsets\u7684\u76f8\u5173\u53d8\u91cf\uff1a<\/span><\/p>\n kvoffsets\uff1a<\/p>\n <\/p>\n kvbuffer:<\/span><\/p>\n bufstart\u3001bufend\u3001bufindex\u4e0ekvsetoffs\u4e2d\u5bf9\u5e94\u7684\u53d8\u91cf\u610f\u4e49\u662f\u76f8\u540c\u7684\uff0c\u4e0d\u8fc7\u53c8\u589e\u52a0\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1abufvoid\u548cbufmark\u3002bufvoid\u8868\u793a\u5b9e\u9645\u4f7f\u7528\u7684\u7f13\u5b58\u7684\u6700\u5c3e\u90e8\uff0c\u7531\u4e8e\u952e\u503c\u5bf9\u7684\u5927\u5c0f\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u6240\u4ee5\u4f7f\u7528bufmark\u6765\u6807\u8bb0\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u7ed3\u675f\uff0c\u6bcf\u5f53\u5199\u5165\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u5c31\u66f4\u65b0\u8fd9\u4e2a\u503c\u3002<\/p>\n map\u8f93\u51fa\u7684\u952e\u503c\u5bf9\u5b58\u5165\u7f13\u5b58\u4e4b\u524d\u8981\u9996\u5148\u7ecf\u8fc7\u5e8f\u5217\u5316\uff0c\u5e8f\u5217\u5316\u4e4b\u540e\u6b64\u624d\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\u3002\u56e0\u4e3a\u6211\u4eec\u662f\u5148\u5c06\u5e8f\u5217\u5316\u7684key\u5b58\u5165\u7f13\u5b58\uff0c\u518d\u5c06\u5e8f\u5217\u5316\u4e86\u7684value\u5b58\u5165\u7f13\u5b58\uff0c\u8fd9\u5c31\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff1a\u5269\u4e0b\u7684\u7a7a\u95f4\u5b58\u4e0d\u4e0bkey\u6216\u8005value\u3002<\/p>\n a.\u5982\u679c\u8bf4\u5b58\u4e0d\u4e0bvalue\uff0c\u5c31\u4f1a\u629b\u51faMapBufferTooSmallException\u5f02\u5e38\uff08\u8fd9\u5c31\u662f\u89e6\u53d1spill\uff0c\u5f00\u59cbspill\u8fc7\u7a0b\u4e86\uff09\u3002<\/p>\n b.\u5982\u679c\u5b58\u4e0d\u4e0bkey\uff0c\u90a3\u4e48\u5c31\u8981\u91c7\u53d6\u63aa\u65bd\u4e86\u3002\u4e0a\u5f20\u56fe\uff1a<\/p>\n <\/p>\n \u7ea2\u8272\u533a\u57df\u8868\u793a\u5df2\u7ecf\u5199\u5165\u7684\u952e\u503c\u5bf9\uff0c\u7b2c\u4e00\u4e2a\u56fe\u4e2dbufvoid-bufindex\u5373\u4f7f\u5f53\u524d\u5269\u4f59\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u800c\u7b2c\u4e8c\u7ae0\u56fe\u4e2d\u7684\u4e24\u5757\u84dd\u8272\u90e8\u5206\u5c31\u662f\u5b58\u5165\u5e8f\u5217\u5316\u7684key\u6240\u9700\u8981\u7684\u7a7a\u95f4\uff0c\u5f88\u660e\u663ekey\u7684\u5b58\u50a8\u8de8\u8d8a\u4e86\u7f13\u5b58\u7684\u5c3e\u7aef\u548c\u9996\u7aef\uff0c\u4f46\u662f\u6267\u884cspill\u7684\u4e4b\u540e\u6211\u4eec\u9700\u8981\u4f7f\u7528RawComparator\u65b9\u6cd5\u6309\u7167key\u5bf9\u952e\u503c\u5bf9\u6392\u5e8f\uff0c\u800c\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u80fd\u5bf9\u8fde\u7eed\u7684\u4e8c\u8fdb\u5236\u5185\u5b58buffer\u8fdb\u884c\u6392\u5e8f\uff0c\u4e5f\u5c31\u9700\u8981\u6bcf\u4e2akey\u90fd\u662f\u8fde\u7eed\u5b58\u50a8\u7684\uff0c\u56e0\u6b64\u5c31\u9700\u8981\u8c03\u7528BlockingBuffer\u7684reset\u65b9\u6cd5\u5c3e\u7aef\u7684\u84dd\u8272\u90e8\u5206\u79fb\u52a8\u5230\u9996\u7aef\uff0c\u6b64\u65f6\u66f4\u65b0bufvoid\u7684\u503c\u3002<\/p>\n c.\u4e0a\u9762\u662f\u60c5\u51b5\u662f\u53ef\u4ee5\u5b58\u4e0bkey\uff0c\u5982\u679ckey\u5b58\u4e0d\u4e0b\u5462\uff0c\u90a3\u4e48\u5c31\u4f1a\u76f4\u63a5\u8f93\u51fakey\u3001value\uff0c\u4e5f\u5c31\u662f\u8c03\u7528spill\u7ebf\u7a0b\u5c06\u952e\u503c\u5bf9\u5199\u5165\u5230\u6587\u4ef6\uff08\u8fd9\u5c31\u662f\u89e6\u53d1\u4e86spill\u673a\u5236\uff0c\u5f00\u59cbspil\u4e86\u5440\uff01\uff09\u3002<\/p>\n <\/p>\n \u5c0a\u91cd\u539f\u521b---\u539f\u6587\u94fe\u63a5\uff1ahttp:\/\/www.mamicode.com\/info-detail-1105399.html\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"map\u9636\u6bb5\u7531\u4e00\u7cfb\u5217map\u4efb\u52a1\u7ec4\u6210_process\u6a21\u578b4\u7ed3\u679c\u89e3\u8bfb\u6807\u7b7e\uff1a\u5982\u4e0a\u6b21\u5206\u6790\uff0c\u5176\u5b9emap\u51fd\u6570\u4e2d\u7684context.write()\u8c03\u7528\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a\u68b3\u7406...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7951"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7951"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7951\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}private<\/span> final<\/span> MapOutputCollector<K,V> collector; <\/span>private<\/span> final<\/span> org.apache.hadoop.mapreduce.Partitioner<K,V> partitioner; <\/span>private<\/span> final<\/span> int<\/span> partitions;<\/pre>\n<\/p><\/div>\n
private<\/span> volatile<\/span> int<\/span> kvstart = 0; \/\/<\/span>\u8868\u793a\u5f53\u524d\u5df2\u5199\u7684\u6570\u636e\u7684\u5f00\u59cb\u4f4d\u7f6e<\/span> private<\/span> volatile<\/span> int<\/span> kvend = 0; \/\/<\/span> \u672a\u6267\u884cspill\u662f\u7b49\u4e8ekvstart\uff0c\u6267\u884cspill\u662f\u4e0d\u7b49\u4e8ekvstart\uff08\u7b49\u4e8ekvindex\uff09<\/span> private<\/span> int<\/span> kvindex = 0; \/\/<\/span> \u8868\u793a\u4e0b\u4e00\u4e2a\u53ef\u5199\u7684\u4f4d\u7f6e<\/span> private<\/span> final<\/span> int<\/span>[] kvoffsets; <\/span>\/\/<\/span>volatile\u4fee\u6539\u7684\u5b57\u6bb5\u8868\u793a\u6b64\u5b57\u6bb5\u53ef\u4ee5\u88ab\u4e0d\u540c\u7ebf\u7a0b\u8bbf\u95ee\u548c\u4fee\u6539<\/span><\/pre>\n<\/p><\/div>\n
private<\/span> volatile<\/span> int<\/span> bufstart = 0; \/\/<\/span> marks beginning of spill<\/span> private<\/span> volatile<\/span> int<\/span> bufend = 0; \/\/<\/span> marks beginning of collectable<\/span> private<\/span> volatile<\/span> int<\/span> bufvoid = 0; \/\/<\/span> marks the point where we should stop <\/span>\/\/<\/span> reading at the end of the buffer<\/span> private<\/span> int<\/span> bufindex = 0; \/\/<\/span> marks end of collected<\/span> private<\/span> int<\/span> bufmark = 0; \/\/<\/span> marks end of record<\/span> private<\/span> byte<\/span>[] kvbuffer; <\/pre>\n<\/p><\/div>\n