我需要知道我正在使用的输入文件的分区的行索引。我可以通过将行索引连接到数据在原始文件中强制执行此操作,但我宁愿在Hadoop中执行此操作。我在映射器中有这个...在Hadoop中获取输入文件的分区ID
String id = context.getConfiguration().get("mapreduce.task.partition");
但是在任何情况下“id”都是0。在“Hadoop:权威指南”中,它提到访问属性,如分区ID“可以通过传递给Mapper或Reducer的所有方法的上下文对象来访问”。从我所知道的来看,它并没有真正涉及如何获取这些信息。
我浏览了Context对象的文档,看起来上面是这样做的方法,脚本也会编译。但是因为每个价值都是0,所以我不确定我是否真的使用了正确的东西,但我无法在网上找到任何可以帮助我们搞清楚的细节。
代码用来测试...
public class Test {
public static class TestMapper extends Mapper<LongWritable, Text, Text, Text> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String id = context.getConfiguration().get("mapreduce.task.partition");
context.write(new Text("Test"), new Text(id + "_" + value.toString()));
}
}
public static class TestReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for(Text value : values) {
context.write(key, value);
}
}
}
public static void main(String[] args) throws Exception {
if(args.length != 2) {
System.err.println("Usage: Test <input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(Test.class);
job.setJobName("Test");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(TestMapper.class);
job.setReducerClass(TestReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
我不清楚关于输入文件分区的行索引究竟是什么意思。你能澄清吗? –
@BinaryNerd我可能是错的,但我认为这将是输入文件的行ID。所以说如果文件中有100行,我希望知道映射器正在工作的当前行是什么(所以从0-99或1-100的数字) – cpd1