gpt4 book ai didi

Java Hadoop 与 ChainMapper 泛型的问题

转载 作者:行者123 更新时间:2023-12-01 14:24:10 27 4
gpt4 key购买 nike

您好,我目前面临 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);

当我尝试编译时出现以下错误

    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)

我真的无法理解我做错了什么,我尝试了一些类似的例子和编译,它们工作了。我也尝试过这里提供的一些例子,但没有成功。谢谢。

最佳答案

乍一看,我的猜测是您正在混合新旧 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 是一个类,而不是一个接口(interface)

您应该能够通过修改 NameMapper 的签名来修复类以匹配旧的 API Mapper 接口(interface):

public class NameMapper extends MapReduceBase 
implements Mapper<Object, Object, Object, Object> { }

这也意味着您当前的 map 方法的签名需要更改

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) {}

关于Java Hadoop 与 ChainMapper 泛型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17293291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com