4 開發MapReduce應用程序


系統參數配置

Configuration類由源來設置,每個源包含以XML形式出現的一系列屬性/值對。如:

configuration-default.xml

configuration-site.xml

Configuration conf = new Configuration();

conf.addResource("configuraition-default.xml");

conf.addResource("|configuration-site.xml");

后添加進來的屬性取值覆蓋前面所添加資源中的屬性取值,除非前面的屬性值被標記為final。


Hadoop默認使用兩個源進行配置,順序加載core-default.xml和core-site.xml。

前者定義系統默認屬性,后者定義在特定的地方重寫。


性能調優

在正確完成功能的基礎上,使執行的時間盡量短,占用的空間盡量小。


輸入采用大文件

1000個2.3M的文件運行33分鐘;合并為1個2.2G的文件后運行3分鐘。

也可借用Hadoop中的CombineFileInputFormat,它將多個文件打包到一個輸入單元中,從而每次執行Map操作就會處理更多的數據。


壓縮文件

對Map的輸出進行壓縮,好處:減少存儲文件的空間;加快在網絡上的傳輸速度;減少數據在內存和磁盤間交換的時間。

mapred.compress.map.output設置為true來對Map的輸出數據進行壓縮;

mapred.map.output.compression.codec設置壓縮格式


修改作業屬性

在conf目錄下修改屬性

mapred.tasktracker.map.tasks.maximum

mapred.tasktracker.reduce.tasks.maximum

設置Map/Reduce任務槽數,默認均為2。


MapReduce工作流

如果處理過程變得復雜了,可以通過更加復雜、完善的Map和Reduce函數,甚至更多的MapReduce工作來體現。


復雜的Map和Reduce函數

基本的MapReduce作業僅僅集成并覆蓋了基類Mapper和Reducer中的核心函數Map或Reduce。

下面介紹基類中的其他函數,使大家能夠編寫功能更加復雜、控制更加完備的Map和Reduce函數。


setup函數

源碼如下

/**
* Called once at the start of the task
*/
protected void setup( Context context) throws IOException, InterruptedException {//NOTHING
}

此函數在task啟動開始時調用一次。

每個task以Map類或Reduce類為處理方法主體,輸入分片為處理方法的輸入,自己的分片處理完之后task也就銷毀了。

setup函數在task啟動之后數據處理之前只調用一次,而覆蓋的Map函數或Reduce函數會針對輸入分片中的每個key調用一次。

可以將Map或Reduce函數中的重復處理放置到setup函數中;

可以將Map或Reduce函數處理過程中可能使用到的全局變量進行初始化;

可以從作業信息中獲取全局變量;

可以監控task的啟動。


cleanup函數

/**
* Called noce at the end of the task
*/
protected void cleanup(Context context) throws IOException, InterruptedException {//NOTHING
}

和setup相似,不同之處在于cleanup函數是在task銷毀之前執行的。


run函數

/**
* Expert users can override this method for more complete control over the execution of the Mapper.
*@param context
*@throws IOException
*/
public void run(Context context) throws IOException, InterruptedException {setup (context);while (context.nextKeyValue()) {map(context.getCurrentKey(), context.getCurrentValue(), context);}cleanup(context);
}

此函數是map函數或Reduce函數的啟動方法。

如果想更完備地控制Map或者Reduce,可以覆蓋此函數。


MapReduce中全局共享數據方法

1、讀寫HDFS文件

利用Hadoop的Java API來實現。

需要注意:多個Map或Reduce的寫操作會產生沖突,覆蓋原有數據。

優點:能夠實現讀寫,比較直觀;

缺點:要貢獻一些很小的全局數據也需要使用IO,這將占用系統資源,增加作業完成的資源消耗。


2、配置Job屬性

在任務啟動之初利用Configuration類中的set(String name, String value)將一些簡單的全局數據封裝到作業的配置屬性中;

然后在task中利用Configuration類中的get(String name)獲取配置到屬性中的全局數據。

優點:簡單,資源消耗小;

缺點:對量比較大的共享數據顯得比較無力。


3、使用DistributedCache

