您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Hadoop計(jì)數(shù)器怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Hadoop計(jì)數(shù)器怎么用”這篇文章吧。
數(shù)據(jù)清洗(ETL)
在運(yùn)行核心業(yè)務(wù)MapReduce程序之前,往往要先對(duì)數(shù)據(jù)進(jìn)行清洗,清理掉不符合用戶要求的數(shù)據(jù)。清理的過(guò)程往往只需要運(yùn)行Mapper程序,不需要運(yùn)行Reduce程序。
1.需求
去除日志中字段長(zhǎng)度小于等于11的日志。
(1)輸入數(shù)據(jù)
web.log
(2)期望輸出數(shù)據(jù)
每行字段長(zhǎng)度都大于11
2.需求分析
需要在Map階段對(duì)輸入的數(shù)據(jù)根據(jù)規(guī)則進(jìn)行過(guò)濾清洗。
3.實(shí)現(xiàn)代碼
(1)編寫(xiě)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 寫(xiě)出數(shù)據(jù) context.write(k, NullWritable.get()); } // 2 解析日志 private boolean parseLog(String line, Context context) { // 1 截取 String[] fields = line.split(" "); // 2 日志長(zhǎng)度大于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)編寫(xiě)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個(gè)數(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); } }
以上是“Hadoop計(jì)數(shù)器怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。