更新时间:2023-01-19 17:12:12
您可以编写一个简单的udf来检查数组中是否存在该元素:
You can write a simple udf that will check if the element is present in the array :
val arrayContains = udf( (col1: Int, col2: Seq[Int]) => if(col2.contains(col1) ) 1 else 0 )
然后调用它并以正确的顺序传递必要的列:
And then just call it and pass the necessary columns in the correct order :
df.withColumn("hasAInB", arrayContains($"a", $"b" ) ).show
+---+---------+-------+
| a| b|hasAInB|
+---+---------+-------+
| 1| [1, 2]| 1|
| 2|[2, 3, 4]| 1|
| 3| [1, 4]| 0|
+---+---------+-------+