最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Hadoop計(jì)數(shù)器的應(yīng)用以及數(shù)據(jù)清洗

 更新時間:2019年01月10日 09:11:27   作者:qq_43193797  
今天小編就為大家分享一篇關(guān)于Hadoop計(jì)數(shù)器的應(yīng)用以及數(shù)據(jù)清洗,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

數(shù)據(jù)清洗(ETL)

在運(yùn)行核心業(yè)務(wù)MapReduce程序之前,往往要先對數(shù)據(jù)進(jìn)行清洗,清理掉不符合用戶要求的數(shù)據(jù)。清理的過程往往只需要運(yùn)行Mapper程序,不需要運(yùn)行Reduce程序。

1.需求

去除日志中字段長度小于等于11的日志。

(1)輸入數(shù)據(jù)

web.log

(2)期望輸出數(shù)據(jù)

每行字段長度都大于11

2.需求分析

需要在Map階段對輸入的數(shù)據(jù)根據(jù)規(guī)則進(jìn)行過濾清洗。

3.實(shí)現(xiàn)代碼

(1)編寫LogMapper類

package com.atguigu.mapreduce.weblog;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
  Text k = new Text();
  @Override
  protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
   // 1 獲取1行數(shù)據(jù)
   String line = value.toString();
   // 2 解析日志
   boolean result = parseLog(line,context);
   // 3 日志不合法退出
   if (!result) {
     return;
   }
   // 4 設(shè)置key
   k.set(line);
   // 5 寫出數(shù)據(jù)
   context.write(k, NullWritable.get());
  }
  // 2 解析日志
  private boolean parseLog(String line, Context context) {
   // 1 截取
   String[] fields = line.split(" ");
   // 2 日志長度大于11的為合法
    if (fields.length > 11) {
     // 系統(tǒng)計(jì)數(shù)器
     context.getCounter("map", "true").increment(1);
     return true;
   }else {
     context.getCounter("map", "false").increment(1);
     return false;
   }
  }
}

(2)編寫LogDriver類

package com.atguigu.mapreduce.weblog;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class LogDriver {
  public static void main(String[] args) throws Exception {
// 輸入輸出路徑需要根據(jù)自己電腦上實(shí)際的輸入輸出路徑設(shè)置
    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 關(guān)聯(lián)map
   job.setMapperClass(LogMapper.class);
   // 4 設(shè)置最終輸出類型
   job.setOutputKeyClass(Text.class);
   job.setOutputValueClass(NullWritable.class);
   // 設(shè)置reducetask個數(shù)為0
   job.setNumReduceTasks(0);
   // 5 設(shè)置輸入和輸出路徑
   FileInputFormat.setInputPaths(job, new Path(args[0]));
   FileOutputFormat.setOutputPath(job, new Path(args[1]));
   // 6 提交
   job.waitForCompletion(true);
  }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論

腾冲县| 太原市| 定边县| 兴仁县| 新河县| 盐城市| 甘谷县| 视频| 定安县| 梧州市| 新疆| 哈尔滨市| 鹰潭市| 酉阳| 加查县| 甘谷县| 尚志市| 邢台县| 内丘县| 迁西县| 屯留县| 鄂伦春自治旗| 永城市| 巫山县| 平昌县| 岢岚县| 翼城县| 晋城| 海门市| 安溪县| 封丘县| 桐乡市| 元氏县| 榆中县| 桐梓县| 青州市| 稷山县| 资阳市| 定兴县| 南平市| 扶风县|