将参数“args”从主类传递到Map类

hts6caw3  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(343)

示例: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]”到map类。。。。。

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

ruoxqz4g1#

您可以使用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);
  }
}

从Map器代码:

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
  }
}

相关问题