2012-03-25 95 views
7

在我開始使用Scoobi或Scrunch之前,我曾試着使用Hadoop(0.20.1)的java綁定將WordCount移植到scala(2.9.1)。Scala/Hadoop:指定Reducer的上下文

我本來:

class Map extends Mapper[LongWritable, Text, Text, IntWritable] { 
    @throws[classOf[IOException]] 
    @throws[classOf[InterruptedException]] 
    def map(key : LongWritable, value : Text, context : Context) { 
    //... 

哪個編譯罰款,但給了我一個運行時錯誤:

java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.LongWritable 

環視了一下之後,我想通了,那是因爲我沒」 t定義了適當的方法(應該已經被缺少override提示),所以我將它固定爲:

override def map(key : LongWritable, value : Text, 
    context : Mapper[LongWritable, Text, Text, IntWritable]#Context) { 

並且瞧,沒有運行時錯誤。

但後來我看着工作輸出,並意識到我的減速機沒有運行。

所以我看着我減速,並注意到reduce簽名有同樣的問題我的映射:

class Reduce extends Reducer[Text, IntWritable, Text, IntWritable] { 
    @throws[classOf[IOException]] 
    @throws[classOf[InterruptedException]] 
    def reduce(key : Text, value : Iterable[IntWritable], context : Context) { 
    //... 

所以我猜reduce正在因爲不匹配所用的身份。

但是,當我試圖糾正reduce簽名:

override def reduce(key: Text, values : Iterable[IntWritable], 
    context : Reducer[Text, IntWritable, Text, IntWritable]#Context) { 

我現在有一個編譯器錯誤:

[ERROR] /path/to/src/main/scala/WordCount.scala:32: error: method reduce overrides nothing 
[INFO]  override def reduce(key: Text, values : Iterable[IntWritable], 

所以我不知道我做錯了。

+0

減少簽名應該是什麼? – 2012-03-25 02:12:25

+0

@ DanielC.Sobral:這是我的問題。 – rampion 2012-03-25 02:14:37

回答

11

乍一看,確保值是java.lang.Iterable,而不是scala Iterable。導入java.lang.Iterable,或者:

override def reduce(key: Text, values : java.lang.Iterable[IntWritable], context : Reducer[Text, IntWritable, Text, IntWritable]#Context) 
+2

這是完全正確的。請參閱此處的示例:https://bitbucket.org/jasonbaldridge/fogbow/src/6c24fb2afda4/src/main/scala/fogbow/example/WordCount.scala – dhg 2012-03-25 02:47:38