0

我正在考虑如何将 Hadoop 的输出写入 txt 文件,而不是写入 HDFS。例如,我把下面的代码:

    // Create the job specification object
    Job job1 = new Job();
    job1.setJarByClass(Main.class);
    job1.setJobName("Day Measurment");

    // Setup input and output paths
    FileInputFormat.addInputPath(job1, new Path(args[0]));
    FileOutputFormat.setOutputPath(job1, new Path(args[1]));

    // Set the Mapper and Reducer classes
    job1.setMapperClass(DayMapper.class);
    job1.setReducerClass(LogReducer.class);

    // Specify the type of output keys and values
    job1.setOutputKeyClass(Text.class);
    job1.setOutputValueClass(LongWritable.class);

    // Wait for the job to finish before terminating
    job1.waitForCompletion(true);

    PrintWriter pw = new PrintWriter("hadoop.csv");
    pw.println("abc");
    pw.close();

在我测试我的程序后,Hadoop 工作正常,但我只得到 hadoop.csv,里面没有内容。它是一个空文件,里面没有“abc”。

谁能告诉我为什么?或者告诉我如何将输出打印到常规文件(.csv 或 .log)中,而不是打印到 HDFS 中?

4

2 回答 2

2

默认情况下,创建的 PrintWriter 对象不使用 flush()。要打开它,您可以在创建 PrintWriter 时向构造函数添加第二个参数。

PrintWriter pw = new PrintWriter(fw,true); 

如果您不想这样做,您应该可以简单地使用flush()- 方法

    PrintWriter pw = new PrintWriter("hadoop.csv");
        pw.println("abc");
        pw.flush();    
        pw.close();

使用flush()将确保要写入的任何数据都不会卡在任何内部缓冲区中,而只是简单地推送到底层输出流中。

看看这个:PrintWriter - Java API

于 2013-07-16T20:48:22.667 回答
0
        FileWriter fw = new FileWriter("hadoop.csv");
        PrintWriter pw = new PrintWriter(fw);

        pw.println("abc");

        pw.flush();
        pw.close();
        fw.close();  
于 2013-07-16T20:43:51.700 回答