2013-06-25 91 views
0

嗨,我目前正面臨着Java的泛型的問題: 我需要使用的方法有此簽名的Java Hadoop的問題ChainMapper泛型

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

還有其他呃兩個參數都正確intialized

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) 

我真的不明白我在做什麼錯了,我嘗試了一些類似的例子和編譯和他們工作。我也嘗試了一些在這裏提供的例子,但沒有成功。 謝謝。

回答

1

一覽我的猜測是你從新舊API包混合類:

ChainMapper(如1.1.2)只對舊的API包(org.apache.hadoop.mapred)來實現,並具有不已被移植到新的API包(org.apache.hadoop.mapreduce)。

因此ChainMapper.addMapperClass<? extends Mapper<K1,V1,K2,V2>> klass)的第二個參數預計爲org.apache.hadoop.mapred.Mapper類型。

作爲當前映射NameMapper擴展映射器類,這使我相信,這是新的API映射器類(org.apache.hadoop.mapreduce),作爲新的API是一個類,而不是一個接口

你應該能夠通過修改您的NameMapper類的簽名來解決,以匹配舊的API映射接口:

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

這也意味着當前的地圖方法的簽名需要從

改變3210
protected void map(Object key, Object value, Context context) { } 
// to 
public void map(Object key, Object value, OutputCollector collector, 
       Reporter reporter) { } 

如果你正在使用新的API映射的設置和清除方法,你就需要更換它們太:

public void close() { } // instead of cleanup(Context) {} 
public void configure(JobConf conf) { } // instead of setup(Context) {} 
+0

的感謝!現在它工作! 但是我想知道在新的API中是否存在類似的可能性,即能夠連接多個map/reduce作業而不必將其寫入fs,而是將中間結果直接保存在內存中。 –