mapreduce统计输出每一个单词出现的总次数

public class WordCount {
    static {
        System.setProperty("hadoop.home.dir", "D:\\专高三\\第四天idea实现hdfs的一些命令\\winutils-master\\hadoop-2.8.3");
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //创建job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word-count");
        job.setJarByClass(WordCount.class);
        //输入文件
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //编写mapper处理
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //shuffle
        //编写reducer处理
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //输出文件
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //提交
        boolean result = job.waitForCompletion(true);
        System.out.println(result?1:0);
    }
    public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String words = value.toString();
            String[] wordArr = words.split(" ");
            for(String word:wordArr){
                context.write(new Text(word),new LongWritable(1));
            }
        }
    }
    public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            Long sum = 0L;
            for(LongWritable value : values){
                sum += value.get();
            }
            context.write(key,new LongWritable(sum));
        }
    }
}
 

 

worldcount 传参
    file:/D://words.txt
    file:/D://out

 

集群运行:

job.setJarByClass(WordCount.class);

命令:

bin/yarn jar hadoop_demo-1.0-SNAPSHOT.jar mapreduce.WordCount file:/home/test/hadoop-2.9.2/NOTICE.txt file:/home/test/out

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值