gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 14:45:57 26 4
gpt4 key购买 nike

我正在尝试使用 java 中的 hadoop mapreduce 编程计算文件中特定 单词的出现次数。文件和单词都应该是用户输入。所以我试图将特定单词作为第三个参数与 i/p 和 o/p 路径一起传递 (In, Out, Word)。但我无法找到将单词传递给 map 功能的方法。我尝试了以下方法,但没有用: - 在映射器类中创建了一个静态字符串变量,并将我的第三个参数(即要搜索的词)的值分配给它。然后尝试在 map 函数中使用这个静态变量。但在 map 函数内部,静态变量值为 Null。我无法在 map 函数中获取第三个参数的值。

是否可以通过 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);
}

}

最佳答案

有一种方法可以使用 Configuration 来做到这一点(参见 api here )。例如,可以使用以下代码将“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);

然后,在您的映射器/缩减器类中,您可以使用以下方法获取 wordToSearch(即本例中的“Tree”):

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

参见 here了解更多详情。

关于java - 如何使用 hadoop mapreduce 编程计算文件中特定单词的出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com