剛接觸mapreduce的人肯定為碰到這樣的問題,就是我們在程式中如下類似的指令
System.out.println(year+" "+airTemperature);//無效,控制台沒有輸出。
但是console控制台不給我們輸出相應的結果,這對于很多通過System.out來調試的人來說,會是一個很頭疼的事情,我也對這個很頭疼。昨天在看《hadoop權威指南第二版》的第五章的時候,書中有介紹通過web界面來浏覽hadoop的作業資訊,發現在web界面中能看到許多作業的相關資訊。并且知道mapreduce的作業資訊都寫在了使用者日志中,存放在目錄hadoop_home/logs/userlogs中。其他日志存放地點參考《hadoop權威指南第二版》p152的表5-2。通過web界面很容用找到這些日志。
NewMaxTemperature.java
package hadoop.chapter2;
// cc NewMaxTemperature Application to find the maximum temperature in the weather dataset using the new context objects MapReduce API
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
// vv NewMaxTemperature
public class NewMaxTemperature {
static class NewMaxTemperatureMapper
extends Mapper<LongWritable, Text, Text, IntWritable> {
private static final int MISSING = 9999;
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String year = line.substring(15, 19);
int airTemperature;
if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs
airTemperature = Integer.parseInt(line.substring(88, 92));
} else {
airTemperature = Integer.parseInt(line.substring(87, 92));
}
String quality = line.substring(92, 93);
if (airTemperature != MISSING && quality.matches("[01459]")) {
context.write(new Text(year), new IntWritable(airTemperature));
System.out.println(year+" "+airTemperature);//無效,控制台沒有輸出。
/*
* stdout logs
* 1950 0
* 1950 22
* 1950 -11
* 1949 111
* 1949 78
* */
}
}
static class NewMaxTemperatureReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values,
Context context)
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
System.out.println(key+" "+value.get());//無效,控制台沒有輸出。
context.write(key, new IntWritable(maxValue));
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: NewMaxTemperature <input path> <output path>");
System.exit(-1);
// /home/hadoop/input/sample.txt /home/hadoop/output/tmp1
Job job = new Job();
job.setJarByClass(NewMaxTemperature.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(NewMaxTemperatureMapper.class);
job.setReducerClass(NewMaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
// ^^ NewMaxTemperature
sample.txt
0067011990999991950051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999
0043011990999991950051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+00221+99999999999
0043011990999991950051518004+68750+023550FM-12+038299999V0203201N00261220001CN9999999N9-00111+99999999999
0043012650999991949032412004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+01111+99999999999
0043012650999991949032418004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+00781+99999999999
我們在eclipse上運作該程式,不通過指令行來執行。
/home/hadoop/input/sample.txt /home/hadoop/output/tmp1
這是文章的主題,就是在console中我們沒有找到我們想要列印的輸出,那麼我們應該如何查找。
http://localhost:50030/ - Hadoop 管理介面
http://localhost:50060/ - Hadoop Task Tracker 狀态
http://localhost:50070/ - Hadoop DFS 狀态
在登入管理界面以後,我們能夠在Completed Jobs中找到我們剛才運作的作業,如下圖所示:
我們進入:job_201110230923_0002這個作業,會看到如下界面:
在這裡就可以看到map任務于reduce人物的詳情,我們首先點選map,進入如下界面:
再點選相應的task:task_201110230923_0002_m_000000,進入如下界面:
我們點選Task Logs列中的ALL,會出現如下界面:
上面的stdout logs就是我們System.out的内容。reduce任務也可以通過同樣的方法得到System.out的内容。其實web管理界面通路的内容都寫在本地,我們可以從本地的使用者日志檔案中找到。比如上述例子中的stdout logs可以在目錄:hadoop_home/logs/userlogs/attempt_201110230923_0002_m_000000_0目錄下找到。該目錄下有以下幾個檔案:log.index stderr stdout syslog。其中stderr中的内容是通過System.err.println輸出的。
文中提到很多作業job,任務task等相關的内容,具體這些關系可以參考《hadoop權威指南第二版》p147的内容。
本文轉自xwdreamer部落格園部落格,原文連結:http://www.cnblogs.com/xwdreamer/archive/2011/10/23/2296953.html,如需轉載請自行聯系原作者