Reputation: 5785
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]);
Upvotes: 18
Views: 27857
Reputation: 33555
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");
Upvotes: 48