Files and Directories Used in this Exercise
In this Hands-On Exercise, you will practice using log4j with MapReduce.
Modify the Average Word Length program you built in the Using ToolRunner and Passing Parameters exercise so that the Mapper logs a debug message indicating whether it is comparing with or without case sensitivity.
Lab Experiment
Enable Mapper Logging for the Job
1. Before adding additional logging messages, try re-running the toolrunner exercise solution with Mapper debug logging enabled by adding -Dmapred.map.child.log.level=DEBUG to the command line. E.g.
2. Take note of the Job ID in the terminal window by using mapred job command:
3. When the job is complete, view the logs. In a browser on your VM, visit the Job Tracker UI: http://localhost:50030/jobtracker.jsp. Find the job you just ran in the Completed Jobs list and click its Job ID. E.g.:
4. In the task summary, click map to view the map tasks.
5. In the list of tasks, click on the map task to view the details of that task:
6. Under Task Logs, click "All". The logs should include both INFO and DEBUG message.
Add Debug Logging Output to the Mapper
7. Copy the code from toolrunner project to the logging project stubs package.
8. Using log4j to output a debug log message indicating whether the Mapper is doing case sensitive or insensitive mapping:
- package solution;
- 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;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.log4j.Logger;
- public class LetterMapper extends Mapper
{ - boolean caseSensitive = false;
- /*
- * Set up a logger for this class.
- */
- private static final Logger LOGGER = Logger.getLogger(LetterMapper.class);
- @Override
- public void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- String line = value.toString();
- for (String word : line.split("\\W+")) {
- if (word.length() > 0) {
- /*
- * Obtain the first letter of the word
- */
- String letter;
- if (caseSensitive)
- letter = word.substring(0, 1);
- else
- letter = word.substring(0, 1).toLowerCase();
- context.write(new Text(letter), new IntWritable(word.length()));
- }
- }
- }
- @Override
- public void setup(Context context) {
- Configuration conf = context.getConfiguration();
- caseSensitive = conf.getBoolean("caseSensitive", false);
- /*
- * If Debug logging is enabled, log a message indicating
- * the value of caseSensitive.
- */
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Case sensitive: " + caseSensitive);
- }
- }
- }
9. Following the eariler steps, test your code with Mapper debug logging enabled. View the map task logs in the Job Tracker UI to confirm that your message is included in the log. (Hint: search for LetterMapper in the page to find your message.)
10. Optional: Try running map logging set to INFO (the default) or WARN instead of DEBUG and compare the log output.
沒有留言:
張貼留言