我创建了一个 mapreduce 程序来获取世界指标数据来显示我想要分析的特定指标的结果。 (即二氧化碳排放量)。数据排成一长行,包括国家、代码、指标、第 1 年排放量、第 2 年排放量等。在我的映射器中,我试图只保留我想要的数据(首先只保留线,如果它具有特定指标),然后保留国家和所有排放水平(在字符串数组中)。
我的整个程序运行,但我注意到它正在接收 Map 输入记录,但没有 Map 输出记录或 Reduce Input/Output 记录。
我一直试图找出我的逻辑哪里出了问题,但我很困惑。感谢任何输入。
我的代码如下:
---映射器--
package org.myorg;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class CO2Mapper extends Mapper <LongWritable, Text, Text, IntWritable>
{
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
String delims = ",";
String splitString = value.toString();
String[] tokens = splitString.split(delims);
int tokenCount = tokens.length;
String country = tokens[1];
String indicator = tokens[3];
int levels;
if(indicator.equals("EN.ATM.CO2E.KT"))
{
for (int j = 4; j < tokenCount; j++)
{
levels = Integer.parseInt(tokens[j]);
context.write(new Text(country), new IntWritable(levels));
}
}
}
}
---- reducer ---
package org.myorg;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class CO2Reducer extends Reducer<Text, IntWritable, Text, IntWritable>
{
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
{
int maxValue = Integer.MIN_VALUE;
int minValue = Integer.MAX_VALUE;
for(IntWritable val : values)
{
maxValue = Math.max(maxValue, val.get());
minValue = Math.min(minValue, val.get());
}
context.write(key, new IntWritable(maxValue));
context.write(key, new IntWritable(minValue));
}
}
---主要---
package org.myorg;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.conf.Configuration;
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;
//import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
public class CO2Levels
{
public static void main(String[] args) throws Exception
{
//with mapreduce
Configuration conf = new Configuration();
Job job = new Job(conf, "co2Levels");
//Job job = new Job();
job.setJarByClass(CO2Levels.class);
//job.setJobName("co2Levels");
job.setMapperClass(CO2Mapper.class);
job.setReducerClass(CO2Reducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setInputFormatClass(TextInputFormat.class);
//job.setInputFormatClass(KeyValueTextInputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
最佳答案
从示例输入中我发现 token 的格式为 6.16E+03 抛出异常,无法解析为整数。
此外,如果您想检查 system.out.println() 的去向,check this
关于java - MapReduce 程序产生空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26110333/