Hadoop高效执行ToolRunner

Hadoop高效执行ToolRunner

使用ToolRunner的原因

关于 MapReduce 运行和参数配置的缺点

  1. MapReduce Job 配置参数写到 java 代码里,一旦变更意味着修改 java 文件源码、编译、打包、部署一连串事情。
  2. MapReduce 依赖配置文件的时候,需要手工编写 java 代码使用 DistributedCache 将其上传到 HDFS 中,以便 mapreduce 函数可以读取。
  3. 当使用mapreduce 函数依赖第三方 jar 文件时,在命令行中使用”-libjars”参数指定依赖 jar 包时,但根本没生效。

ToolRunner可以并发执行,在终端执行时可以指定参数。

GenericOptionsParser介绍

功能

GenericOptionsParser可以将命令行中参数自动设置到变量 conf 中。不需要在代码中配置参数。

优点

  • 不需要将其硬编码到 java 代码中,很轻松就可以将参数与代码分离开。

例子

GenericOptionsParser 解析命令行参数

WordCount.java

public class WordCount {
    // 略...
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, 
                                            args).getRemainingArgs();
        // 略...
        Job job = new Job(conf, "word count");
        // 略...
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
通过命令行设置 reduce task 数量
bin/hadoop jar MyJob.jar com.xxx.MyJobDriver -Dmapred.reduce.tasks=5
常用的参数-libjars-files
bin/hadoop jar MyJob.jar com.xxx.MyJobDriver -Dmapred.reduce.tasks=5 \ 
    -files ./dict.conf  \
    -libjars lib/commons-beanutils-1.8.3.jar,lib/commons-digester-2.1.jar

参数-libjars的作用是上传本地 jar 包到 HDFSMapReduce 临时目录并将其设置到 mapreduce taskclasspath 中;参数-files的作用是上传指定文件到 HDFS 中 mapreduce 临时目录,并允许 mapreduce task 读取到它。这两个配置参数其实都是通过 DistributeCache 来实现的。

ToolRunnerGenericOptionsParser配合使用
public class WordCount extends Configured implements Tool {
    @Override
    public int run(String[] arg0) throws Exception {
        Job job = new Job(getConf(), "word count");
        // 略...
        System.exit(job.waitForCompletion(true) ? 0 : 1);
        return 0;
    }
    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new WordCount(), args);
        System.exit(res);
    }
}

使用ToolRunner后可以将 GenericOptionsParser 调用隐藏到自身 run 方法,自动执行。

ToolRunnerDerive的区别

  1. WordCount 继承 Configured 并实现 Tool 接口。
  2. 重写 Tool 接口的 run 方法,run方法不是 static 类型,性能很好。
  3. WordCount中通过 getConf() 获取 Configuration 对象。

实现思路

  • Drive类继承org.apache.hadoop.conf.Configured
  • Drive类实现org.apache.hadoop.util.Tool接口,实现run`方法
  • run方法完成Job任务配置
  • main()方法中实例化Drive类,调用run()方法

案例

词频统计

package hadoop.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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 java.io.IOException;
import java.util.Iterator;

public class WordCountDriveTool extends Configured implements Tool {

    static class WordCountMapper extends Mapper<LongWritable, Text,Text,LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 切分词汇
            String[] values = value.toString().split("\t");
            // 遍历输出
            for (String word : values) {
                context.write(new Text(word),new LongWritable(1));
            }
        }
    }

    static class WordCountReduce extends Reducer<Text,LongWritable,Text,LongWritable>{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            // 统计每个单词的数量
            Iterator<LongWritable> iterator = values.iterator();
            long count=0L;
            while (iterator.hasNext()) {
                count+=iterator.next().get();
            }
            context.write(key,new LongWritable(count));
        }
    }


    @Override
    public int run(String[] args) throws Exception {
        setConf(new Configuration());
        Job job = Job.getInstance(getConf(), this.getClass().getName());
        job.setJarByClass(WordCountDriveTool.class);
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        return job.waitForCompletion(true)?1:0;
    }

    public static void main(String[] args) throws Exception {
        args=new String[]{"D:\\BigData\\hadoop\\mr\\wordcount\\wcinput","D:\\BigData\\hadoop\\mr\\wordcount\\wcoutput"};
        WordCountDriveTool driveTool = new WordCountDriveTool();
        driveTool.run(args);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值