我需要知道我正在使用的輸入文件的分區的行索引。我可以通過將行索引連接到數據在原始文件中強制執行此操作,但我寧願在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