1.run
public void run(Context context) throws IOException, InterruptedException {
setup(context);
try {
while (context.nextKeyValue()) {
map(context.getCurrentKey(), context.getCurrentValue(), context);
}
} finally {
cleanup(context);
}
}
mapper的run在执行时不止执行map
map是在每读到一行数据,就执行一次map
在读开始之前有一次setup数据准备
在读完之后进行一次cleanup数据销毁
2.setup
/**
* Called once at the beginning of the task.
* 任务开始时调用一次
*/
protected void setup(Context context
) throws IOException, InterruptedException {
// NOTHING
}
3.map
不设置参数也可以正常运行
但是输出的是把map的东西直接输出了
/**
* Called once for each key/value pair in the input split. Most applications
* should override this, but the default is the identity function.
* 对输入拆分中的每个键/值对调用一次。大多数应用程序应该覆盖这个,但是默认值是identity函数。
*/
@SuppressWarnings("unchecked")
protected void map(KEYIN key, VALUEIN value,
Context context) throws IOException, InterruptedException {
context.write((KEYOUT) key, (VALUEOUT) value);
}
4.cleanup 释放资源
/**
* Called once at the end of the task.
*/
protected void cleanup(Context context
) throws IOException, InterruptedException {
// NOTHING
}