【问题标题】:Hadoop map reduce whole file input formatHadoop map减少整个文件输入格式
【发布时间】:2015-04-16 19:55:10
【问题描述】:

我正在尝试使用 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!");
    }

}

我做错了什么?

【问题讨论】:

  • pastebin的内容放到问题中。

标签: java hadoop mapreduce


【解决方案1】:

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

希望这会有所帮助。

【讨论】:

  • 嘿,更改没有帮助,因为现在 wholeFileInputFormat 类出现编译错误,但感谢您让我意识到我正在使用旧版本的 MapReduce api。(我以为我是不知何故使用最新的,因为我的hadoop版本是2.6)。所以我找到了一个在旧版本的 MapReduce api 上使用的解决方案,现在它正在工作。这是解决方案:stackoverflow.com/questions/11457700/…谢谢!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我们也遇到了类似的情况,并采用了另一种开箱即用的方法。

    假设您需要处理 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多