18

I'm using new Hadoop API and looking for a way to pass some parameters (few strings) to mappers.
How can I do that?

This solutions works for old API:

JobConf job = (JobConf)getConf();
job.set("NumberOfDocuments", args[0]);

Here, “NumberOfDocuments” is the name of parameter and its value is read from “args[0]“, a command line argument. Once you set this arguments, you can retrieve its value in reducer or mapper as follows:

private static Long N;
public void configure(JobConf job) {
     N = Long.parseLong(job.get("NumberOfDocuments"));
}

Note, the tricky part is that you cannot set parameters like this:

Configuration con = new Configuration();
con.set("NumberOfDocuments", args[0]);
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
wlk
  • 5,695
  • 6
  • 54
  • 72
  • both new and old methods are here: http://www.thecloudavenue.com/2011/11/passing-parameters-to-mappers-and.html – weefwefwqg3 Nov 21 '16 at 17:50

1 Answers1

48

In the main method set the required parameter as below or using the -D command line option while running the job.

Configuration conf = new Configuration();
conf.set("test", "123");

Job job = new Job(conf);

In the mapper/reducer get the parameter as

Configuration conf = context.getConfiguration();
String param = conf.get("test");
barik
  • 160
  • 2
  • 12
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117