【问题标题】:How to pass system property to map function in hadoop如何将系统属性传递给hadoop中的映射函数
【发布时间】:2013-07-19 13:12:00
【问题描述】:

有没有办法将系统参数(类似于 -Dmy_param=XXX)传递给 hadoop map reduce 框架中的 map 函数。 通过 .setJarByClass() 向 hadoop 集群提交作业。 在映射器中,我必须创建配置,所以我想让它可配置,所以我认为通过属性文件的标准方式就可以了。只是在设置属性的位置传递参数。另一种方法是将属性文件添加到提交的 jar 中。有人有经验如何解决这个问题吗?

【问题讨论】:

  • 那么,您想将一个属性文件传递给每个映射器吗?
  • 是的。让我再解释一下。我们的映射器正在访问 HBase 中的数据,这意味着我们需要在映射函数中创建配置。出于测试等目的,我们没有硬编码的配置。如果有更好的集成 HBase hadoop mapreduce 的方法,我不确定这是否是最好的方法。

标签: hadoop configuration mapreduce hbase


【解决方案1】:

如果您尚未在作业中使用它,可以尝试使用 GenericOptionsParser、Tool 和 ToolRunner 来运行 Hadoop 作业。

注意: MyDriver 扩展 Configured 并实现 Tool。 而且,要运行你的工作,请使用这个

hadoop -jar somename.jar MyDriver -D your.property=value arg1 arg2

欲了解更多信息,check this link

这是我为您准备的一些示例代码:

public class MyDriver extends Configured implements Tool {

  public static class MyDriverMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable> {

    protected void map(LongWritable key, Text value, Context context)
      throws IOException, InterruptedException {
      // In the mapper you can retrieve any configuration you've set
      // while starting the job from the terminal as shown below

      Configuration conf = context.getConfiguration();
      String yourPropertyValue = conf.get("your.property");
    }
  }

  public static class MyDriverReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable> {

    protected void reduce(LongWritable key, Iterable<NullWritable> values, Context context) 
      throws IOException, InterruptedException {
      // --- some code ---
    }
  }

  public static void main(String[] args) throws Exception {
    int exitCode = ToolRunner.run(new MyDriver(), args);
    System.exit(exitCode);
  }

  @Override
  public int run(String[] args) throws Exception {
    Configuration conf = getConf();
    // if you want you can get/set to conf here too.
    // your.property can also be file location and after
    // you retrieve the properties and set them one by one to conf object.

    // --other code--//
    Job job = new Job(conf, "My Sample Job");
    // --- other code ---//
    return (job.waitForCompletion(true) ? 0 : 1);
  }
}

【讨论】:

  • 代码中缺少导入。链接是 404。Configuration 类的包是什么?
猜你喜欢
  • 2017-03-02
  • 2015-10-08
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 2015-03-18
相关资源
最近更新 更多