遇到一个坑,我在使用spark-core时,使用了foreachPartition方法,程序如下
使用foreachPartition进行往redis中存值,对里面的Iterator使用了map方法,但是并没有存进去,换成foreach方法可以
val conf = new SparkConf().setAppName(this.getClass.getName).setMaster("local")
val sc = new SparkContext(conf)
sc.textFile("D:\\Yue\\项目\\用户画像\\app_dict.txt").map(_.split("\t",-1)).filter(_.size>=4)
.map(t=>(t(4),t(1))).foreachPartition(it=>{
val jedis = JedisPools.getJedis()
//此处调用了迭代器的map方法,但是 平没有起作用,没有存到jedis中,但是使用foreach可以
it.map(m=>{
jedis.hset("dict2",m._1,m._2)
})
})
sc.stop()
原因:本来以为时foreachPartition方法的问题,但是后来发现并不是,这个Scala的迭代器的原因:
写了个程序进行了测试,对一个普通的scala的迭代器进行map方法,在map中进行打印,然而什么都没有打印,索引就证明了对于迭代器来说,map方法完全没有作用
object Test01f {
def main(args: Array[String]): Unit = {
val it: Iterator[Int] = List(1,5,6,8).toIterator
it.map(t=>{
print(t)
})
}
}