所以我的任务是返回一个包含在文本文件中的所有单词的排序列表,同时保留重复。Hadoop MapReduce:返回文本文件中单词的排序列表
{生存还是毁灭} - →{是不或向}
我的想法是把每个单词为重点,以及价值。这样,因为hadoop对键进行排序,它们将自动按字母顺序排序。在Reduce阶段,我只需将具有相同键(所以基本上相同的单词)的所有单词附加到单个文本值。
public class WordSort {
public static class Map extends Mapper<LongWritable, Text, Text, Text> {
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
// transform to lower case
String lower = word.toString().toLowerCase();
context.write(new Text(lower), new Text(lower));
}
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String result = "";
for (Text value : values){
res += value.toString() + " ";
}
context.write(key, new Text(result));
}
}
但我的问题是,如何我只是在我的输出文件返回值?目前,我有这样的:
be be be
not not
or or
to to to
因此,在每一行我有钥匙,然后再价值观,但我只想让我得到返回的值是:
be be
not
or
to to
是这甚至可能或者我必须从每个单词的值中删除一个条目?
哦,我觉得只是一个冒落空值是显而易见的解决方案:d!是的,用maprecude解决这个任务对我来说似乎也很奇怪......但我没有创造它......我的老师做到了。 – gaussd
确实有很多情况下,您只使用Map/Reduce的“地图”部分... –