如果在 Apache Spark Java 中为 null,则将行的值替换为另一个值

jgp*_*jgp 1 java dataframe apache-spark apache-spark-sql

我在 Java 中使用 Apache Spark 1.6.2。

我有一个包含以下内容的数据帧:

  • 创建日期字段中的日期,
  • close_date 字段中的结束日期。

如果业务未关闭,则 close_date 中的值为 null。

我想要:

  • 向我的 DataFrame 添加一个名为 last_date_business 的额外列
  • 用 close_date 的值填充它
  • 如果 close_date 为空,则使用 current_date()

我可以让 Spark 来做还是应该手动做?

zer*_*323 7

您只需要一个coalesce

import static org.apache.spark.sql.functions.*;

df.withColumn("last_date_business", coalesce(col("close_date"), current_date()));
Run Code Online (Sandbox Code Playgroud)