如何在spark scala中的数组中添加空值

时间:2017-04-26 14:26:59

标签: scala apache-spark

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条目。我在下面试过,但它没有用。

val data = Array(-999.9,-0.5, -0.3, 0.0, 0.2, 999.9, null)

1 个答案:

答案 0 :(得分:3)

您需要制作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|
+--------+