MapReduce输出为ArrayList

时间:2013-07-01 10:03:45

标签: hadoop mapreduce

如何在普通的java项目中调用map reduce方法,是否可以将reducer输出作为Arraylist / Hashmap而不是平面文件返回,以及如何从jboss appServer访问mapreduce方法。

1 个答案:

答案 0 :(得分:0)

以下是使用MultipleOutput

的示例程序
    public void reduce(Text key, Iterator<IntWritable> values,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        int total = 0;
          for (; values.hasNext();) {
            total += values.next().get();
            mos.getCollector("text", reporter).collect(key,
                    new IntWritable(total));
            mos.getCollector("seq", reporter).collect(key,
                    new IntWritable(total));
        }

    }

您需要在configure方法中创建MultipleOutputs实例。

    private MultipleOutputs mos;

    @Override
    public void configure(JobConf job) {

        mos = new MultipleOutputs(job);
    }

在你的驱动程序类中,你需要知道你想要使用哪些输入格式。下面将以文本和序列文件格式生成输出。

// Defines additional single text based output 'text' for the job
    MultipleOutputs.addNamedOutput(conf, "text", TextOutputFormat.class,
            Text.class, IntWritable.class);

    // Defines additional sequence-file based output 'sequence' for the job
    MultipleOutputs.addNamedOutput(conf, "seq",
            SequenceFileOutputFormat.class, Text.class, IntWritable.class);

但是从我对你的问题的理解,你基本上想要从你的代码中访问你的mapreduce输出。您可以使用HDFS API下载输出文件。但更好的方法是将您的数据放在Hive表中并使用JDBC进行访问。