模板代码可能看起来像这样(Reducer 未显示,但主体相同)
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class ToolExample extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Job job = new Job(getConf());
Configuration conf = job.getConfiguration();
conf.set("strProp", "value");
conf.setInt("intProp", 123);
conf.setBoolean("boolProp", true);
// rest of your config here
// ..
return job.waitForCompletion(true) ? 0 : 1;
}
public static class MyMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
private String strProp;
private int intProp;
private boolean boolProp;
@Override
protected void setup(Context context) throws IOException,
InterruptedException {
Configuration conf = context.getConfiguration();
strProp = conf.get("strProp");
intProp = conf.getInt("intProp", -1);
boolProp = conf.getBoolean("boolProp", false);
}
}
public static void main(String args[]) throws Exception {
System.exit(ToolRunner.run(new ToolExample(), args));
}
}