2017-01-18 5 views
2

Пожалуйста, мне нужна помощь в этом коде:Как построить метод buildOutputValue

public class ThirdQueryReducer extends 
    Reducer<IntWritable, Text, NullWritable, Text> { 

private NullWritable nullWritableKey = NullWritable.get(); 
private Text outputValue = new Text(); 

private StringBuilder buildOutputValue(IntWritable key, 
     StringBuilder reduceValueBuilder, Text value) { 


} 

@Override 
public void reduce(IntWritable key, Iterable<Text> values, Context context) 
     throws IOException, InterruptedException { 


}} 

Как я Коула реализовать buildOutputValue? зная, что выход в картографа, как это: mapDriver.withOutput(new IntWritable(1981), new Text("Mercurey"));

И в buildOutputValue() должен обрабатывать даты, как это:

List<Text> values = new ArrayList<Text>(); values.add(new Text("Pommard")); values.add(new Text("Gentil")); reduceDriver.withInput(new IntWritable(1980), values);

И выход в редукторе, как это:

reduceDriver.withOutput(NullWritable.get(), new Text("Pommard\nGentil"));

заранее спасибо

ответ

1

Вы можете implem Ent его, как показано ниже:

общественный класс ThirdQueryReducer расширяет Reducer {

private NullWritable nullWritableKey = NullWritable.get(); 
private Text outputValue = new Text(); 

private StringBuilder buildOutputValue(IntWritable key, 
     StringBuilder reduceValueBuilder, Text value) { 
    return reduceValueBuilder.append(value); 
} 

@Override 
public void reduce(IntWritable key, Iterable<Text> values, Context context) 
     throws IOException, InterruptedException { 
    Iterator<Text> valueIter = values.iterator(); 
    StringBuilder reduceValueBuilder = new StringBuilder(); 
    while (valueIter.hasNext()) { 
     buildOutputValue(key, valueIter.next(), reduceValueBuilder); 
     if (valueIter.hasNext()) { 
      reduceValueBuilder.append("\n"); 
     } 
    } 
    context.write(NullWritable.get(), new Text(reduceValueBuilder.toString())); 
} 

}

+0

Спасибо, это отличная альтернатива! –