gpt4 book ai didi

java - 产生奇怪结果的简单字数统计 MapReduce 示例

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

我在 Hadoop Map/Reduce 作业中遇到了一个奇怪的问题。作业正确提交、运行,但产生不正确/奇怪的结果。似乎 mapper 和 reducer 根本没有运行。输入文件转换自:

12
16
132
654
132
12

0   12
4 16
8 132
13 654
18 132
23 12

我假设第一列是在映射器之前为对生成的键,但映射器和缩减器似乎都没有运行。当我使用旧 API 时,工作运行良好。

下面提供了作业的来源。我使用 Hortonworks 作为平台。

public class HadoopAnalyzer
{
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
{
JobConf conf = new JobConf(HadoopAnalyzer.class);
conf.setJobName("wordcount");
conf.set("mapred.job.tracker", "192.168.229.128:50300");
conf.set("fs.default.name", "hdfs://192.168.229.128:8020");
conf.set("fs.defaultFS", "hdfs://192.168.229.128:8020");
conf.set("hbase.master", "192.168.229.128:60000");
conf.set("hbase.zookeeper.quorum", "192.168.229.128");
conf.set("hbase.zookeeper.property.clientPort", "2181");
System.out.println("Executing job.");
Job job = new Job(conf, "job");
job.setInputFormatClass(InputFormat.class);
job.setOutputFormatClass(OutputFormat.class);
job.setJarByClass(HadoopAnalyzer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
TextInputFormat.addInputPath(job, new Path("/user/usr/in"));
TextOutputFormat.setOutputPath(job, new Path("/user/usr/out"));
job.setMapperClass(Mapper.class);
job.setReducerClass(Reducer.class);
job.waitForCompletion(true);
System.out.println("Done.");
}
}

也许我遗漏了一些明显的东西,但是任何人都可以阐明这里可能出了什么问题吗?

最佳答案

输出符合预期,因为您使用了以下内容,

job.setMapperClass(Mapper.class);
job.setReducerClass(Reducer.class);

应该是——

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

您使用 Map 和 Reduce 扩展了 Mapper 和 Reducer 类,但没有在您的工作中使用它们。

关于java - 产生奇怪结果的简单字数统计 MapReduce 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184102/

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