mapreduce日志信息数据处理

mapreduce日志信息数据处理

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class WebLog {
    //Mapper类
    public class WebLog_Mapper extends Mapper<LongWritable, Text,Text, NullWritable>{
        Text k = new Text();
        //实现map方法
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //读取文件中的数据
            String line = value.toString();
            //这里是获得了一行数据,但是需要把数据转换成我们想要的格式
            //为了方便,我们这里通过一个方法进行转换
            WebLogBean info = WebLog_parse(line);
            //判断,如果不满足条件直接返回
            if (StringUtils.isNotEmpty(info.toString()) || !info.isVolid()) return;
            k.set(info.toString());
            context.write(k,NullWritable.get());
        }
    }

    //转换类,把原始的日志数据转换成我们想要的格式
    public static WebLogBean WebLog_parse(String log){
        WebLogBean webLogBean = new WebLogBean();
        //对获取的一行数据进行切割
        String[] words = log.split(" ");
        //进行判断日志数据是否合法
        if (words.length == 10){
            //虽然可以获得我们想要的数据,但是在获取数据之前我们需要对时间日期进行转换
            String date = (words[3]+""+words[4].replace("[","").replace("]",""));
            //此时已经截取到我们想要的时间格式
            //此时说明数据是合法,对原始日志数据获取到我们想要的数据内容,但是我们需要对日期格式进行转换
            date = WebLog.parseDate(date);
            webLogBean.set(words[0],words[1],date,words[6],words[8],words[9]);
        }
        //在解析完之后,我们需要对请求的状态码进行判断
        if (webLogBean.getStatus().charAt(0) == '4' || webLogBean.getStatus().charAt(0) == '5'){
            webLogBean.setVolid(false);
        }else {
            webLogBean.setVolid(true);
        }
        return webLogBean;
    }
    //对日期格式进行转化
    public static String parseDate(String date){
        //在日期进行转化时,我们需要注意的是在英文中月是用MMM表示的,而中文使用MM表示的,日志一般都是英文的
        //而Locale是表示地区的,在这里是US
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US);
        try {
            Date parse = fmt.parse(date);
            //对原始的时间解析之后,我们需要再次进行解析,转换成可视化的时间
            fmt = new SimpleDateFormat("yyyy-MM-dd",Locale.CANADA);
            //这次对我们已经解析完的时间进行二次解析
            String format = fmt.format(parse);
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    //由于有多个属性,我们把这些属性进行封装
    //定义一个javaBean,用来对日志文件中的一行数据封装
    //这里面我们不需要实现序列化,因为我们没有使用输入和输出
    public static class WebLogBean {
        private String remote_ip;//记录客户端的ip地址
        private String remote_user;//记录客户端的用户名称 , 忽略使用—
        private String time_zone;//访问时间与时区
        private String request_url;//请求url
        private String status;//请求的状态码
        private String http_body_sent;//请求体的大小
        //判断数据是否合法
        private boolean volid = true;

        public void set(String remote_ip, String remote_user, String time_zone, String request_url, String status, String http_body_sent) {
            this.remote_ip = remote_ip;
            this.remote_user = remote_user;
            this.time_zone = time_zone;
            this.request_url = request_url;
            this.status = status;
            this.http_body_sent = http_body_sent;
        }

        @Override
        public String toString() {
            return remote_ip + ' ' +
                    " " + remote_user + ' ' +
                    " " + time_zone + ' ' +
                    " " + request_url + ' ' +
                    " " + status + ' ' +
                    " " + http_body_sent;

        }

        //get、set的方法
        public String getRemote_ip() {
            return remote_ip;
        }

        public void setRemote_ip(String remote_ip) {
            this.remote_ip = remote_ip;
        }

        public String getRemote_user() {
            return remote_user;
        }

        public void setRemote_user(String remote_user) {
            this.remote_user = remote_user;
        }

        public String getTime_zone() {
            return time_zone;
        }

        public void setTime_zone(String time_zone) {
            this.time_zone = time_zone;
        }

        public String getRequest_url() {
            return request_url;
        }

        public void setRequest_url(String request_url) {
            this.request_url = request_url;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getHttp_body_sent() {
            return http_body_sent;
        }

        public void setHttp_body_sent(String http_body_sent) {
            this.http_body_sent = http_body_sent;
        }

        public boolean isVolid() {
            return volid;
        }

        public void setVolid(boolean volid) {
            this.volid = volid;
        }
    }
    //测试类

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
            Configuration con = new Configuration();
            con.set("mapreduce.framework.name", "yarn");
            con.set("yarn.resourcemanager.hostname", "hadoop");
            Job job = Job.getInstance(con);
            job.setMapperClass(WebLog_Mapper.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(NullWritable.class);

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

            job.setJarByClass(WebLog.class);
            boolean b = job.waitForCompletion(true);
            System.out.println(b ? "submit ok" : "submit error");

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值