我正在尝试对不同配置的不同输入文件重用相同的Map器。因为我正在重用Map器,所以我想将一些上下文信息传递给Map器,告诉它当前运行的是哪个键/文件。
所有Map器遵循几乎相同的逻辑,Map器输出由规则决定。要添加一个要Map的新文件,我们应该只更新这个配置,然后添加一个新的Map器。下面是一个示例规则文件:
[
{
"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"
},
]
我编写了一个通用Map器,它可以读取配置并运行相应的规则文件来计算Map器输出。它只能在“key”可用时读取配置。
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
}
}
driver类读取配置文件并根据需要创建尽可能多的Map器。我需要一种方法将“key”参数从这里传递到Map器。
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;
}
这样做的原因是为了使此作业配置驱动。我们将允许这项工作的用户上传非常简单的配置,通过用户界面时,他们必须添加新的文件考虑到。当我们有新的用例时,这将使我们从代码更改和部署中解放出来。
暂无答案!
目前还没有任何答案,快来回答吧!