java - Hadoop、MapReduce - 多输入/输出路径

标签 java hadoop mapreduce

在为我的 MapReduce 作业制作 Jar 时,在我的输入文件中,我使用了 Hadoop-local 命令。我想知道是否有一种方法,而不是专门指定我的输入文件夹中要在 MapReduce 作业中使用的每个文件的路径,我是否可以只指定并传递我的输入文件夹中的所有文件。这是因为由于我尝试配置的 MapReduce 作业的性质,文件的内容和数量可能会发生变化,而且我不知道文件的具体数量,除了这些文件的内容之外,有没有办法将输入文件夹中的所有文件传递到我的 MapReduce 程序,然后遍历每个文件以计算某个函数,然后将结果发送到 Reducer。我只使用一个 Map/Reduce 程序并且我正在用 Java 编码。我可以使用 hadoop-moonshot 命令,但目前我正在使用 hadoop-local。

谢谢。

最佳答案

您不必将单个文件作为 MapReduce 作业的输入。

FileInputFormat类已经提供 API 来接受多个文件列表作为 Map Reduce 程序的输入。

public static void setInputPaths(Job job,
                 Path... inputPaths)
                          throws IOException

Add a Path to the list of inputs for the map-reduce job. Parameters:

conf - The configuration of the job

path - Path to be added to the list of inputs for the map-reduce job.

来自 Apache 的示例代码 tutorial

Job job = Job.getInstance(conf, "word count");
FileInputFormat.addInputPath(job, new Path(args[0]));

MultipleInputs提供以下 API。

public static void addInputPath(Job job,
                Path path,
                Class<? extends InputFormat> inputFormatClass,
                Class<? extends Mapper> mapperClass)

Add a Path with a custom InputFormat and Mapper to the list of inputs for the map-reduce job.

相关的 SE 问题:

Can hadoop take input from multiple directories and files

引用MultipleOutputs关于您对多个输出路径的第二个查询的 API。

FileOutputFormat.setOutputPath(job, outDir);

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

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

查看有关多个输出文件的相关 SE 问题。

Writing to multiple folders in hadoop?

hadoop method to send output to multiple directories

关于java - Hadoop、MapReduce - 多输入/输出路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37229646/

相关文章:

hadoop - 在使用 MapReduce 执行字数统计时,是否可以在 map 函数中将数据拆分为字词?

hadoop - 启用安全性后,运行任何Hadoop命令都会失败。

java - Gradle:强制使用另一个操作系统

hadoop - 为什么cloudera建议选择他们在Spark中做的executors、cores和RAM的数量

python - 从我保存在本地文件系统上的配置单元查询输出中删除空行

security - 在 Hadoop 环境中使用多个 AWS key

java - JAR 到 EXE 转换器?

java - 编译器先编译Object还是String?

java - 在 OS X 上使用屏幕菜单栏时如何使菜单项加速键起作用

javascript - 在 couchdb reduce 中使用 "eval"- 这有多危险?