您好我目前面临Java Generics的问题: 我需要使用的方法有这个签名
static<K1,V1,K2,V2> void addMapper(JobConf job,
Class<? extends Mapper<K1,V1,K2,V2>> klass,
Class<? extends K1> inputKeyClass,
Class<? extends V1> inputValueClass,
Class<? extends K2> outputKeyClass,
Class<? extends V2> outputValueClass,
boolean byValue, JobConf mapperConf)
这就是我称呼它的方式
ChainMapper.addMapper(conf, NameMapper.class,
Object.class, Object.class, Object.class, Object.class,
false, nameYearConf);
其中NameMapper定义如下
public class NameMapper extends Mapper<Object, Object, Object, Object> { }
以及其他两个参数正确初始化
JobConf conf = new JobConf(); JobConf nameYearConf = new JobConf(false);
当我尝试编译时,我获得以下错误
src\MeanYear.java:107: error: method addMapper in class ChainMapper cannot be applied to given types;
ChainMapper.addMapper(conf, NameMapper.class, Object.class,
^
required: JobConf,Class<? extends Mapper<K1,V1,K2,V2>>,Class<? extends K1>,Class<? extends V1>,Class<? extends K2>,Class<? extends V2>,boolean,JobConf
found: JobConf,Class<MeanYear.NameMapper>,Class<Object>,Class<Object>,Class<Object>,Class<Object>,boolean,JobConf
reason: no instance(s) of type variable(s) K1,V1,K2,V2 exist so that argument type Class<MeanYear.NameMapper> conforms to formal parameter type Class<? extends Mapper<K1,V1,K2,V2>>
where K1,V1,K2,V2 are type-variables:
K1 extends Object declared in method <K1,V1,K2,V2>addMapper(JobConf,Class<? extends Mapper<K1,V1,K2,V2>>,Class<? extends K1>,Class<? extends V1>,Class<? extends K2>,Class<? extends V2>,boolean,JobConf)
V1 extends Object declared in method <K1,V1,K2,V2>addMapper(JobConf,Class<? extends Mapper<K1,V1,K2,V2>>,Class<? extends K1>,Class<? extends V1>,Class<? extends K2>,Class<? extends V2>,boolean,JobConf)
K2 extends Object declared in method <K1,V1,K2,V2>addMapper(JobConf,Class<? extends Mapper<K1,V1,K2,V2>>,Class<? extends K1>,Class<? extends V1>,Class<? extends K2>,Class<? extends V2>,boolean,JobConf)
V2 extends Object declared in method <K1,V1,K2,V2>addMapper(JobConf,Class<? extends Mapper<K1,V1,K2,V2>>,Class<? extends K1>,Class<? extends V1>,Class<? extends K2>,Class<? extends V2>,boolean,JobConf)
我真的无法理解我做错了什么,我尝试了一些类似的例子和编译,他们的工作。我也试过这里提供的一些例子,但没有成功。 感谢。
答案 0 :(得分:1)
我一眼就能猜到你是在混合来自新旧api软件包的课程:
ChainMapper
(从1.1.2开始)仅针对旧的API包(org.apache.hadoop.mapred
实施,并且尚未移植到新的API包(org.apache.hadoop.mapreduce
)。< / p>
因此,ChainMapper.addMapper
(Class<? extends Mapper<K1,V1,K2,V2>> klass
)的第二个参数应为org.apache.hadoop.mapred.Mapper
类型。
由于您当前的映射器NameMapper
扩展了Mapper类,这使我相信这是新的api Mapper类(org.apache.hadoop.mapreduce
),因为新API是一个类,而不是一个接口< / p>
您应该可以通过修改NameMapper
类的签名来修复以匹配旧的API Mapper接口的签名:
public class NameMapper extends MapReduceBase
implements Mapper<Object, Object, Object, Object> { }
这也意味着您当前的地图方法的签名需要从
更改protected void map(Object key, Object value, Context context) { }
// to
public void map(Object key, Object value, OutputCollector collector,
Reporter reporter) { }
如果你正在使用新Mapper API的设置和清理方法,你也需要更换它们:
public void close() { } // instead of cleanup(Context) {}
public void configure(JobConf conf) { } // instead of setup(Context) {}