输出收集器hadoop中的空指针异常

时间:2012-07-25 22:31:03

标签: hadoop mapreduce

我在reduce类的close方法中有输出收集器。输出收集器的格式为<Text, Text>

我传递Text name_txt作为键,Text val作为值。

我发现Text name_txt和Text val都不是通过打印它们为空,但是当我在输出中设置时,我在output.collect(name_str,val)中得到空指针异常;

为了清楚起见,我附上了以下代码。请帮助!

private OutputCollector<Text, Text> output;

public void close() throws IOException {          
  for(int i = 0; i<arraylist.size(); i++)
  {
    String name = arraylist.get(i).getToken();
    Text name_txt = new Text(name);
    System.out.println("name_txt: "+name_txt);
    Text val = new Text("hi");
    output.collect(name_txt, val);         
  }         
}

1 个答案:

答案 0 :(得分:0)

您在哪里设置输出值?

我看到你正在使用旧的API(mapred),所以你没有奢侈的设置方法。相反,您可能需要在map方法中添加条件:

void map(K1 key, V1 value, OutputCollector<K2, V2> output, Reporter reporter)
  throws IOException {
  if (this.output == null) {
    this.output = output;
  }

  // your current map implementation goes here..
}