如何在Hadoop MapReduce中查看日志和Sysout
在编写程序时,为了进行调试,我们确实放置了一些日志或者system.out来显示消息。我们还可以在MapReduce程序中使用记录器或者sysouts进行调试。在本文中,我们将介绍如何在Hadoop MR2中访问这些日志或者system.out.print消息。
如何在MapReduce2中查看日志消息
当然,第一件事就是将日志放入代码中。然后,在运行MapReduce作业时,我们可以从控制台记录该作业的application_id。运行MapReduce作业后,将在控制台上显示如下一行,显示应用程序ID。
18/06/13 15:20:59 INFO impl.YarnClientImpl: Submitted application application_1528883210739_0001
使用相同的application_id,将在HADOOP_INSTALLATION_DIR / logs / userlogs /位置创建一个文件夹,我们将在其中找到包含映射器和化简器日志的文件夹。在这些文件夹中,我们可以检查stdout文件中是否有system.out.print和syslog日志消息。
示例MapReduce显示如何放置日志
我们可以使用Hadoop捆绑包随附的Apache Commons日志记录来进行记录。这是一个简单的单词计数MapReduce程序,其中包含一些log.info和sysout消息。
import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount{ public static final Log log = LogFactory.getLog(WordCount.class); // Map function public static class WordMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { log.info("in map method"); // Splitting the line on spaces String[] stringArr = value.toString().split("\s+"); for (String str : stringArr) { word.set(str); System.out.println("word -- " + word.toString()); context.write(word, one); } } } // Reduce function public static class CountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { log.info("in reducer "); int sum = 0; for (IntWritable val : values) { sum += val.get(); } log.info(key + " -- Sum is --- " + sum); result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordMapper.class); //job.setNumReduceTasks(0); job.setReducerClass(CountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
运行它并知道应用程序ID后,只需转到该位置并检查stdout和syslog文件。
作为示例,运行此代码后,我可以在路径-HADOOP_INSTALLATION_DIR / logs / userlogs / application_1528883210739_0001 / container_1528883210739_0001_01_000002 / stdout中访问标准输出,并在其中查看我的sysouts-
word -- This word -- is word -- a word -- test word -- file. word -- This word -- is word -- a word -- Hadoop word -- MapReduce word -- program word – file.
或者,我可以在路径HADOOP_INSTALLATION_DIR / logs / userlogs / application_1528883210739_0001 / container_1528883210739_0001_01_000003 / syslog中访问syslog,并查看减速器的记录器。
2018-06-13 15:21:15,321 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,321 INFO [main] org.theitroad.WordCount$WordMapper: Hadoop -- Sum is --- 1 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: MapReduce -- Sum is --- 1 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: This -- Sum is --- 2 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: a -- Sum is --- 2 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: file. -- Sum is --- 2 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: is -- Sum is --- 2 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,322 INFO [main] org.theitroad.WordCount$WordMapper: program -- Sum is --- 1 2018-06-13 15:21:15,323 INFO [main] org.theitroad.WordCount$WordMapper: in reducer 2018-06-13 15:21:15,323 INFO [main] org.theitroad.WordCount$WordMapper: test -- Sum is --- 1