实例化hadoop中一个映射器的所有映射使用的对象

时间:2015-02-03 09:29:50

标签: java hadoop mapper

我想实例化一个对象以供所有地图操作使用。实例化需要一些参数(约10个左右)。我想我应该使用Mapper.setup方法做到这一点,并使用作业配置来传递参数。 我找不到合适的例子。 (请注意,我是hadoop的新手)

基本上,我要找的是:

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private static final IntWritable one = new IntWritable(1);

    private static MyParser parser;

    protected void setup(Context context) 
            throws IOException, InterruptedException{

        String param1 = "";  // how to get those?
        String param2 = "";

        parser = new MyParser(param1,param2);
    }

    protected void map(LongWritable offset, Text value, Context context) 
            throws IOException, InterruptedException {

        String key = parser.parse(value.toString());
        context.write(new Text(key),one);
    }
}

这是一种合适的方法吗?有替代方案吗?

子问题:如果参数取决于处理的文件怎么办?

1 个答案:

答案 0 :(得分:1)

在main方法中,在声明配置对象后添加这些行并设置参数

Configuration con = new Configuration();
con.set("param1", "welcome"); // for e.g
con.set("param2", "hello"); // for e.g

在Mapper设置方法中添加这些行。可以在上下文对象

的配置对象的帮助下重新获取这些参数
Configuration conf = context.getConfiguration();
 String param1 =conf.get("param1"); // welcome will be coming here
String param2 =conf.get("param2"); // hello will be coming here

如果要处理使用distriubuted cache,可以将其作为静态参数并放在文件中 -

相关问题