Hadoop map减少整个文件输入格式

时间:2015-04-16 19:55:11

标签: java hadoop mapreduce

我正在尝试使用hadoop map reduce,但我不是一次映射Mapper中的每一行,而是想一次映射整个文件。

所以我找到了这两个类 (https://code.google.com/p/hadoop-course/source/browse/HadoopSamples/src/main/java/mr/wholeFile/?r=3) 那可以帮助我做到这一点。

我收到了一个编译错误:

  

类型中的方法setInputFormat(Class)   JobConf不适用于参数   (类)Driver.java / ex2 / src第33行Java   问题

我将我的Driver类更改为

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.InputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

import forma.WholeFileInputFormat;

/*
 * Driver
 * The Driver class is responsible of creating the job and commiting it.
 */
public class Driver {
    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(Driver.class);
        conf.setJobName("Get minimun for each month");

        conf.setOutputKeyClass(IntWritable.class);
        conf.setOutputValueClass(IntWritable.class);

        conf.setMapperClass(Map.class);
        conf.setCombinerClass(Reduce.class);
        conf.setReducerClass(Reduce.class);

        // previous it was 
        // conf.setInputFormat(TextInputFormat.class);
        // And it was changed it to :
        conf.setInputFormat(WholeFileInputFormat.class);

        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf,new Path("input"));
        FileOutputFormat.setOutputPath(conf,new Path("output"));

        System.out.println("Starting Job...");
        JobClient.runJob(conf);
        System.out.println("Job Done!");
    }

}

我做错了什么?

3 个答案:

答案 0 :(得分:1)

确保您的wholeFileInputFormat类具有正确的导入。您在作业驱动程序中使用旧的MapReduce Api。我认为您在WholeFileInputFormat类中导入了新的API FileInputFormat。如果我是对的,您应该在wholeFileInputFormat类中导入 org.apache.hadoop.mapred.FileInputFormat ,而不是 org.apache.hadoop.mapreduce.lib.input.FileInputFormat

希望这有帮助。

答案 1 :(得分:1)

最简单的方法是gzip你的输入文件。这将使FileInputFormat.isSplitable()返回false。

答案 2 :(得分:1)

我们也碰到了类似的东西,并采用了另一种开箱即用的方法。

假设你需要处理100个大文件(f1,f2,...,f100),这样你就需要在map函数中读取一个完全的文件。因此,而不是使用" WholeInputFileFormat"读者方法我们创建了等效的10个文本文件(p1,p2,...,p10),每个文件包含f1-f100文件的HDFS URL或Web URL。

因此p1将包含f1-f10的url,p2将为f11-f20包含url等等。

然后将这些新文件p1到p10用作映射器的输入。因此,映射器m1处理文件p1将一次打开文件f1到f10并完全处理它。

这种方法允许我们控制映射器的数量,并在map-reduce应用程序中编写更详尽和复杂的应用程序逻辑。例如,我们可以使用这种方法在PDF文件上运行NLP。