传递论证" args"从主类到Map类

时间:2015-04-08 14:29:42

标签: hadoop mapreduce command-line-arguments

例子:jar类arg1 arg2 arg3

arg 1表示输入格式,arg 2表示输出格式,如下所示:

public static void main(String[] args)
{
FileInputFormat.addInputPath(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
.
.
.
.
}

我需要发送arg3“args [2]”来映射类.....

public class JoinMultiMap extends MapReduceBase
  implements Mapper<LongWritable, Text, Text, Text> 
{
i need arg3 her
}

1 个答案:

答案 0 :(得分:2)

您可以使用Configuration类来设置和获取自定义配置属性,如下所示:

在您的驱动程序代码中:

import org.apache.hadoop.conf.Configuration;
// other imports ignored for brevity
// ...

public class YourDriver extends Configured implements Tool {
  public int run(String[] args) {
    Configuration conf = getConf();
    conf.set("yourcustom.property", args[2]);
    // other driver code ignored
    // ...
  }

  public static void main(String[] args)  {
    int res = ToolRunner.run(new Configuration(), new YourDriver(), args);
    System.exit(res);
  }
}

从Mapper代码:

public class YourMapper extends Mapper<...> {
  private String yourArgument;

  @Override
    protected void setup(Context context) {
        Configuration c = context.getConfiguration();
        yourArgument = c.get("yourcustom.property");
    }

  @Override
  protected void map(...) {
    // use your argument
  }
}