mapReducer第一个例子WordCount
生活随笔
收集整理的這篇文章主要介紹了
mapReducer第一个例子WordCount
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
mapreducer第一個例子,主要是統計一個目錄下各個文件中各個單詞出現的次數。
mapper
?
package com.mapreduce.wordCount;import java.io.IOException;import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper;/** TextInputFormat中的recorder 每次讀取 一個分片中的 一行文本* 所以map 函數每次讀取一行。規定:* 輸入:key: 行偏移量 value:一行的文本* 輸出: key: 一個詞 value: 1* * map 做個映射。*/public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{Text keyOut = new Text();IntWritable valueOut = new IntWritable();protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {String line = value.toString();String[] worlds = line.split(" ");for( String w:worlds){keyOut.set(w);valueOut.set(1);context.write(keyOut,valueOut);}}}?
reudcer
?
package com.mapreduce.wordCount;import java.io.IOException;import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; /** 輸入: 對應maper 的輸出 [key: values] {"love":[1,1,1,1,1,1]}* 輸出: 詞和每個詞的出現次數。* 中間shuffle 階段自動排序分區。 因為沒有分區,所以輸出到一個文件中 // 所以結果文件是按 key 排序的。* */ public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{protected void reduce(Text key, Iterable<IntWritable> value,Context context)throws IOException, InterruptedException {int count = 0;for( IntWritable v:value){count += v.get();}context.write(key, new IntWritable(count));} }?
job 驅動
package com.mapreduce.wordCount;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public class WordCountDemo {public static void main(String[] args) throws Exception {// 1 獲取configurationConfiguration configuration = new Configuration();// 2 job Job job = Job.getInstance(configuration);// 3 作業jar包 job.setJarByClass(WordCountDemo.class);// 4 map, reduce jar 包job.setMapperClass(WordCountMapper.class);job.setReducerClass(WordReducer.class);// 5 map 輸出類型 job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 6 最終 輸出類型 (reducer) job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// 7 inputformatclass , outputformatclass 輸入輸出入文件類型 可能決定分片信息 job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);// 8 輸入輸出文件路徑 FileInputFormat.setInputPaths(job, new Path("d:/input"));FileOutputFormat.setOutputPath(job, new Path("d:/output"));// 9 job提交 job.waitForCompletion(true);}}?
轉載于:https://www.cnblogs.com/lijins/p/10092885.html
總結
以上是生活随笔為你收集整理的mapReducer第一个例子WordCount的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VGA显示正圆
- 下一篇: 牛逼,Java中表达式引擎工具就用它!建