2013-07-20 54 views
2

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

+0

所以,你想一个属性文件传递给每一个映射器的? –

+0

是的。让我再解释一下。我们的映射器正在访问HBase中的数据,这意味着我们需要在映射函数中创建配置。为了测试等目的,我们不需要配置硬编码。我不确定如果这是最好的方法,如果有更好的方法来整合HBase hadoop mapreduce。代码中缺少进口 – jaksky

回答

7

如果您尚未在作业中使用此功能,则可以尝试运行Hadoop作业的GenericOptionsParser,Tool和ToolRunner。

注意: MyDriver扩展配置并实现工具。 而且,运行您工作中使用这个

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); 
    } 
} 
+0

。链接是404.配置类的包是什么? – harschware

相关问题