Hadoop使用KeyValueTextInputFormat

whi*_*ail 8 java hadoop mapreduce word-count

我使用hadoop 1.0.1做一些项目,我想让我的输入.txt文件成为我需要的"关键"和"值",如:

如果我有一个test.txt文件,文件内容是

1,10 10

我认为可以用"KeyValueTextInputFormat",使""是分离的符号,所以输入之后,关键'1’'10 10’.

但是,我得到的结果是所有信息都是关键,值是空的.我不知道问题出在哪里.

请给我一些帮助,谢谢!

这是示例代码:

public class WordCount{
    public class WordCountMapper extends Mapper<Text, Text, Text, Text>{  

        public void map(Text key, Text value, Context context) throws IOException, InterruptedException {
            context.write(value, value);
            context.write(key, key);
        }   
      }
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("key.value.separator.in.input.line",",");
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length != 2) {
          System.err.println("Usage: wordcount <in> <out>");
          System.exit(2);
        }
        Job job = new Job(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountMapper.class);
        job.setInputFormatClass(KeyValueTextInputFormat.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        KeyValueTextInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
      }
}
Run Code Online (Sandbox Code Playgroud)

fai*_*zan -1

输入文件被转换为键值对,并且将为所有此类对调用映射函数。现在,在您的示例中,映射的输入将是某个键(可能是 1,因为它是文件中的行号),最重要的是您的值将是 1,10 10。

现在,您可以从映射器输出任何内容,只有在对映射器的所有输出进行交换和排序后,这些内容才会进入减速器类的reduce 函数。

因此,如果您从映射器输出 context.write(value) 并从化简器输出相同的内容,您将从所有文件中获得唯一的行。

我认为我没有解释你想要什么,但这是 Hadoop Map-Reduce 中发生的基本事情。