【问题标题】:hadoop give output after all reducers are donehadoop 在所有 reducer 完成后给出输出
【发布时间】:2013-12-02 11:38:25
【问题描述】:

在代码的第一部分,我设置了一些数组来跟踪一些值,如下所示:

  @Override 
  public void configure(JobConf conf){
       top5=new String[5];
      counttop5=new int[5]
      }

现在在 reducer 的一些代码之后,我想将顶部的内容输出到输出文件,但是,为了做到这一点,我创建了一个 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 {
    }

有人知道解决办法吗?

【问题讨论】:

  • “代码的第一部分”是指在映射器内部还是之前?
  • 我已经添加了函数的数据签名,但是文件很大,我怀疑它是否适合。
  • 谢谢(并对噪音感到抱歉)但是扩展org.apache.hadoop.mapreduce.Reducer而不是实现org.apache.hadoop.mapred.Reducer接口是不可能的?如果没有,我想我有一个答案。
  • 扩展而不是实施,谢谢!
  • @ArtemTsikiridis 应该写一个答案,你可以接受它,并奖励赏金。

标签: java hadoop counter cloudera


【解决方案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 {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2015-01-07
    • 2018-08-02
    • 2016-06-16
    • 2014-06-04
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多