2017-08-12 7 views
0

mapPartitionsに返されるIterator listWords()メソッドを受け入れる手助けがあります。Map Partition Iterator return

object MapPartitionExample { 

    def main(args: Array[String]): Unit = { 

    val conf= new SparkConf().setAppName("MapPartitionExample").setMaster("local[*]") 
    val sc= new SparkContext(conf) 

    val input:RDD[String] = sc.parallelize(List("ABC","DEF","GHU","YHG")) 

    val x= input.mapPartitions(word => listWords(word)) 


    } 

    def listWords(words: Iterator[String]) : util.Iterator[String] = { 

    val arrList = new util.ArrayList[String]() 
    while(words.hasNext) { 
     arrList.add(words.next()) 
    } 
    return arrList.iterator() 
    } 

} 

答えて

0

Iterable[NotInferU]が期待されていますが、それがあるようです残りのコードの

def listWords(words: Iterator[String]) : Iterator[String] = { 
    val arrList = new util.ArrayList[String]() 
    while(words.hasNext) { 
     arrList.add(words.next()) 
    } 
    import scala.collection.JavaConversions._ 
    return arrList.toList.iterator 
    } 

以下のようscala.collection.JavaConversions._をインポートすることにより

あなたはscala Iteratorjava.util.Iteratorを変換する必要がありますjava.util.Iterator[String]を戻ってきています。

私は答えがmapPartitionsで使用される機能の有用

+0

おかげでたくさん.... – Tinku

+0

私の喜びを@ Tinku :)それは動作しましたか? –

+0

はい、完全に動作します。 – Tinku

0

戻り値の型がscala.collection.Iterator、ないjava.util.Iteratorであるべきであると思います。私はあなたの現在のコードの多くのポイントが表示されていないが、あなたはScalaの可変コレクションを使用することができます。

import scala.collection.mutable.ArrayBuffer 

def listWords(words: Iterator[String]) : Iterator[String] = { 
    val arr = ArrayBuffer[String]() 
    while(words.hasNext) { 
    arr += words.next() 
    } 
    arr.toIterator 
} 

個人的に私は思いますちょうどmap

def listWords(words: Iterator[String]) : Iterator[String] = { 
    // Some init code 
    words.map(someFunction) 
} 
+0

ありがとうたくさん...それは働いた。 – Tinku

関連する問題