val mappedDpt = dept.map(p => Department( p(0).toInt,p(1).toString))p这是一个字符串而不是一行 (你可能认为)。更精确的p是这里是文本文件的每一行,你可以确认读取scaladoc。
“ 返回文本文件行的RDD”。
因此,当您应用apply方法((0))时,您将按行上的位置访问字符。这就是为什么你"49, ','"从toInt第一个字符串中得到49,它返回字符的ascii值和','行上的第二个字符。
编辑如果需要重现该read方法,可以执行以下操作:
object Department { /* The Option here is to handle errors. / def fromRawArray(data: Array[String]): Option[Department] = data match {
case Array(raw_dept_id, dept_name) => Some(Department(raw_dept_id.toInt, dept_name))
case _ => None
}}
// We use flatMap instead of map, to unwrap the values from the Option, the Nones get removed.val mappedDpt = dept.flatMap(line => Department.fromRawArray(line.split(",")))但是,我希望这只是为了学习。在生产代码上,你应该一直使用该read版本。