首页 > 解决方案 > Scala代码替换列表形式的数据框中列的空值

问题描述

我正在尝试从列表形式的数据集中替换空值。我使用了下面的代码仍然没有给我想要的结果。我应该怎么办?

我正在使用以下代码:

 val mergedDS =customerDS.join(acctstep1,Seq("customerId"),"outer")
    .withColumn("numberAccounts", 'numberAccounts.cast("Int"))
    .withColumn("totalBalance",'totalBalance.cast("Long"))

  // Lets remove all the accounts with missing values for customers
  val customerAccountOutputDS = mergedDS.as[CustomerAccountOutput].na.fill(0).show(false)
  print(customerAccountOutputDS)

在此处输入图像描述

标签: listscalareplace

解决方案


import org.apache.spark.sql.functions._
import org.apache.spark.sql.SparkSession;
object DefaultEmptyArray {

  def main(args: Array[String]): Unit = {
    val input = List(Bean(List("A","B")),Bean(null),Bean(List("C","D")))
    val spark = SparkSession.builder().master("local[*]").getOrCreate();
    val df  = spark.createDataFrame(input)

    df.select("inputList")
      .withColumn("outputList",when(col("inputList").isNull,Array[String]()).otherwise(col("inputList")))
      .show()
  }
}

case class Bean( inputList : List[String])

推荐阅读