java—示例化hadoop中一个Map器的所有Map所使用的对象

zf9nrax1  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(315)

我想示例化一个对象,供所有Map操作使用。示例化需要几个参数集(~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);
    }
}

这是一个合适的方法吗?有别的选择吗?
子问题:如果参数依赖于所处理的文件怎么办?

ioekq8ef

ioekq8ef1#

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

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

在Map器设置方法中添加这些行。这些参数可以在上下文对象的configuation对象的帮助下检索

Configuration conf = context.getConfiguration();
 String param1 =conf.get("param1"); // welcome will be coming here
String param2 =conf.get("param2"); // hello will be coming here

如果要使用分布式缓存进行处理,可以将其设置为静态参数并保存在文件中-

相关问题