MapReduce执行警告WARN mapreduce.JobResourceUploader

WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.

这个警告的意思是代码里用的运行方式过时了,推荐使用继承Configured实现Tool接口的方式来实现它

如下:

 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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 org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class WordCountUpMR extends Configured implements Tool {

    //Map
    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String linData = value.toString();
            //切割条件
            String[] words = linData.split(",");
            for (String word : words) {
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }

    //Reduce
    public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long count = 0;
            for (LongWritable value : values) {
                count += value.get();
            }
            context.write(key, new LongWritable(count));
        }
    }

    //Driver
    public int run(String args[]) throws Exception {
        //create job
        Job job = Job.getInstance(this.getConf(), "name01");

        //设置程序的主类
        job.setJarByClass(this.getClass());

        //设置Map和Reduce   程序代码
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //设置Map输出的key   value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置Reduce输出的key   value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //设置去哪里读取数据input
        FileInputFormat.addInputPath(job, new Path(args[0]));

        //设置最终结果写到哪里去(输出路径)output
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //提交任务commit
        boolean isSuccess = job.waitForCompletion(true);
        return (isSuccess) ? 0 : 1;
    }

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        try {
            //判断输出目录是否存在,若存在,则删除
            Path fileOutPath = new Path(args[1]);
            FileSystem fileSystem = FileSystem.get(configuration);
            if (fileSystem.exists(fileOutPath)) {
                fileSystem.delete(fileOutPath, true);
            }
            int status = ToolRunner.run(configuration, new WordCountUpMR(), args);
            System.exit(status);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

执行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值