大家好stackoverflow的好朋友们,
我运行了一个mapreduce代码,在文件中查找唯一的单词。输入数据集(文件)位于hdfs的文件夹中。所以我在运行mapreduce程序时输入了文件夹的名称。
我不知道在同一个文件夹里还有另外两个文件。mapreduce程序继续运行,读取所有3个文件并给出输出。输出良好。
这是mapreduce的默认行为吗?这意味着如果您指向一个文件夹而不仅仅是一个文件(作为输入数据集),mapreduce会使用该文件夹中的所有文件吗?我感到惊讶的原因是,在mapper中,没有读取多个文件的代码。我知道驱动程序中的第一个参数args[0]是我给出的文件夹名。
这是驱动程序代码:
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class DataSort {
public static void main(String[] args) throws Exception {
/*
* Validate that two arguments were passed from the command line.
*/
if (args.length != 2) {
System.out.printf("Usage: StubDriver <input dir> <output dir>\n");
System.exit(-1);
}
Job job=Job.getInstance();
/*
* Specify the jar file that contains your driver, mapper, and reducer.
* Hadoop will transfer this jar file to nodes in your cluster running
* mapper and reducer tasks.
*/
job.setJarByClass(DataSort.class);
/*
* Specify an easily-decipherable name for the job.
* This job name will appear in reports and logs.
*/
job.setJobName("Data Sort");
/*
* TODO implement
*/
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(ValueIdentityMapper.class);
job.setReducerClass(IdentityReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
/*
* Start the MapReduce job and wait for it to finish.
* If it finishes successfully, return 0. If not, return 1.
*/
boolean success = job.waitForCompletion(true);
System.exit(success ? 0 : 1);
}
}
Map程序代码:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ValueIdentityMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line=value.toString();
for (String word:line.split("\\W+"))
{
if (word.length()>0)
{
context.write(new Text(word),new IntWritable(1));
}
}
}
}
减速机代码:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class IdentityReducer extends Reducer<Text, IntWritable, Text, Text> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
String word="";
context.write(key, new Text(word));
}
}
1条答案
按热度按时间sg24os4d1#
这是mapreduce的默认行为吗?
不是mapreduce,只是你使用的输入格式。
FileInputFormat
api参考setInputPaths(JobConf conf, Path... inputPaths)
设置数组Path
s作为map reduce作业的输入列表。Path
api参考在文件中命名文件或目录
FileSystem
.所以,当你说
没有读取多个文件的代码
是的,确实有,只是不用写。
Mapper<LongWritable, Text,
正确处理指定区域中所有文件的所有文件偏移InputFormat
.