Created
November 25, 2012 15:40
-
-
Save xmzhao/4144079 to your computer and use it in GitHub Desktop.
[hadoop] Passing parameters to Mappers and Reducer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://www.thecloudavenue.com/2011/11/passing-parameters-to-mappers-and.html | |
There might be a requirement to pass additional parameters to the mapper and reducers, besides the the inputs which they process. Lets say we are interested in Matrix multiplication and there are multiple ways/algorithms of doing it. We could send an input parameter to the mapper and reducers, based on which the appropriate way/algorithm is picked. There are multiple ways of doing this | |
Setting the parameter: | |
1. Use the -D command line option to set the parameter while running the job. | |
2. Before launching the job using the old MR API | |
JobConf job = (JobConf) getConf(); | |
job.set("test", "123"); | |
3. Before launching the job using the new MR API | |
Configuration conf = new Configuration(); | |
conf.set("test", "123"); | |
Job job = new Job(conf); | |
Getting the parameter: | |
1. Using the old API in the Mapper and Reducer. The JobConfigurable#configure has to be implemented in the Mapper and Reducer class. | |
private static Long N; | |
public void configure(JobConf job) { | |
N = Long.parseLong(job.get("NumberOfDocuments")); | |
} | |
The variable N can then be used with the map and reduce functions. | |
2. Using the new API in the Mapper and Reducer. The context is passed to the setup, map, reduce and cleanup functions. | |
Configuration conf = context.getConfiguration(); | |
String param = conf.get("test"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment