? ? ? ?Hadoop Streaming是python可以調(diào)用于執(zhí)行MapReduce任務(wù)的接口茉帅,本人在學(xué)習(xí)使用時(shí)踩了很多坑叨叙,也折騰了一段時(shí)間,本篇文章主要記錄一下該工具的簡(jiǎn)單使用堪澎。
一擂错、介紹
? ? ? ?hadoop streaming 是Hadoop的一個(gè)工具,可以用其創(chuàng)建和運(yùn)行map\reduce作業(yè)樱蛤,程序只要遵循標(biāo)準(zhǔn)輸入钮呀、輸出(stdin讀、stdout寫(xiě))即可昨凡。mapper和reducer步驟可以是文件或者可執(zhí)行腳本爽醋。
基本格式如下:
hadoop command [genericOptions] [streamingOptions]
? ? ? ?注意:普通選項(xiàng)一定要寫(xiě)在streaming選項(xiàng)前面
二、普通選項(xiàng)
Parameter | Optional/Required | Description |
---|---|---|
-conf configuration_file | Optional | Specify an application configuration file |
-D property=value | Optional | Use value for given property |
-fs host:port or local | Optional | Specify a namenode |
-files | Optional | Specify comma-separated files to be copied to the Map/Reduce cluster |
-libjars | Optional | Specify comma-separated jar files to include in the classpath |
-archives | Optional | Specify comma-separated archives to be unarchived on the compute machines |
? ? ? ?其中:-D property=value是很重要的指令便脊。
※指定map\reduce任務(wù)數(shù):
-D mapred.reduce.tasks= 2
指定reducer個(gè)數(shù)蚂四,為0時(shí),該作業(yè)只有mapper
※指定mapper輸出分隔符:
-D stream.map.output.field.separator=.
指定mapper每條輸出key,value分隔符
-D stream.num.map.output.key.fields=4
第4個(gè) . 之前的部分為key,剩余為value
-D map.output.key.field.separator=.
設(shè)置map輸出中哪痰,Key內(nèi)部的分隔符
※指定基于哪些key進(jìn)行分桶:
-D num.key.fields.for.partition=1
只用1列Key做分桶
-D num.key.fields.for.partition=2
使用1,2共兩列key做分桶
-D mapred.text.key.partitioner.option =-k2,3
第2,3列Key做分桶
-D mapred.text.key.partitioner.option =-k2,2
第2列key做分桶
※使用上述-D配置后遂赠,下文需加上:
-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner
三、streaming命令選項(xiàng)
Parameter | Optional/Required | Description |
---|---|---|
-input directoryname or filename | Required | Input location for mapper |
-output directoryname | Required | Output location for reducer |
-mapper executable or JavaClassName | Optional | Mapper executable. If not specified, IdentityMapper is used as the default |
-reducer executable or JavaClassName | Optional | Reducer executable. If not specified, IdentityReducer is used as the default |
-file filename | Optional | Make the mapper, reducer, or combiner executable available locally on the compute nodes |
-inputformat JavaClassName | Optional | Class you supply should return key/value pairs of Text class. If not specified, TextInputFormat is used as the default |
-outputformat JavaClassName | Optional | Class you supply should take key/value pairs of Text class. If not specified, TextOutputformat is used as the default |
-partitioner JavaClassName | Optional | Class that determines which reduce a key is sent to |
-combiner streamingCommand or JavaClassName | Optional | Combiner executable for map output |
-cmdenv name=value | Optional | Pass environment variable to streaming commands |
-inputreader | Optional | For backwards-compatibility: specifies a record reader class (instead of an input format class) |
-verbose | Optional | Verbose output |
-lazyOutput | Optional | Create output lazily. For example, if the output format is based on FileOutputFormat, the output file is created only on the first call to Context.write |
-numReduceTasks | Optional | Specify the number of reducers |
-mapdebug | Optional | Script to call when map task fails |
-reducedebug | Optional | Script to call when reduce task fails |
(常用選項(xiàng)已經(jīng)標(biāo)注)
示例:
hadoop jar /usr/hadoop/hadoop-2.5.1/share/hadoop/tools/lib/hadoop-streaming-2.5.1.jar \
-D stream.num.map.output.key.fields=4 \
-D stream.map.output.field.separator=. \
-D mapred.text.key.partitioner.options=-k1,2 \
-D map.output.key.field.separator=. \
-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner \
-input /user/input/in.txt \
-output /user/output \
-mapper mapper.py -file mapper.py \
-reducer reducer.py -file reducer.py
總結(jié):
? ? ? ? 1、map操作會(huì)默認(rèn)將輸出按照key進(jìn)行排序,而不管value
? ? ? ? 2庐氮、需自己指定關(guān)鍵字列肛走,從而打到不同的reduce作業(yè)中
? ? ? ?后續(xù)將更新mapreduce工作原理以及shuffle流程,以及好友推薦谍椅、搜索自動(dòng)補(bǔ)全項(xiàng)目。
參考鏈接:
http://hadoop.apache.org/docs/current/hadoop-streaming/HadoopStreaming.html
https://www.cnblogs.com/shay-zhangjin/p/7714868.html