如何在 spark scala 中使用带有 2 列的 array_contains? [英] How to use array_contains with 2 columns in spark scala?
本文介绍了如何在 spark scala 中使用带有 2 列的 array_contains?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我想检查一个字符串数组是否包含另一列中存在的字符串.我目前正在使用以下代码,但出现错误.
I have an issue , I want to check if an array of string contains string present in another column . I am currently using below code which is giving an error.
.withColumn("is_designer_present", when(array_contains(col("list_of_designers"),$"dept_resp"),1).otherwise(0))
错误:
java.lang.RuntimeException: Unsupported literal type class org.apache.spark.sql.ColumnName dept_resp
at org.apache.spark.sql.catalyst.expressions.Literal$.apply(literals.scala:77)
推荐答案
你可以编写一个 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"))
这篇关于如何在 spark scala 中使用带有 2 列的 array_contains?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文