繁体   English   中英

将不同的参数传递给每个映射器

[英]Passing different parameters to each mapper

我的工作使用多个映射器和一个减速器。 映射器几乎相同,除了它们用于产生结果的String的值不同。

目前我有几个类,一个用于我提到的String每个值 - 感觉应该有更好的方法,这不需要那么多的代码重复。 有没有办法将这些String值作为参数传递给映射器?

我的工作看起来像这样:

Input File A  ---->  Mapper A using
                       String "Foo"  ----+
                                         |--->  Reducer
                     Mapper B using  ----+
Input File B  ---->    String "Bar" 

我想把它变成这样的东西:

Input File A  ---->  GenericMapper parameterized
                               with String "Foo" ----+
                                                     |--->  Reducer
                     GenericMapper parameterized ----+ 
Input File B  ---->            with String "Bar"

编辑:这是我目前拥有的两个简化的映射器类。 它们准确地代表了我的实际情况

class MapperA extends Mapper<Text, Text, Text, Text> {
    public void map(Text key, Text value, Context context) {
        context.write(key, new Text(value.toString() + "Foo"));
    }
}

class MapperB extends Mapper<Text, Text, Text, Text> {
    public void map(Text key, Text value, Context context) {
        context.write(key, new Text(value.toString() + "Bar"));
    }
}

编辑:每个映射器应使用的字符串仅取决于数据来自哪个文件。 除文件名外,无法区分文件。

假设您使用文件输入格式,您可以在映射器中获取当前输入文件名,如下所示:

if (context.getInputSplit() instanceof FileSplit) {
    FileSplit fileSplit = (FileSplit) context.getInputSplit();
    Path inputPath = fileSplit.getPath();
    String fileId = ... //parse inputPath into a file id
    ...
}

您可以根据需要解析inputPath,例如仅使用文件名或仅使用分区ID等来生成标识输入文件的唯一ID。 例如:

/some/path/A -> A
/some/path/B -> B

为驱动程序中的每个可能文件“id”配置属性:

conf.set("my.property.A", "foo");
conf.set("my.property.B", "bar"); 

在映射器计算文件“id”中如上所述并获取值:

conf.get("my.property." + fileId);

也许你会在mapper中使用if语句在字符串之间进行选择。 什么取决于使用一个或另一个字符串?

或者也许使用Abstract Mapper类。

也许是这样的?

abstract class AbstractMapper extends Mapper<Text, Text, Text, Text> {
    protected String text;
    public void map(Text key, Text value, Context context) {
        context.write(key, new Text(value.toString() + text));
    }
}
class MapperImpl1 extends AbstractMapper{
    @Override
    public void map(Text key, Text value, Context context) {
        text = "foo";
        super.map();
    }
}
class MapperImpl2 extends AbstractMapper{
        @Override
        public void map(Text key, Text value, Context context) {
            text = "bar";
            super.map();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM