【发布时间】:2014-12-11 01:07:43
【问题描述】:
我在学习 Map reduce 时有以下疑问。如果有人能回答,那将是非常有帮助的。
我有两个映射器处理同一个文件 - 我使用 MultipleInputFormat 配置它们
映射器 1 - 预期输出 [提取文件的几列后]
a - 1234
b - 3456
c - 1345
Mapper 2 预期输出[提取同一文件的几列后]
a - Monday
b - Tuesday
c - Wednesday
还有一个 reducer 函数,它只输出它作为输入获得的键值对 所以我希望输出是我知道类似的键将被打乱以制作一个列表。
a - [1234,Monday]
b - [3456, Tuesday]
c - [1345, Wednesday]
但是我得到了一些奇怪的输出。我猜只有 1 个 Mapper 正在运行。 这不应该是预期的吗?每个映射器的输出会单独洗牌吗?两个映射器会并行运行吗?
如果这是一个蹩脚的问题,请原谅请理解我是 Hadoop 和 Map Reduce 的新手
下面是代码
//Mapper1
public class numbermapper extends Mapper<Object, Text, Text, Text>{
public void map(Object key,Text value, Context context) throws IOException, InterruptedException {
String record = value.toString();
String[] parts = record.split(",");
System.out.println("***Mapper number output "+parts[0]+" "+parts[1]);
context.write(new Text(parts[0]), new Text(parts[1]));
}
}
//Mapper2
public class weekmapper extends Mapper<Object, Text, Text, Text> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String record = value.toString();
String[] parts = record.split(",");
System.out.println("***Mapper week output "+parts[0]+" "+parts[2]);
context.write(new Text(parts[0]), new Text(parts[2]));
}
}
//Reducer
public class rjoinreducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Text values, Context context)
throws IOException, InterruptedException {
context.write(key, values);
}
}
//Driver class
public class driver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "Reduce-side join");
job.setJarByClass(numbermapper.class);
job.setReducerClass(rjoinreducer.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
MultipleInputs.addInputPath(job, new Path(args[0]),TextInputFormat.class, numbermapper.class);
MultipleInputs.addInputPath(job, new Path(args[0]),TextInputFormat.class, weekmapper.class);
Path outputPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outputPath);
outputPath.getFileSystem(conf).delete(outputPath);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
这是我得到的 O/P-
a Monday
b Tuesday
c Wednesday
使用的数据集
a,1234,Monday
b,3456,Tuesday
c,1345,Wednesday
【问题讨论】:
-
你的奇怪输出是什么?
-
您能否提供一个演示,说明您如何编写代码以及“奇怪的输出”是什么?
-
我编辑了问题以包括 I/P O/P 和我使用的代码。它只是给出第二个映射器的输出。