gpt4 book ai didi

java - hadoopOutputCollector空指针异常

转载 作者:行者123 更新时间:2023-12-01 15:07:51 25 4
gpt4 key购买 nike

我正在尝试实现内存排序,即根据编号进行排序。计数。我面临以下问题,Reduce 类的 close 方法中的 output.collect 中存在 null pinter 异常。请帮忙!

我的编码逻辑正确吗?我将来自reduce 方法的不同实例的标记保留在内存中。请帮我!我想要基于 TCount 的排序输出。

package com.a;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;


public class Map1 extends MapReduceBase implements Mapper<LongWritable, Text, Text,
Text >{
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output,
Reporter reporter) throws IOException {

StringTokenizer tokenizer = new StringTokenizer(value.toString());

String tk = tokenizer.nextToken();
String id = tokenizer.nextToken();
String name = tokenizer.nextToken();

StringTokenizer tkz = new StringTokenizer(name, ",");


ArrayList<String> al = new ArrayList<String>();

while(tkz.hasMoreTokens())
{
name = tkz.nextToken();
al.add(name);
}

for(int i = 0; i<al.size(); i++)
{
output.collect(new Text(t+" "+al.get(i)), new Text("1"));
System.out.println("out key:----->"+t+" "+al.get(i));
}

}

}



public class Reduce1 extends MapReduceBase implements Reducer<Text, Text, Text, Text>{
// @SuppressWarnings("unchecked")

ArrayList<TCount> al = new ArrayList<TCount>();
String key_str = null;
private OutputCollector<Text, Text> output;



public void reduce (Text key, Iterator<Text> values, OutputCollector<Text,
Text> output, Reporter reporter) throws IOException {

int sum = 0;

while(values.hasNext())
{
String val = values.next().toString();
sum = sum+Integer.parseInt(val);;

}

String str_val = String.valueOf(sum);
key_str = key.toString();
//output.collect(key, new Text(str_val));
TCount tc = new TCount(key.toString(), sum);
al.add(tc);


}

private Text t = new Text();
private Text txt_key = new Text();


public void close() throws IOException {
Collections.sort(al);

for(int i = 0; i<al.size(); i++)
{

String tkn = al.get(i).getT();
System.out.println("token:-------------------> "+tkn);

System.out.println("output: "+output);
txt_key = new Text(t);
txt = new Text(String.valueOf(al.get(i).getCount()));
output.collect(txt_key, t);

}
}
}

最佳答案

在类Reduce1中,您声明输出对象:

private OutputCollector<Text, Text> output; 

没有初始化它 -> 所以此时它为空。

也在方法reduce()中你传递了一个相同类型的参数( OutputCollector<Text, Text> output ) -> 所以在这个方法中,我猜你想说:

this.output=output;
// if the object is null, initialize it if you wanna use it

问题是,只要您的对象未初始化(实例化),您就会遇到空指针异常。

关于java - hadoopOutputCollector空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com