Apache spark Spark SQL在拆分后无法识别空值

Apache spark Spark SQL在拆分后无法识别空值,apache-spark,split,apache-spark-sql,Apache Spark,Split,Apache Spark Sql,我有类似的数据和问题来回答这里提出的问题: 我已经使用了Spark建议的解决方案您可以通过使用udf: val empty = udf(() => null: String) df.withColumn("likes", explode( when(col("likes").isNotNull, col("likes")) // If null explode an array<string> with a single null .otherwise(

我有类似的数据和问题来回答这里提出的问题:


我已经使用了Spark建议的解决方案您可以通过使用
udf

val empty = udf(() => null: String)

df.withColumn("likes", explode(
  when(col("likes").isNotNull, col("likes"))
    // If null explode an array<string> with a single null
    .otherwise(array(empty()))))
val empty=udf(()=>null:String)
df.withColumn(“likes”),爆炸(
当(col(“likes”).notnull时,col(“likes”))
//如果为null,则使用单个null分解数组
。否则(数组(空()))

我真的找到了一种方法。除非另有说明,否则必须写下以下内容:


。否则(数组(亮(无)。施放(“字符串”;))

您好,谢谢。我只是复制粘贴你的函数,我得到一个错误:文件“”,第1行:未定义的val empty=udf(()=>null:String)^SyntaxError:无效语法。也许它不适用于所有版本?我正在使用pysparkHi,我不知道为什么udf对我不起作用,但我在这里找到了另一种方法和答案。无论如何,我会把你的答案记为好的,这样它就能帮助别人。谢谢
df.withColumn("likes", f.when(col('likes').isNotNull(), 0).otherwise(2)).show()

+--------+------+
|likes   |origin|
+--------+------+
|    CARS|     0|
|    CARS|     0|
|    null|     0|
|    null|     0|
val empty = udf(() => null: String)

df.withColumn("likes", explode(
  when(col("likes").isNotNull, col("likes"))
    // If null explode an array<string> with a single null
    .otherwise(array(empty()))))