Hadoop Streaming的使用
2019-03-13 本文已影响0人
興華的mark
Hadoop Streaming是python可以调用于执行MapReduce任务的接口,本人在学习使用时踩了很多坑,也折腾了一段时间,本篇文章主要记录一下该工具的简单使用。
一、介绍
hadoop streaming 是Hadoop的一个工具,可以用其创建和运行map\reduce作业,程序只要遵循标准输入、输出(stdin读、stdout写)即可。mapper和reducer步骤可以是文件或者可执行脚本。
基本格式如下:
hadoop command [genericOptions] [streamingOptions]
注意:普通选项一定要写在streaming选项前面
二、普通选项
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任务数:
-D mapred.reduce.tasks= 2
指定reducer个数,为0时,该作业只有mapper
※指定mapper输出分隔符:
-D stream.map.output.field.separator=.
指定mapper每条输出key,value分隔符
-D stream.num.map.output.key.fields=4
第4个 . 之前的部分为key,剩余为value
-D map.output.key.field.separator=.
设置map输出中,Key内部的分隔符
※指定基于哪些key进行分桶:
-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命令选项
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 |
(常用选项已经标注)
示例:
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
总结:
1、map操作会默认将输出按照key进行排序,而不管value
2、需自己指定关键字列,从而打到不同的reduce作业中
后续将更新mapreduce工作原理以及shuffle流程,以及好友推荐、搜索自动补全项目。
参考链接:
http://hadoop.apache.org/docs/current/hadoop-streaming/HadoopStreaming.html
https://www.cnblogs.com/shay-zhangjin/p/7714868.html