我有一个mapreduce程序,该程序的reduce方法输出Text作为键,并输出FloatArrayWritable作为值。但是,这些值将输出数组地址,而不是toString()方法的值。

我得到的输出是:
IYE marketDataPackage.MarketData@69204998IYE marketDataPackage.MarketData@69204998
输出应为:
IYE 38.89, 38.50, etc.
有人可以告知我代码中的错误吗?谢谢。

public static class Map extends Mapper<LongWritable, Text, Text, MarketData> {

    private Text symbol = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

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

        while (tokenizer.hasMoreTokens()) {

            StringTokenizer tokenizer2 = new StringTokenizer(tokenizer.nextToken().toString(), ",");
            symbol.set(tokenizer2.nextToken());
            context.write(symbol, new MarketData(tokenizer2.nextToken(), Float.parseFloat(tokenizer2.nextToken())));
        }
    }
}

public static class Reduce extends Reducer<Text, FloatWritable, Text, FloatArrayWritable> {

    public void reduce(Text key, Iterable<MarketData> values, Context context) throws IOException, InterruptedException, ParseException {

        Calendar today = Calendar.getInstance();
        today.add(Calendar.DAY_OF_MONTH, -45);
        Calendar testDate = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/m/d");

        List<FloatWritable> prices = new ArrayList<FloatWritable>();

        for (MarketData m : values) {

            testDate.setTime(sdf.parse(m.getTradeDate()));

            if (testDate.after(today)) {

                prices.add(new FloatWritable(m.getPrice()));
            }
        }

        context.write(key, new FloatArrayWritable(prices.toArray(new FloatWritable[prices.size()])));
    }
}

public static void main(String[] args) {

    Configuration conf = new Configuration();

    Job job = new Job(conf, "Security_Closing_Prices");

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(MarketData.class);

    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.waitForCompletion(true);
}

FloatArrayWritable类:
public class FloatArrayWritable extends ArrayWritable {

public FloatArrayWritable() {

    super(FloatWritable.class);
}

public FloatArrayWritable(FloatWritable[] values) {
    super(FloatWritable.class, values);
}

@Override
public FloatWritable[] get() {
    return (FloatWritable[]) super.get();
}

@Override
public String toString() {

    FloatWritable[] values = get();
    String prices = "";

    for (FloatWritable f : values) {

        prices = prices + f.toString() + ", ";
    }

    if (prices != null && !prices.isEmpty()) {
        prices = prices.substring(0, prices.length() - 2);
    }

    return prices;
}

}

最佳答案

MarketData类应覆盖toString()。您没有为该类提供代码,但是我怀疑没有。

09-26 21:19