如何在when-then函数中使用spark-sql字符串函数



如果文本列有英文文本,我想更改语言列的值。因此我使用了when-then函数,但它无法评估ascii火花字符串函数

import org.apache.spark.sql.functions._
val newdf = df.withColumn("asc", ascii("text"))
.withColumn("language", when(col("asc") > 0, "en")
.otherwise(col("hi")))

甚至尝试过

val newdf = df.withColumn("language", when(ascii(col("asc")) > 0, "en")
.otherwise(col("hi")))

函数"ascii"参数必须是列(而不是字符串(,并且在"otherwise"中必须指定字符串:

val newdf = df.withColumn("asc", ascii($"text"))
.withColumn("language", when(col("asc") > 0, "en")
.otherwise("hi"))

在您的代码中,问题是在几个地方混合了列名和列。以下代码将给出正确的结果:

df.withColumn("language", when(ascii(col("text")) > 0, "en").otherwise("hi"))

最新更新