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

Java怎么編寫Mapreduce程序

共計(jì) 1494 個(gè)字符,預(yù)計(jì)需要花費(fèi) 4 分鐘才能閱讀完成。

編寫 MapReduce 程序的基本步驟如下:

  1. 創(chuàng)建一個(gè)實(shí)現(xiàn)了 Mapper 接口的類,重寫 map 方法。map 方法接收一個(gè)鍵值對(duì)作為輸入,將輸入數(shù)據(jù)處理并輸出為中間鍵值對(duì)。
public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {word.set(tokenizer.nextToken());
            context.write(word, one);
        }
    }
}
  1. 創(chuàng)建一個(gè)實(shí)現(xiàn)了 Reducer 接口的類,重寫 reduce 方法。reduce 方法接收中間鍵值對(duì)作為輸入,將輸入數(shù)據(jù)根據(jù)鍵匯總并輸出為最終結(jié)果鍵值對(duì)。
public class MyReducer 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);
    }
}
  1. 創(chuàng)建一個(gè)配置對(duì)象,設(shè)置 MapReduce 作業(yè)的相關(guān)參數(shù)。
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
  1. 指定輸入數(shù)據(jù)的路徑和輸出結(jié)果的路徑。
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
  1. 設(shè)置 Mapper 和 Reducer 的類。
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
  1. 設(shè)置最終結(jié)果的鍵值對(duì)類型。
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
  1. 提交 MapReduce 作業(yè)。
System.exit(job.waitForCompletion(true) ? 0 : 1);

以上就是編寫 MapReduce 程序的基本步驟。根據(jù)具體需求,可以對(duì) Mapper 和 Reducer 的邏輯進(jìn)行擴(kuò)展和修改。

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2024-01-29發(fā)表,共計(jì)1494字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 凤冈县| 博白县| 仁布县| 双牌县| 林州市| 兴城市| 高碑店市| 北流市| 扶绥县| 白水县| 兴宁市| 郑州市| 寿阳县| 康乐县| 余干县| 延长县| 海伦市| 金乡县| 伊吾县| 正镶白旗| 简阳市| 延庆县| 绥中县| 鹤山市| 台北县| 湖州市| 广东省| 德昌县| 恩平市| 遵义县| 垦利县| 米林县| 三原县| 眉山市| 花垣县| 连南| 凤凰县| 陈巴尔虎旗| 界首市| 化德县| 恩平市|