mapreduce-floatarraywritable打印地址

kmbjn2e3  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(310)

我有一个mapreduce程序谁的reduce方法输出一个 Text 作为钥匙和钥匙 FloatArrayWritable 作为价值观。但是,这些值输出的是数组地址,而不是 toString() 方法。
我得到的结果是: IYE marketDataPackage.MarketData@69204998 IYE 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;
}

}
6ju8rftf

6ju8rftf1#

这个 MarketData 类应重写 toString() . 你没有为那个类提供代码,但我怀疑它没有。

相关问题