【发布时间】:2018-12-31 00:06:59
【问题描述】:
您好,我一直坚持在 spark DF 上实现自定义条件。基本上我想根据列中存在的 Null 值将列标记为 0 或 1,即如果有
column contains null 对应该行的状态为0 否则 1
val someData = Seq(
Row(8, "bat"),
Row(64, "mouse"),
Row(null, "rat")
)
val someSchema = List(
StructField("number", IntegerType, true),
StructField("word", StringType, true)
)
val someDF = sparkSession.createDataFrame(
sparkSession.sparkContext.parallelize(someData),
StructType(someSchema)
)
val fieldList: Seq[Column] = Seq(col("word"),col("number"))
val df = fieldList.foldLeft(inputDf)(
(inputDf, f) => {
dfin = inputDf.withColumn(Status, lit(0))
dfin
.withColumn(
Status,
when(f.isNotNull and col("status").isin(0), 0).otherwise(1)
)
}
但它会根据fieldList 中的最后一列进行检查,但应该是这样的
col 1 col2 status
zyx . pqe . 0
null . zyz . 1
xdc . null 1
null null 1
【问题讨论】:
标签: scala apache-spark