以下用于从 Row 读取值的 Scala (Spark 1.6) 代码NullPointerException
在值为 null 时失败并显示 a。
val test = row.getAs[Int]("ColumnName").toString
虽然这很好用
val test1 = row.getAs[Int]("ColumnName") // returns 0 for null
val test2 = test1.toString // converts to String fine
是什么原因造成NullPointerException
的,处理此类情况的推荐方法是什么?
PS:从DataFrame中获取行如下:
val myRDD = myDF.repartition(partitions)
.mapPartitions{ rows =>
rows.flatMap{ row =>
functionWithRows(row) //has above logic to read null column which fails
}
}
functionWithRows
上面已经提到了NullPointerException
。
MyDF 架构:
root
|-- LDID: string (nullable = true)
|-- KTAG: string (nullable = true)
|-- ColumnName: integer (nullable = true)