為應用提供緩存文件的只讀工具,可以緩存文本文件、壓縮文件、jar文件。

在使用時,用戶可以在作業配置時使用本地或HDFS文件的UCRL來將其設置成共享緩存文件。

在作業啟動之后和task啟動之前,MapReduce框架會將可能需要的緩存文件復制到執行任務結點的本地。

優點:每個Job共享文件只會在啟動之后復制一次,適用于大量的共享數據;

缺點:只讀。

//配置
Configuration conf = new Configuration();
DistributedCache.addCacheFile(new URI("/myapp/lookup"), conf);
//在Map函數中使用:
public static class Map extends Mapper<...>{private Path[] localArchives;private Paht[] localFiles;public void setup (Context context) throws IOException, InterruptedException{Configuration conf = context.getConfiguration();localArchives = DistributedCache.getLocalCacheArchives(conf);localFiles = DistributedCache.getLocalCacheFiles(conf);}public void map(K key, V value, Context context) throws IOException {//使用從緩存文件中獲取的數據context.collect(k, v);}
} 



鏈接MapReduce Job

如果問題不是一個MapReduce作業就能解決,就需要在工作流中安排多個MapReduce作業,讓它們配合起來自動完成一些復雜的任務,而不需要用戶手動啟動每一個作業。


1、線性MapReduce Job流

最簡單的辦法是設置多個有一定順序的Job,每個Job以前一個Job的輸入作為輸入,經過處理,將數據再輸入到下一個Job中。

這種辦法的實現非常簡單,將每個Job的啟動代碼設置成只有上一個Job結束之后才執行,然后將Job的輸入設置成上一個Job的輸出路徑。


2、復雜MapReduce Job流

第一種方法在某些復雜任務下仍然不能滿足需求。

如Job3需要將Job1和Job2的輸出結果組合起來進行處理。這種情況下Job3的啟動依賴于Job1和Job2的完成,但Job1和Job2之間沒有關系。

針對這種復雜情況,MapReduce框架提供了讓用戶將Job組織成復雜Job流的API--ControlledJob類和JobControl類。這兩個類屬于org.apache.hadoop.mapreduce.lib.jobcontrol包。

具體做法:

先按照正常情況配置各個Job;

配置完成后再將各個Job封裝到對應的ControlledJob對象中;

然后使用ControlledJob的addDependingJob()設置依賴關系;

接著再實例化一個JobControl對象,并使用addJob()方法將所有的Job注入JobControl對象中;

最后使用JobControl對象的run方法啟動Job流。


3、Job設置預處理和后處理過程

org.apache.hadoop.mapred.lib包下的ChainMapper和ChainReducer兩個靜態類來實現。


The ChainMapper class allows to use multiple Mapper classes within a single Map task.

The Mapper classes are invoked in a chained (or piped) fashion, the output of the first becomes the input of the second, and so on until the last Mapper, the output of the last Mapper will be written to the task's output.


The ChainReducer class allows to chain multiple Mapper classes after a Reducer within the Reducer task.

For each record output by the Reducer, the Mapper classes are invoked in a chained (or piped) fashion, the output of the first becomes the input of the second, and so on until the last Mapper, the output of the last Mapper will be written to the task's output. ?


The key functionality of this feature is that the Mappers in the chain do not need to be aware that they are executed in a chain. This enables having reusable specialized Mappers that can be combined to perform composite operations within a single task.

Special care has to be taken when creating chains that the key/values output by a Mapper are valid for the following Mapper in the chain. It is assumed all Mappers and the Reduce in the chain use maching output and input key and value classes as no conversion is done by the chaining code.

Using the ChainMapper and the ChainReducer classes is possible to compose Map/Reduce jobs that look like[MAP+ / REDUCE MAP*]. And immediate benefit of this pattern is a dramatic reduction in disk IO.

IMPORTANT: There is no need to specify the output key/value classes for the ChainMapper, this is done by the addMapper for the last mapper in the chain.

ChainMapper usage pattern:

