且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 spark scala 中使用带有 2 列的 array_contains?

更新时间:2023-02-15 09:16:17

你可以编写一个 udf 函数来完成你的工作

you can write a udf function to get your job done

import org.apache.spark.sql.functions._
def stringContains = udf((array: collection.mutable.WrappedArray[String], str: String) => array.contains(str))
df.withColumn("is_designer_present", when(stringContains(col("list_of_designers"), $"dept_resp"),1).otherwise(0))

你可以从udf函数本身返回适当的值,这样你就不必使用when函数

You can return appropriate value from udf function itself so that you don't have to use when function

import org.apache.spark.sql.functions._
def stringContains = udf((array: collection.mutable.WrappedArray[String], str: String) => if (array.contains(str)) 1 else 0)
df.withColumn("is_designer_present", stringContains(col("list_of_designers"), $"dept_resp"))