【发布时间】:2019-03-31 16:15:00
【问题描述】:
我正在尝试在 Scala 中使用 regexp_replace 将空格替换为 null 值。但是,我尝试过的所有变体都没有达到预期的输出:
+---+-----+
| Id|col_1|
+---+-----+
| 0| null|
| 1| null|
+---+-----+
我试了一下,看起来像这样:
import org.apache.spark.sql.functions._
val df = spark.createDataFrame(Seq(
(0, " "),
(1, null),
(2, "hello"))).toDF("Id", "col_1")
val test = df.withColumn("col_1", regexp_replace(df("col_1"), "^\\s*", lit(Null)))
test.filter("col_1 is null").show()
【问题讨论】:
标签: scala null regexp-replace