将参数传递给同一个映射器,使其对不同文件的行为不同

时间:2016-03-25 23:03:51

标签: java hadoop mapreduce bigdata

我正在尝试为不同配置的不同输入文件重用相同的映射器。由于我正在重用mapper,我想将一些上下文信息传递给mapper,以了解它当前正在运行的键/文件。

所有映射器遵循几乎相同的逻辑,映射器输出由规则确定。要添加要映射的新文件,我们应该只更新此配置并添加新的映射器。以下是示例规则文件:

[
{
    "key":"company1",
    "fields":["name", "age", "salary", "department", "state"],
    "mapperFilePath":"/from-dw/company1Employees.tsv",
    "rulesFilePath":"configuration/attribution/company1Rules.js"
},
{
    "key":"company2",
    "fields":["firstName", "LastName", "department", "salary", "country"],
    "mapperFilePath":"/from-dw/company2Employees.tsv",
    "rulesFilePath":"configuration/attribution/company2Rules.js"
},
]

我编写了一个通用映射器,可以读取配置并运行相应的规则文件来计算映射器输出。只有“密钥”可用时才能读取配置。

public class GenericMapper extends Mapper<K,V,KO,VO> {
    @Override
    protected void setupMap(Context context) throws IOException, InterruptedException {
        super.setupMap(context);
        // RECEIVE KEY HERE 
    }

    @Override
    public void map(LongWritable positionInFile, Text line, Context context)
    {
        // processing here depends on the key
    }
}

驱动程序类读取配置文件并根据需要创建尽可能多的映射器。我需要一种方法将“key”参数从这里传递给mapper。

public class Driver extends Configured implements Tool {

    @Override
    protected boolean setupJob(Job job) throws Exception {
        Configuration conf = job.getConfiguration();
        Map<String, Config> companyConfigMap = configsHolder.getConfigMap();
        for (Map.Entry<String, Config> eachEntry : companyConfigMap.entrySet()) {
            String key = eachEntry.getKey();
            String filePath = eachEntry.getValue().getCandidatesFilePath();
            MultipleInputs.addInputPath(job, new Path(filePath), inputFormatClass, GenericMapper.class);
            // SOMEHOW SEND KEY HERE
        }
    return true;
    }

执行此操作的原因是使此作业配置驱动。我们将允许此作业的用户在必须添加要考虑的新文件时通过UI上传非常简单的配置。每当我们有新的用例时,这将使我们无需进行代码更改和部署。

0 个答案:

没有答案