 ...conf.setJobName("chain");conf.setInputFormat(TextInputFormat.class);conf.setOutputFormat(TextOutputFormat.class);

JobConf mapAConf = new JobConf(false);...ChainMapper.addMapper(conf, AMap.class, LongWritable.class, Text.class,Text.class, Text.class, true, mapAConf);

JobConf mapBConf = new JobConf(false);...ChainMapper.addMapper(conf, BMap.class, Text.class, Text.class,LongWritable.class, Text.class, false, mapBConf);

JobConf reduceConf = new JobConf(false);...ChainReducer.setReducer(conf, XReduce.class, LongWritable.class, Text.class,Text.class, Text.class, true, reduceConf);

ChainReducer.addMapper(conf, CMap.class, Text.class, Text.class,LongWritable.class, Text.class, false, null);

ChainReducer.addMapper(conf, DMap.class, LongWritable.class, Text.class,LongWritable.class, LongWritable.class, true, null);

FileInputFormat.setInputPaths(conf, inDir);FileOutputFormat.setOutputPath(conf, outDir);...

JobClient jc = new JobClient(conf);RunningJob job = jc.submitJob(conf);...


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387169.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387169.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387169.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

實用的HTML5的上傳圖片方法

<input type"file" accept"video/*;capturecamcorder"> <input type"file" accept"audio/*;capturemicrophone"><input type"file" accept"image/*;capturecamera">直接調用相機<input type…

3.11 列出完數

完數&#xff1a;一個數恰好等于不包括自身的所有不同因子之和。如6123。 輸入&#xff1a;每一行含有一個整數n。 輸出&#xff1a;對每個整數n&#xff0c;輸出所有不大于n的完數。輸出格式為&#xff1a;整數n&#xff0c;冒號&#xff0c;空格&#xff0c;完數&#xff0…

angularjs 上傳

xxx.module.ts模塊 import { NgModule} from “angular/core”; import { FileUploadModule } from “ng2-file-upload” ; import { XXXComponent } from “./xxx.component”; NgModule({ imports:[ FileUploadModule ], declarations:[ XXXComponent &#xff0c;/component…

PHPCMS的產品篩選功能

如下圖所示功能&#xff1a; 首先&#xff0c;用下面這些代碼替換掉phpcms/libs/functions/extention.func.php的內容 <?php /*** extention.func.php 用戶自定義函數庫** copyright (C) 2005-2010 PHPCMS* license http://www.phpcms.cn/licen…

框架使用SpringBoot + Spring Security Oauth2 +PostMan

框架使用SpringBoot Spring Security Oauth2 主要完成了客戶端授權 可以通過mysql數據庫讀取當前客戶端表信息進行驗證&#xff0c;token存儲在數據庫中 1.引入依賴 oauth2 依賴于spring security&#xff0c;需要引入spring&#xff0c; mysql&#xff0c;redis&#xff0c; …

3.12 12!配對

找出輸入數據中所有兩兩相乘的積為12!的個數。 輸入樣例&#xff1a; 1 10000 159667200 9696 38373635 1000000 479001600 3 1 479001600 輸出樣例&#xff1a; 3 有3對&#xff1a; 1 479001600 1 479001600 3 159667200 #include<iostream> #include<fstre…

程序員自身價值值這么多錢么?

xx 網絡公司人均獎金 28 個月…… xx 科技公司人均獎金 35 個月…… 每到年底&#xff0c;這樣的新聞在互聯網業內簡直是鋪天蓋地。那些獎金不高的程序員們一邊羨慕嫉妒&#xff0c;一邊暗暗比較一下自己的身價&#xff0c;考慮是不是該跳槽了。 不同水平的程序員&#xff0c;薪…

3.13 判讀是否是對稱素數

輸入&#xff1a;11 101 272 輸出&#xff1a; Yes Yes No #include<fstream> #include<iostream> #include<sstream> #include<string> #include<cmath> using namespace std;bool isPrime(int); bool isSymmetry(int);int main(){ifstream…

Spring MVC中使用 Swagger2 構建Restful API

0.Spring MVC配置文件中的配置[java] view plaincopy<!-- 設置使用注解的類所在的jar包&#xff0c;只加載controller類 --> <span style"white-space:pre"> </span><context:component-scan base-package"com.jay.plat.config.contro…

Go語言規范匯總

目錄 統一規范篇合理規劃目錄GOPATH設置import 規范代碼風格大小約定命名篇基本命令規范項目目錄名包名文件名常量變量變量申明變量命名慣例全局變量名局部變量名循環變量結構體(struct)接口名函數和方法名參數名返回值開發篇包魔鬼數字常量 & 枚舉結構體運算符函數參數返回…

3.14 01串排序

將01串首先按照長度排序&#xff0c;其次按1的個數的多少排序&#xff0c;最后按ASCII碼排序。 輸入樣例&#xff1a; 10011111 00001101 10110101 1 0 1100 輸出樣例&#xff1a; 0 1 1100 1010101 00001101 10011111 #include<fstream> #include<iost…

platform(win32) 錯誤

運行cnpm install后&#xff0c;出現雖然提示不適合Windows&#xff0c;但是問題好像是sass loader出問題的。所以只要執行下面命令即可&#xff1b;方案一&#xff1a;cnpm rebuild node-sass #不放心可以重新安裝下 cnpm install方案二&#xff1a;npm update npm install no…

Error: Program type already present: okhttp3.Authenticator$1

在app中的build.gradle中加入如下代碼&#xff0c; configurations {all*.exclude group: com.google.code.gsonall*.exclude group: com.squareup.okhttp3all*.exclude group: com.squareup.okioall*.exclude group: com.android.support,module:support-v13 } 如圖 轉載于:ht…

3.15 排列對稱串

篩選出對稱字符串&#xff0c;然后將其排序。 輸入樣例&#xff1a; 123321 123454321 123 321 sdfsdfd 121212 \\dd\\ 輸出樣例 123321 \\dd\\ 123454321 #include<fstream> #include<iostream> #include<string> #include<set> using …

ES6規范 ESLint

在團隊的項目開發過程中&#xff0c;代碼維護所占的時間比重往往大于新功能的開發。因此編寫符合團隊編碼規范的代碼是至關重要的&#xff0c;這樣做不僅可以很大程度地避免基本語法錯誤&#xff0c;也保證了代碼的可讀性&#xff0c;畢竟&#xff1a;程序是寫給人讀的&#xf…

前端 HTML 常用標簽 head標簽相關內容 script標簽

script標簽 定義JavaScript代碼 <!--定義JavaScript代碼--> <script type"text/javascript"></script> 引入JavaScript文件 src""引入的 js文件路徑 <!-- 引入JavaScript文件 --> <script src"./index.js"></s…

3.16 按績點排名

成績60分及以上的課程才予以計算績點 績點計算公式&#xff1a;[(課程成績-50) / 10 ] * 學分 學生總績點為所有績點之和除以10 輸入格式&#xff1a; 班級數 課程數 各個課程的學分 班級人數 姓名 各科成績 輸出格式&#xff1a; class 班級號: 姓名&#xff08;占1…

iview日期控件,雙向綁定日期格式

日期在雙向綁定之后格式為&#xff1a;2017-07-03T16:00:00.000Z 想要的格式為2017-07-04調了好久&#xff0c;幾乎一天&#xff1a;用一句話搞定了 on-change”addForm.Birthday$event”<Date-picker placeholder"選擇日期" type"datetime" v-model&…

移除html,jsp中的元素

移除html&#xff0c;jsp中的元素 某些時候&#xff0c;需要移除某個元素&#xff0c;比如移除表中的某一行 $("#tbody").children().eq(i).remove();或者 $("#tr").remove();PS&#xff1a;獲取表中的tr的數量&#xff1a; $("#tbody").childre…

ACM001 Quicksum

本題的重點在于數據的讀入。 可采用cin.getlin()一行一行讀入數據&#xff1b;也可采用cin.get()一個一個讀入字符。 cin會忽略回車、空格、Tab跳格。 cin.get()一個一個字符讀&#xff0c;不忽略任何字符。 cin.getline()一行一行讀入。 #include<fstream> #include…