【发布时间】:2018-02-18 09:18:54
【问题描述】:
我编写了下面的代码,用于在 RDD 对象中查找偶数和它之前的数字。在此,我首先将其转换为 List 并尝试使用我自己的函数来查找偶数和它们之前的数字。以下是我的代码。在此我创建了一个空列表,我试图在其中一个一个地附加数字。
object EvenandOdd
{
def mydef(nums:Iterator[Int]):Iterator[Int]=
{
val mylist=nums.toList
val len= mylist.size
var elist=List()
var i:Int=0
var flag=0
while(flag!=1)
{
if(mylist(i)%2==0)
{
elist.++=List(mylist(i))
elist.++=List(mylist(i-1))
}
if(i==len-1)
{
flag=1
}
i=i+1
}
}
def main(args:Array[String])
{
val myrdd=sc.parallelize(List(1,2,3,4,5,6,7,8,9,10),2)
val myx=myrdd.mapPartitions(mydef)
myx.collect
}
}
我无法在 Scala shell 和 Eclipse 中执行此命令,也无法找出错误,因为我只是 Scala 的初学者。
以下是我在 Scala Shell 中遇到的错误。
<console>:35: error: value ++= is not a member of List[Nothing]
elist.++=List(mylist(i))
^
<console>:36: error: value ++= is not a member of List[Nothing]
elist.++=List(mylist(i-1))
^
<console>:31: error: type mismatch;
found : Unit
required: Iterator[Int]
while(flag!=1)
^
【问题讨论】:
-
请显示您遇到的具体错误。
-
@n.m.我已添加收到的错误。
-
a) 不要使用变量。 b) 3 消息通知您,while 循环计算为一个单元。作为您方法的最后一条语句,它违反了断言,返回一个 Iterator[Int]。 c) 输入数据的期望结果是什么? d) 什么是 sc (sc.parallelize ...)?
标签: scala apache-spark rdd