嗨,我目前正面临Java泛型的问题:我需要使用的方法有这个签名
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);
当我试图编译时,我得到了以下错误
srcMeanYear.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)
我真的不明白我做错了什么,我尝试了一些类似的例子和编译,它们都起作用了。我也尝试过这里提供的一些例子,但没有成功。谢谢
乍一看,我猜你是在混合新的和旧的api包中的类:
ChainMapper
(自1.1.2起)仅针对旧的API包(org.apache.hadoop.mapred
)实现,尚未移植到新的API包(org.apache.hadoop.mapreduce
)。
因此,ChainMapper.addMapper
(Class<? extends Mapper<K1,V1,K2,V2>> klass
)的第二自变量被期望为org.apache.hadoop.mapred.Mapper
类型。
由于您当前的映射器NameMapper
扩展了mapper类,这使我相信这是新的api mapper类(org.apache.hadoop.mapreduce
),因为新的api是一个类,而不是接口
您应该能够通过修改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) {}