发现类型不匹配:预期单位:RDD [XYZ ...] [英] type mismatch found : Unit expected : RDD[XYZ...]
问题描述
在以下情况下,我遇到类型不匹配错误
I'm getting type mismatch error for below scenario
//scores sample file
0000006,Danny,6.2
0000002,Danny,7.0
代码.
case class Person(id: String, dob: String, country: String, score: Double)
def getResultRecords(persons: RDD[List[String]]): RDD[Person] = {
val personUS = persons.map(rec => Person(rec.head, rec(1),"US"), java.lang.Double.parseDouble(cpg.getOrElse(rec.head,"0").toString())
val personMX = persons.map(rec => Person(rec.head, rec(1),"MX"),java.lang.Double.parseDouble(cpg.getOrElse(rec.head,"0").toString()))
val personAll = personUS.union(personMX)
//return personAll
}//Getting an error saying " type mismach found : Unit expected : RDD[Person]"
我在这里做错了什么?我该如何解决?
What am I doing wrong here? How can I fix it?
请帮助我.
推荐答案
您的方法签名为 def getResultRecords(persons:RDD [List [String]]):RDD [Person]
;这意味着该方法应返回 RDD [Person]
的实例.
YOur method signature is def getResultRecords(persons: RDD[List[String]]): RDD[Person]
; that means the method should return an instance of RDD[Person]
.
但是,同一方法的最后一行说- val personAll = personUS.union(personMX)
-不返回任何内容.
However, the last line of that same method says - val personAll = personUS.union(personMX)
- which returns nothing.
取消注释 return personAll
行-这将清除您的异常.
Uncomment return personAll
line - and this will erase your exception.
此外,我发现您的语法有问题.
Also, I found some issue with your syntax.
这是您的代码段.
case class Person(id: String, dob: String, country: String, score: Double)
def getResultRecords(persons: RDD[List[String]]): RDD[Person] = {
val personUS = persons.map(rec => Person(rec.head, rec(1),"US", java.lang.Double.parseDouble(cpg.getOrElse(rec.head,"0").toString()))
val personMX = persons.map(rec => Person(rec.head, rec(1),"MX",java.lang.Double.parseDouble(cpg.getOrElse(rec.head,"0").toString()))
val personAll = personUS.union(personMX)
return personAll
}
此处提供了额外的括号)
- Person(rec.head,rec(1),"US"
.并且 return
语句已被注释掉.
An extra parenthesis )
is given here - Person(rec.head, rec(1),"US"
.
And return
statement was commented out.
这篇关于发现类型不匹配:预期单位:RDD [XYZ ...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!