2017-04-26 71 views
2
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) 

回答

1

你需要讓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| 
+--------+ 
+0

太謝謝你了。它工作 – user6200992

+0

很酷。很高興它有幫助。祝你好運! – Psidom