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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MapReduce基础开发之十一DistributedCache使用

發布時間:2025/4/16 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MapReduce基础开发之十一DistributedCache使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、需求場景:
? ?過濾無意義的單詞后再進行文本詞頻統計。處理流程是:
1)預定義要過濾的無意義單詞保存成文件,保存到HDFS中;
2)程序中將該文件定位為作業的緩存文件,使用DistributedCache類;
3)Map中讀入緩存文件,對文件中的單詞不做詞頻統計。
該場景主要解決文件在Hadoop各task之間共享的問題,用conf傳遞參數不能傳輸大文件,于是通過DistributedCache派發文件到各節點。


2、具體代碼如下:

package com.word;import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.HashSet; import java.util.StringTokenizer;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.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.filecache.DistributedCache; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;public class FilterWordCount {public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{private final static IntWritable one = new IntWritable(1);private Text word = new Text();private HashSet<String> keyWord;private Path[] localFiles;//setup函數在Map task啟動之后立即執行public void setup(Context context) throws IOException,InterruptedException{keyWord=new HashSet<String>();Configuration conf=context.getConfiguration();localFiles=DistributedCache.getLocalCacheFiles(conf);//將緩存文件內容讀入到當前Map Task的全局變量中for(int i=0;i<localFiles.length;i++){String aKeyWord;BufferedReader br=new BufferedReader(new FileReader(localFiles[i].toString()));while((aKeyWord=br.readLine())!=null){keyWord.add(aKeyWord);}br.close();}}//根據緩存文件中緩存的無意義單詞對輸入流進行過濾public void map(Object key, Text value, Context context)throws IOException, InterruptedException {StringTokenizer itr = new StringTokenizer(value.toString());while (itr.hasMoreTokens()) {String aword=itr.nextToken();//獲取字符if(!keyWord.contains(aword)){//不包含無意義單詞word.set(aword);context.write(word, one);}}}}public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {private IntWritable result = new IntWritable();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[] args) throws Exception {Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if (otherArgs.length != 2) {System.err.println("Usage: FilterWordCount <in> <out>");System.exit(2);}//將HDFS上的文件設置成當前作業的緩存文件DistributedCache.addCacheFile(new URI("/tmp/fjs/kw.txt"), conf);Job job = new Job(conf, "FilterWordCount");job.setJarByClass(FilterWordCount.class);job.setMapperClass(TokenizerMapper.class);job.setCombinerClass(IntSumReducer.class);job.setReducerClass(IntSumReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);} } /** 統計的輸入文件:hadoop fs -put /var/log/boot.log /tmp/fjs/* 無意義單詞緩存文件:/tmp/fjs/kw.txt* 結果輸出文件:/tmp/fjs/fwcout* 執行命令:hadoop jar /mnt/FilterWordCount.jar /tmp/fjs/boot.log /tmp/fjs/fwcout*/

總結

以上是生活随笔為你收集整理的MapReduce基础开发之十一DistributedCache使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。