在代码的第一部分我设置了一些数组来跟踪这样的一些值:
@Override
public void configure(JobConf conf){
top5=new String[5];
counttop5=new int[5]
}
现在,在reducer的一些代码之后,我想将top的内容输出到输出文件,但是,为了做到这一点,我创建了一个close()函数:
@Override
public void close(){
//what goes here?
}
但是你可以看到没有什么可以调用的,因为输出是在reducer方法中定义的。 虽然代码本身很长,但这里是方法的数据签名:
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Writable> {
private static IntWritable one = new IntWritable(1);
public void map(LongWritable key, Text value, OutputCollector<Text, Writable> output, Reporter reporter) throws IOException {
public static class Reduce extends MapReduceBase implements Reducer<Text, Writable, Text, Writable> {
static ArrayList<String> files;
static String[] top5;
static int[] counttop5;
int reducedterms;
public void configure(JobConf conf){
files= new ArrayList<String>();
top5=new String[5];
reducedterms=0;
counttop5=new int[5];
}
@Override
public void close(){
//what goes here?
}
public void reduce(Text key, Iterator<Writable> values, OutputCollector<Text, Writable> output, Reporter reporter) throws IOException {
}
有人知道修复吗?
答案 0 :(得分:1)
这是使用org.apache.hadoop.mapreduce
类的答案。 API与您提供的代码略有不同,因为我们现在扩展org.apache.hadoop.mapreduce.Reducer
基类而不是实现org.apache.hadoop.mapred.Reducer
接口,但这是一种直接的方式来满足您的需求:
public static class Reduce extends Reducer<Text, Writable, Text, Writable> {
static ArrayList<String> files;
static String[] top5;
static int[] counttop5;
int reducedterms;
//setup method instead of configure
public void setup(Context context){
files= new ArrayList<String>();
top5=new String[5];
reducedterms=0;
counttop5=new int[5];
}
//In cleanup you may access the write method
@Override
public void cleanup(Context context){
// Use top5 and counttop5 the way you see fit
// Use context.write() to pass them ahead!
}
public void reduce(Text key, Iterator<Writable> values, Context context) throws IOException {
}