2021-11-05

2021-11-05,第1张

2021-11-05 WordCount的实现

map端

public static class WordCountMapper extends Mapper{

        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] words = value.toString().split(" ");

            for (String word:words) {

                context.write(new Text(word),new IntWritable(1));

            }

        }

    }

reduce端

public static class WordCountReducer extends Reducer{

        protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {

            int sum = 0;

            for (IntWritable value:values) {

                sum+=value;

            }

            context.write(key,new IntWritable(sum));

        }

    }

job端

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        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);

        FileSystem fs = FileSystem.get(conf);

        Path path = new Path("D:\hadoop\mapreduce\wordcount\output");

        if (fs.exists(path)){

          fs.delete(path,true);}FileInputFormat.setInputPaths(job,new  Path("D:\hadoop\mapreduce\wordcount\input\test.txt"));

        FileOutputFormat.setOutputPath(job,path);

        boolean isDone = job.waitForCompletion(true);

        System.exit(isDone ? 0 : 1);

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5116618.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存