我有一个sparkscala数据框,其中一列是struct,当struct中的所有值都为null时,我想要null而不是objects。
val someDF = Seq(
(8, null,null),
(64, "mouse", "s"),
(-27, "horse", "e")
).toDF("a", "b", "c")
def make_week_struct (week:String) : Column = {
val summary = struct($"b", $"c").alias(s"wks_${week}_jrny")
return summary
}
val week1_summary = make_week_struct("1")
var dd = someDF.select($"a",week1_summary)
display(dd)
样本数据
a b c
8 null null
64 mouse s
-27 horse e
电流输出
a wks_1_jrny
8 object:{a:null, b:null}
64 object:{a:"mouse", b:"s"}
-27 object:{a:"horse", b:"e"}
预期产量
a wks_1_jrny
8 null
64 object:{a:"mouse", b:"s"}
-27 object:{a:"horse", b:"e"}
2条答案
按热度按时间u1ehiz5o1#
这也应该起作用:
dzhpxtsq2#
你也可以使用
to_json
函数筛选空json(&F){}
.