簡體   English   中英

Spark(Scala):如何將Array [Row]轉換為DataSet [Row]或DataFrame?

[英]Spark (Scala): How to turn an Array[Row] into either a DataSet[Row] or a DataFrame?

我有一個Array [Row],我想將其轉換為Dataset[Row]DataFrame

我如何提出一個行數組?

好吧,我正在嘗試從數據集中清除null:

  • 無需過濾EACH列(我有很多)和..
  • 使用.na.drop()函數從DataFrameNaFunctions ,因為它不能檢測時,電池居然有字符串"null"

因此,我想出了以下行來過濾所有列中的null

val outDF = inputDF.columns.flatMap { col => inputDF.filter(col + "!='' AND " + col + "!='null'").collect() }

問題是,outDF是Array[Row] ,因此是一個問題! 任何想法歡迎!

這是您的代碼可以正常工作的方式:

inputDF.columns.map {
  col => inputDF.filter((inputDF(col) =!= "") and (inputDF(col) =!= "null"))
}.reduce(_ union _)

像這樣:

inputDF.where(inputDF.columns.map {
  col => (inputDF(col) =!= "") and (inputDF(col) =!= "null")
}.foldLeft(lit(true))(_ and _))

是你想要的。

請注意,第一個解決方案創建了非排他的子集,因此具有如下數據:

val inputDF = Seq(("1", "a"), ("2", ""), ("null", "")).toDF

結果將是:

+---+---+
| _1| _2|
+---+---+
|  1|  a|
|  2|   |
|  1|  a|
+---+---+

對於解決方案,我認為是正確的:

+---+---+
| _1| _2|
+---+---+
|  1|  a|
+---+---+

我根據我的評論發布答案。

df.na.drop(df.columns).where("'null' not in ("+df.columns.mkString(",")+")")

根據Srinivas先生的評論,通過使用以下代碼來回答此問題:

//First drop all typical nulls
val prelimDF = inputDF.na.drop()

//Then drops all columns actually saying 'null'
val finalDF = prelimDF.na.drop(prelimDF.columns).where("'null' not in ("+prelimDF.columns.mkString(",")+")")

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM