亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MapReduce編寫wordcount程序代碼實現

發布時間:2020-07-26 20:20:59 來源:網絡 閱讀:2610 作者:興趣e族 欄目:大數據

MapReduce經典案例代碼(wordcount)

以經典的wordcount為例,通過自定義的mapper和reducer來實現單詞計數

package com.fwmagic.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * MapReduce單詞統計
 */
public class WordCountDemo {

    /**
     * 自定義Mapper繼承:org.apache.hadoop.mapreduce.Mapper,實現map方法
     */
    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[] words = value.toString().split(" ");
            for (String word : words) {
                context.write(new Text(word), new IntWritable(1));
            }
        }
    }

    /**
     * 自定義Reducer繼承:org.apache.hadoop.mapreduce.Reducer,實現reduce方法
     */
    public static class WordCountReducer 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 writable : values) {
                count += writable.get();
            }
            context.write(key, new IntWritable(count));
        }
    }

    /**
     * job啟動類,設置參數并集群中提交job
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(WordCountDemo.class);

        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));
        FileOutputFormat.setOutputPath(job, new Path("/wordcount/output"));

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

集群中/wordcount/input目錄下數據內容

MapReduce編寫wordcount程序代碼實現

打包項目,執行job

hadoop jar fwmagic-wordcount.jar 

執行輸出結果

MapReduce編寫wordcount程序代碼實現

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

余江县| 渝北区| 盖州市| 七台河市| 社会| 江永县| 彩票| 紫金县| 康保县| 南澳县| 建平县| 织金县| 永寿县| 安图县| 惠安县| 桂东县| 昌宁县| 淳化县| 渭源县| 沾益县| 龙里县| 禄丰县| 潮安县| 浠水县| 南召县| 三明市| 灵石县| 榆林市| 老河口市| 洮南市| 星子县| 乐亭县| 工布江达县| 卢湾区| 普定县| 淮南市| 呼图壁县| 渭南市| 繁峙县| 三门峡市| 澎湖县|