gpt4 book ai didi

java - Hadoop 中 Mapreduce 程序的意外输出

转载 作者:可可西里 更新时间:2023-11-01 14:53:41 25 4
gpt4 key购买 nike

我正在研究 Hadoop。我的输出是预期的两倍
我无法理解为什么会这样。
请帮助我

下面是映射器类:

 import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;

public class StringMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable>
{
//hadoop supported data types
private static IntWritable send;
private Text word;

//map method that performs the tokenizer job and framing the initial key value pairs
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
{
String line = value.toString();
String out="";
int count=0;
out+=Integer.toString(count);
send = new IntWritable(1);
word = new Text(out);
output.collect(word, send);
}
}

下面是reducer类

import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;

public class StringReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>
{
//reduce method accepts the Key Value pairs from mappers, do the aggregation based on keys and produce the final output
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
{
int sum=0;
while(values.hasNext()){
sum=sum+values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

示例输入:
dashjdasdhashjfsda
dashjdasdhashjfsda
dashjdasdhashjfsda
dashjdasdhashjfsda
dashjdasdhashjfsda

示例输出
0 10

这里输出应该是0 5而不是0 10,因为我的输入只有五行。

最佳答案

您的程序似乎没问题。我复制了你的代码并在我的机器上运行它。它给出正确的输出,即 0 5

如果您使用的是 eclipse,请创建一个新配置并更改您的输入目录。
那么它可能会起作用。

关于java - Hadoop 中 Mapreduce 程序的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923716/

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