Java 如何使用hadoop mapreduce编程计算文件中特定单词的出现次数?

Java 如何使用hadoop mapreduce编程计算文件中特定单词的出现次数?,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我试图使用java中的hadoop mapreduce编程计算文件中特定单词的出现次数。文件和单词都应该是用户输入。因此,我尝试将特定的单词作为第三个参数与I/p和o/p路径(In、Out、word)一起传递。但是我无法找到一种方法将单词传递给map函数。 我尝试过以下方法,但不起作用: -在mapper类中创建了一个静态字符串变量,并将我的第三个参数(即要搜索的单词)的值赋给它。然后尝试在map函数中使用这个静态变量。但在map函数中,静态变量值为Null。 我无法在map函数中获取第三个ar

我试图使用java中的hadoop mapreduce编程计算文件中特定单词的出现次数。文件和单词都应该是用户输入。因此,我尝试将特定的单词作为第三个参数与I/p和o/p路径(In、Out、word)一起传递。但是我无法找到一种方法将单词传递给map函数。 我尝试过以下方法,但不起作用: -在mapper类中创建了一个静态字符串变量,并将我的第三个参数(即要搜索的单词)的值赋给它。然后尝试在map函数中使用这个静态变量。但在map函数中,静态变量值为Null。 我无法在map函数中获取第三个arument的值

是否仍然可以通过JobConf对象设置值?请帮忙。我已将代码粘贴到下面

public class MyWordCount {

    public static class MyWordCountMap extends Mapper < Text, Text, Text, LongWritable > {
        static String wordToSearch;
        private final static LongWritable ONE = new LongWritable(1L);
        private Text word = new Text();
        public void map(Text key, Text value, Context context)
        throws IOException, InterruptedException {
            System.out.println(wordToSearch); // Here the value is coming as Null
            if (value.toString().compareTo(wordToSearch) == 0) {
                context.write(word, ONE);
            }
        }
    }


    public static class SumReduce extends Reducer < Text, LongWritable, Text, LongWritable > {

        public void reduce(Text key, Iterator < LongWritable > values,
            Context context) throws IOException, InterruptedException {
            long sum = 0L;
            while (values.hasNext()) {
                sum += values.next().get();
            }
            context.write(key, new LongWritable(sum));
        }
    }

    public static void main(String[] rawArgs) throws Exception {

        GenericOptionsParser parser = new GenericOptionsParser(rawArgs);
        Configuration conf = parser.getConfiguration();
        String[] args = parser.getRemainingArgs();
        Job job = new Job(conf, "wordcount");
        job.setJarByClass(MyWordCountMap.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        job.setMapperClass(MyWordCountMap.class);
        job.setReducerClass(SumReduce.class);
        job.setInputFormatClass(SequenceFileInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        String MyWord = args[2];
        MyWordCountMap.wordToSearch = MyWord;
        job.waitForCompletion(true);
    }

}
公共类MyWordCount{
公共静态类MyWordCountMap扩展了映射器{
静态字符串字搜索;
私有最终静态长可写1=新的长可写(1L);
私有文本字=新文本();
公共无效映射(文本键、文本值、上下文)
抛出IOException、InterruptedException{
System.out.println(wordToSearch);//这里的值为Null
if(value.toString().compareTo(wordToSearch)==0){
上下文。写(单词,一);
}
}
}
公共静态类SumReduce扩展了Reducer{
public void reduce(文本键,迭代器值,
上下文)抛出IOException、InterruptedException{
长和=0L;
while(values.hasNext()){
sum+=values.next().get();
}
write(key,新的LongWritable(sum));
}
}
公共静态void main(字符串[]rawArgs)引发异常{
GenericOptionsParser=新的GenericOptionsParser(rawArgs);
conf=parser.getConfiguration();
字符串[]args=parser.getRemainingArgs();
Job Job=新作业(conf,“wordcount”);
job.setJarByClass(MyWordCountMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
setMapperClass(MyWordCountMap.class);
job.setReducerClass(SumReduce.class);
作业.setInputFormatClass(SequenceFileInputFormat.class);
setOutputFormatClass(TextOutputFormat.class);
addInputPath(作业,新路径(args[0]);
setOutputPath(作业,新路径(args[1]);
字符串MyWord=args[2];
MyWordCountMap.wordToSearch=MyWord;
job.waitForCompletion(true);
}
}

有一种方法可以通过
配置实现这一点(请参见api)。例如,可以使用以下代码将“Tree”设置为要搜索的单词:

//Create a new configuration
Configuration conf = new Configuration();
//Set the work to be searched
conf.set("wordToSearch", "Tree");
//create the job
Job job = new Job(conf);
然后,在mapper/reducer类中,您可以使用以下命令获得
wordToSearch
(即本例中的“树”):

//Create a new configuration
Configuration conf = context.getConfiguration();
//retrieve the wordToSearch variable
String wordToSearch = conf.get("wordToSearch");

有关详细信息,请参阅。

非常感谢Sam。。这对我来说非常有效。非常感谢您的时间和努力……)@GoodBoy,如果可以,你能分享完整的程序吗:)