在hadoop MapReduce 中寫日誌消息

在hadoop集羣中,在自帶的web界面中,可以顯示在代碼中寫入的一些日誌消息,下面進行簡單的記錄:

程序:

package canma.dmml.MRJobWithLog;



import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * Created by macan on 2017/1/25.
 */
public class WordCountWithLogging extends Configured implements Tool{

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
        public Text data = new Text();
        public IntWritable num = new IntWritable(1);

        //logger setting
        public static Logger logger = Logger.getLogger(WordCountMapper.class);

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] words = value.toString().split(" ");

            for (String word : words){
                //普通日誌,使用info()
                logger.info("Mapper key  :  " + key);
                if (logger.isDebugEnabled()){
                    logger.info("Mapper value  " + value);
                }
                data.set(word);
                context.write(data, num);
            }

        }
    }

    public static class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

        public IntWritable num = new IntWritable();

        //logger setting
        public static Logger logger = Logger.getLogger(WordcountReducer.class);
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values){
                sum += val.get();
            }
            logger.info("Recuder key  :  " + key);
            if (logger.isDebugEnabled()){
                logger.info("Reducer value  :  " + sum);
            }
            num.set(sum);
            context.write(key, num);
        }
    }


    @Override
    public int run(String[] strings) throws Exception {
        Job job = Job.getInstance(getConf());

        job.setJarByClass(WordCountWithLogging.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordcountReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        String[] args = new GenericOptionsParser(getConf(), strings).getRemainingArgs();

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        ToolRunner.run(new WordCountWithLogging(), args);
    }
}

其中在WordcountMapper 和WordcountReducer這兩個類中的Logger對象進行日誌的管理,
寫日誌的方法如下:

logger.info(string)

在集羣中運行程序,可以在jobHistory service 中看到我們寫的日誌消息。

發佈了57 篇原創文章 · 獲贊 108 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章