Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use array_contains with 2 columns in spark scala?

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))

error:

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)
like image 588
user3222101 Avatar asked Mar 07 '23 01:03

user3222101


2 Answers

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))

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"))
like image 168
Ramesh Maharjan Avatar answered Mar 10 '23 05:03

Ramesh Maharjan


With Spark 1.6 you can wrap your array_contains() as a string into the expr() function:

import org.apache.spark.sql.functions.expr

.withColumn("is_designer_present",
    when(expr("array_contains(list_of_designers, dept_resp)"), 1).otherwise(0))

This form of array_contains inside the expr can accept a column as the second argument.

like image 44
Civyshk Avatar answered Mar 10 '23 05:03

Civyshk