gpt4 book ai didi

java - Hadoop : Reducer class not called even with Overrides

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

我在 hadoop 中尝试了 mapreduce wordcount 代码,但是 reducer 类从未被调用,程序在运行 mapper 类后终止。

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
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());
context.write(word, one);
}
}
}

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {


@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();

Job job = new Job(conf, "wordcount");

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.waitForCompletion(true);
}

}

我什至根据需要覆盖了类。

集成开发环境:Eclipse Luna
Hadoop:版本 2.5

最佳答案

Job 对象构成了作业的规范,并使您可以控制作业的运行方式。当我们在 Hadoop 集群上运行此作业时,我们会将代码打包到一个 JAR 文件中(Hadoop 将在集群中分发该文件)。

我们可以在 Job 的 setJarByClass() 方法中传递一个类,而不是显式指定 JAR 文件的名称,Hadoop 将使用该类通过查找包含该类的 JAR 文件来定位相关的 JAR 文件。

我没有在 main 方法中看到语句。因此,包含它然后编译并运行代码。

job.setJarByClass(WordCount.class);

关于java - Hadoop : Reducer class not called even with Overrides,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678084/

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