我最近开始学习hadoop。现在,我想在本地磁盘中打开一个文件,并在 reduce 函数中将一些数据写入该文件,但我找不到关闭该文件的好方法。
据我所知,关闭并重新打开它不是一个好主意,所以我不想这样做。
public class MyClass extends Configured implements Tool{
main(){
//all configurations here
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
}
static class MyMapper extends Mapper <LongWritable,Text,Text,Text>{
//does something
}
static class MyReducer extends Reducer <LongWritable,Text,Text,Text>{
//create file, filewriter etc here
public MyReducer() {
//open a file here
}
public reduce(){
//write to file here
bw.write("entered the reduce task for " + key);
while(there is more item)
bw.write( value + " will be written to my file \n");
}
}
}
工作流程将如下所示(如果我错了,请纠正我):
for(each reduce task)
write to file "entered the reduce task for " + *key*
for each *value* for that *key*
write *value*
我想将键/值对写入本地磁盘上写入的 myfile,然后想关闭该文件,但我找不到解决该问题的好方法。或者这会是一个问题,如果我不关闭文件,我的意思是,hadoop 会处理这个问题吗?
谢谢,