Hadoop系统入门之原始数据ELT操作例子

package com.imooc.bigdata.hadoop.project.mrv2;

import com.imooc.bigdata.hadoop.project.utils.GetPageId;
import com.imooc.bigdata.hadoop.project.utils.LogParser;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.util.Map;

/**

  • 原始数据ETL操作
    */
    public class ETLApp {

    // Driver端的代码:八股文
    public static void main(String[] args) throws Exception{
    Configuration configuration = new Configuration();

     // 如果输出目录已经存在,则先删除
     FileSystem fileSystem = FileSystem.get(configuration);
     Path outputPath = new Path("input/etl/");
     if(fileSystem.exists(outputPath)) {
         fileSystem.delete(outputPath,true);
     }
    
     Job job = Job.getInstance(configuration);
     job.setJarByClass(ETLApp.class);
    
     job.setMapperClass(MyMapper.class);
    
     job.setMapOutputKeyClass(NullWritable.class);
     job.setMapOutputValueClass(Text.class);
    
     FileInputFormat.setInputPaths(job, new Path("/Users/rocky/data/trackinfo_20130721.data"));
     FileOutputFormat.setOutputPath(job, new Path("input/etl/"));
    
     job.waitForCompletion(true);
    

    }

    static class MyMapper extends Mapper<LongWritable, Text, NullWritable, Text> {

     private LogParser parser;
    
     @Override
     protected void setup(Context context) throws IOException, InterruptedException {
         parser = new LogParser();
     }
    
     @Override
     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
         String log = value.toString();
         Map<String, String> logInfo = parser.parse(log);
    
         String ip = logInfo.get("ip");
         String url = logInfo.get("url");
         String sessionId = logInfo.get("sessionId");
         String time = logInfo.get("time");
         String country = logInfo.get("country") == null ? "-" : logInfo.get("country");
         String province = logInfo.get("province")== null ? "-" : logInfo.get("province");
         String city = logInfo.get("city")== null ? "-" : logInfo.get("city");
         String pageId = GetPageId.getPageId(url)== "" ? "-" : GetPageId.getPageId(url);
    
         StringBuilder builder = new StringBuilder();
         builder.append(ip).append("\t");
         builder.append(url).append("\t");
         builder.append(sessionId).append("\t");
         builder.append(time).append("\t");
         builder.append(country).append("\t");
         builder.append(province).append("\t");
         builder.append(city).append("\t");
         builder.append(pageId);
    
         if (StringUtils.isNotBlank(pageId) && !pageId.equals("-")) {
             System.out.println("------" + pageId);
         }
    
    
         context.write(NullWritable.get(), new Text(builder.toString()));
     }
    

    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值