久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

WordCount怎么實現單詞計數

161次閱讀
沒有評論

共計 4057 個字符,預計需要花費 11 分鐘才能閱讀完成。

這篇文章主要介紹“WordCount 怎么實現單詞計數”,在日常操作中,相信很多人在 WordCount 怎么實現單詞計數問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”WordCount 怎么實現單詞計數”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!

一:

public class WordCount {
 public static class TokenizerMapper extends Mapper Object, Text, Text, IntWritable {private final static IntWritable one = new IntWritable(1);
 private Text word = new Text();
 public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
 // 將每一行拆分成一個個的單詞,并肩 word,1 作為 map 方法的結果輸出。StringTokenizer itr = new StringTokenizer(value.toString());
 //  測試其是否還有更多可用的標記
 while (itr.hasMoreTokens()) {word.set(itr.nextToken());
 context.write(word, one);
 public static class IntSumReducer extends Reducer Text,IntWritable,Text,IntWritable  {private IntWritable result = new IntWritable();
  * reduce 函數的輸入也是一個 key/value 的形式,不過它的 value 是一個迭代器的形式 Iterable IntWritable  values, *  也就是說 reduce 的輸入是一個 key 對應一組的值的 value,reduce 也有 context 和 map 的 context 作用一致。 * */
 public void reduce(Text key, Iterable IntWritable  values, Context context) throws IOException, InterruptedException {
 int sum = 0;
 for (IntWritable val : values) {sum += val.get();
 result.set(sum);
 context.write(key, result);
 public static void main(String[] arg) throws Exception {
 // 初始化 Configuration,該類主要是讀取 mapreduce 系統配置信息,這些信息包括 hdfs 還有 mapreduce 等。Configuration conf = new Configuration();
 // 構建一個 job,Job job = Job.getInstance(conf, word count 
 // 裝載程序員編寫好的計算程序
 job.setJarByClass(WordCount.class);
 // 實現 map 函數,根據輸入的 key,value 對生成中間結果。配置 mapreduce 如何運行 map 和 reduce 函數
 job.setMapperClass(TokenizerMapper.class);
 //Combiner 類,實現 combine 函數,合并中間結果中具有相同 key 值的鍵值對。  默認為 null  即不合并中間結果。job.setCombinerClass(IntSumReducer.class);
 //Reducer 類   實現 reduce 函數   將中間結果合并,得到最終結果。job.setReducerClass(IntSumReducer.class);
 // 定義輸出的 key/value 的類型,也就是最終存儲在 hdfs 上結果文件的 key/value 的類型
 job.setOutputKeyClass(Text.class);
 job.setOutputValueClass(IntWritable.class);
 // 第一行就是構建輸入的數據文件,第二行是構建輸出的數據文件,FileInputFormat.addInputPath(job, new Path( hdfs://192.168.226.129:9000/rootdir/mapreduce.txt));
 FileOutputFormat.setOutputPath(job, new Path( hdfs://192.168.226.129:9000/rootdir/data/studytest/ +System.currentTimeMillis()+ / ));
 // 如果 job 運行成功了,我們的程序就會正常退出
 System.exit(job.waitForCompletion(true) ? 0 : 1);
}

二:

public class WordCount1 {public static final IntWritable ONE = new IntWritable(1);
 public static class WordCountMapper extends Mapper LongWritable, Text, Text, IntWritable {
 @Override
 protected void map(LongWritable key, Text value, Mapper LongWritable, Text, Text, IntWritable .Context context)
 throws IOException, InterruptedException {String[] vs = value.toString().split( \\s 
 for (String string : vs) {context.write(new Text(string) , ONE);

public static class WordCountReduce extends Reducer Text, IntWritable, Text, IntWritable { @Override protected void reduce(Text key, Iterable IntWritable  values, Reducer Text, IntWritable, Text, IntWritable .Context context) throws IOException, InterruptedException { int count =0; for (IntWritable v : values) {count += v.get(); context.write(key,new IntWritable(count) ); public static void main(String[] args) { try {Configuration conf = new Configuration(); Job job = Job.getInstance(conf, word count job.setJarByClass(WordCount1.class); // 實現 map 函數,根據輸入的 key,value 對生成中間結果。job.setMapperClass(WordCountMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setReducerClass(WordCountReduce.class); FileInputFormat.addInputPath(job, new Path( hdfs://192.168.226.129:9000/rootdir/mapreduce.txt)); FileOutputFormat.setOutputPath(job, new Path( hdfs://192.168.226.129:9000/rootdir/data/studytest/ +System.currentTimeMillis()+ / )); System.exit(job.waitForCompletion(true) ? 0 : 1); } catch (IllegalStateException e) {e.printStackTrace(); } catch (IllegalArgumentException e) {e.printStackTrace(); } catch (ClassNotFoundException e) {e.printStackTrace(); } catch (IOException e) {e.printStackTrace(); } catch (InterruptedException e) {e.printStackTrace(); }

  例如:

輸入:

xujun xujun xj , cxj 
cccc ddd xujun xj
yyy jjj
ccc yyy

輸出:

, 1
ccc 1
cccc 1
cxj 1
ddd 1
jjj 1
xujun 3
xxxx 1
yyy 2

到此,關于“WordCount 怎么實現單詞計數”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計4057字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 安龙县| 徐水县| 林周县| 化德县| 唐海县| 元谋县| 浮梁县| 石河子市| 荃湾区| 江山市| 黄石市| 松江区| 沙洋县| 湄潭县| 马龙县| 太康县| 临颍县| 宿州市| 五大连池市| 禹州市| 汝阳县| 横山县| 寿光市| 中阳县| 泽州县| 嘉鱼县| 旺苍县| 友谊县| 万安县| 黔南| 仁布县| 华亭县| 崇礼县| 邳州市| 德兴市| 安福县| 桓仁| 麦盖提县| 仁化县| 英山县| 翼城县|