嗨,我被困在实现一个自定义条件的火花DF。基本上,我希望根据列中的空值将列标记为0或1,也就是说,如果有的话
列包含空,与该行对应的状态将为0 status 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发布于 2018-12-30 16:06:59
val df = someDF.withColumn("status", when(fieldList.map(x => col(x).isNull).reduce(_ || _), 1).otherwise(0)首先,将每个列名转换为一个列,然后检查它是否为空(映射),如果至少有一个为null,那么简单的减缩就会导致true。
https://stackoverflow.com/questions/53978854
复制相似问题