資料清洗
》》1:什么是ETL
ETL,是英文Extract-Transform-Load的縮寫,用來描述將資料從來源端經過抽取(extract)、轉換(transform)、加載(load)至目的端的程序
ETL是將業務系統的資料經過抽取、清洗轉換之后加載到資料倉庫的程序,目的是將企業中的分散、零亂、標準不統一的資料整合到一起,為企業的決策提供分析依據
在運行核心業務MapReduce程式之前,往往要先對資料進行清洗,清理掉不符合用戶要求的資料,
清理的程序往往只需要運行Mapper程式,不需要運行Reduce程式,
資料清洗案例實操-簡單決議版
需求分析
去除日志中欄位長度小于等于11的日志,
(1)輸入資料
194.237.142.21 - - [18/Sep/2013:06:49:18 +0000] "GET /wp-content/uploads/2013/07/rstudio-git3.png HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible;)"
183.49.46.228 -
163.177.71.12 - - [18/Sep/2013:06:49:33 +0000] "HEAD / HTTP/1.1" 200 20 "-" "DNSPod-Monitor/1.0"
(2)期望輸出資料
每行欄位長度都大于11,
需要在Map階段對輸入的資料根據規則進行過濾清洗,
實作代碼
(1)撰寫LogMapper類
package com.dev1.weblog;
//ETL 抽取 清洗 轉換 加載
//清洗 去除掉長度<=11的log
//int countTrue = 0;
//int countFalse = 0;
public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//讀取行
String line = value.toString();
//呼叫一個判斷長度的方法
boolean result = parseLog(line);
if(result){
//寫到磁盤
Text text = new Text();
text.set(line);
context.write(text,NullWritable.get());
context.getCounter("etl","countTrue").increment(1);
}else{
//什么也不處理
context.getCounter("etl","countFalse").increment(1);
}
}
private boolean parseLog(String line) {
//判斷當前log的長度是>=11的嗎? ["a","BCD"]
//當前的line 通過 split切割,得到陣列的元素>11
String[] arr = line.split(" ");
if( arr . length >11){
return true;
}else{
return false;
}
}
}
(2)撰寫LogDriver類
package com.dev1.weblog;
public class LogDriver {
public static void main(String[] args) throws Exception {
// 輸入輸出路徑需要根據自己電腦上實際的輸入輸出路徑設定
args = new String[] { "e:/input/inputlog", "e:/output1" };
// 1 獲取job資訊
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2 加載jar包
job.setJarByClass(LogDriver.class);
// 3 關聯map
job.setMapperClass(LogMapper.class);
// 4 設定最終輸出型別
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
// 設定reducetask個數為0
job.setNumReduceTasks(0);
// 5 設定輸入和輸出路徑
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 6 提交
job.waitForCompletion(true);
}
}
計數器代碼最后加
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276734.html
標籤:其他
