日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

mapReducer第一个例子WordCount

發(fā)布時(shí)間:2025/3/16 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mapReducer第一个例子WordCount 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

mapreducer第一個(gè)例子,主要是統(tǒng)計(jì)一個(gè)目錄下各個(gè)文件中各個(gè)單詞出現(xiàn)的次數(shù)。

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 每次讀取 一個(gè)分片中的 一行文本* 所以map 函數(shù)每次讀取一行。規(guī)定:* 輸入:key: 行偏移量 value:一行的文本* 輸出: key: 一個(gè)詞 value: 1* * map 做個(gè)映射。*/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; /** 輸入: 對(duì)應(yīng)maper 的輸出 [key: values] {"love":[1,1,1,1,1,1]}* 輸出: 詞和每個(gè)詞的出現(xiàn)次數(shù)。* 中間shuffle 階段自動(dòng)排序分區(qū)。 因?yàn)闆](méi)有分區(qū),所以輸出到一個(gè)文件中 // 所以結(jié)果文件是按 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 驅(qū)動(dòng)

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 作業(yè)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);}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/lijins/p/10092885.html

總結(jié)

以上是生活随笔為你收集整理的mapReducer第一个例子WordCount的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。