如何在火花scala中的数组中添加空值
问题描述:
val data = Array(-999.9,-0.5, -0.3, 0.0, 0.2, 999.9)
val dataFrame = sqlContext.createDataFrame(data.map(Tuple1.apply)).toDF("features")
我想在上面的数组中引入null条目。我在下面尝试,但它没有奏效。如何在火花scala中的数组中添加空值
val data = Array(-999.9,-0.5, -0.3, 0.0, 0.2, 999.9, null)
答
你需要让Option
类型的阵列和null
将无:
val data = Array(Some(-999.9),Some(-0.5), Some(-0.3), Some(0.0), Some(0.2), Some(999.9),None)
// data: Array[Option[Double]] = Array(Some(-999.9), Some(-0.5), Some(-0.3), Some(0.0), Some(0.2), Some(999.9), None)
val dataFrame = spark.createDataFrame(data.map(Tuple1.apply)).toDF("features")
// dataFrame: org.apache.spark.sql.DataFrame = [features: double]
dataFrame.show
+--------+
|features|
+--------+
| -999.9|
| -0.5|
| -0.3|
| 0.0|
| 0.2|
| 999.9|
| null|
+--------+
太谢谢你了。它工作 – user6200992
很酷。很高兴它有帮助。祝你好运! – Psidom