在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 中看到我们写的日志消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值