MR計算模型二

2021-09-01 16:06:44 字數 1619 閱讀 7481

重寫public void map(object key, object value, context context) throws ioexception, interruptedexception 方法

map函式主要用於資料的清洗和原始處理

map函式每執行一次,處理一條資料

map的輸入,key預設是行號的偏移量,value是一行的內容

context.write(object, object)方法輸出

map的輸出是reduce的輸入

繼承reducer

重寫public void reduce(object key, iterablevalues, context context) throws ioexception, interruptedexception 方法 reduce函式是主要的業務處理和資料探勘部分

context.write(data, new intwritable(1))方法輸出

reduce的輸入時map的輸出,但不是直接輸出,而是按照相同key彙總過後的集合

context.write(object, object)方法輸出

logger.warn("hellohadoopsort已啟動");

configuration coresiteconf = new configuration();

coresiteconf.addresource(resources.getresource("core-site.xml"));

job job = job.getinstance(coresiteconf, "hellohadoopsort");

job.setjarbyclass(hellohadoopsort.class);

//設定map和reduce處理類

job.setreducerclass(sortreducer.class);

//設定map輸出型別

job.setoutputkeyclass(intwritable.class);

job.setoutputvalueclass(intwritable.class);

fileinputformat.addinputpath(job, new path("/sort/input"));

fileoutputformat.setoutputpath(job, new path("/sort/output"));

boolean flag = job.waitforcompletion(true);

logger.warn("hellohadoopsort已完成,執行結果:" + flag);

wordcountmap類繼承了

wordcountreduce類繼承了org.apache.hadoop.mapreduce.reducer,4個泛型型別含義與map類相同。

map的輸出型別與reduce的輸入型別相同,而一般情況下,map的輸出型別與reduce的輸出型別相同,因此,reduce的輸入型別與輸出型別相同。

在map中,讀取一行內容,按照空格分組,得到一行中的每個單詞,把單詞做為key輸出,value的內容可以為空或任意內容。

在reduce中,獲取到某個單詞及所有集合,集合的尺寸即是該單詞出現的數量,把單詞及其數量輸出到hdfs中

MR計算框架特點

1.資料劃分和計算任務排程 系統自動將乙個作業 job 待處理的大資料劃分為很多個資料塊,每個資料塊對應於乙個計算任務 task 並自動 排程計算節點來處理相應的資料塊。作業和任務排程功能主要負責分配和排程計算節點 map節點或reduce節點 同時負責監控這些節點的執行狀態,並 負責map節點執行...

MapReduce計算模型二

之前寫過關於hadoop方面的mapreduce框架的文章mapreduce框架hadoop應用 一 介紹了mapreduce的模型和hadoop下的mapreduce框架,此文章將進一步介紹mapreduce計算模型能用於解決什麼問題及有什麼巧妙優化。mapreduce準確的說,它不是乙個產品,而...

MR 二次排序

二次排序 eg 對左側序列進行排序,需要先對字母排序,然後在對數字進行排序.得到右側的序列.a 2 a 2 c 4 a 4 b 3 b 1 c 1 b 3 a 4 c 1 b 1 c 4public class sortmapreduce extends configured implements ...