I do not see the built-in function, but you can use UDF:
import scala.collection.mutable.WrappedArray;
val intersect = udf ((a : WrappedArray[Int], b : WrappedArray[Int]) => {
var count = 0;
a.foreach (x => {
if (b.contains(x)) count = count + 1;
});
count;
});
val one = sc.parallelize(List(
(1, Array(1, 2, 3)),
(2, Array(1,2 ,3, 4)),
(3, Array(1, 2,3)),
(4, Array(1,2))
)).toDF("user", "arr");
val two = sc.parallelize(List(
(1, Array(1, 2, 3)),
(2, Array(1,2 ,3, 4)),
(3, Array(1, 2, 3)),
(4, Array(1))
)).toDF("user", "arr");
one.join(two, one("user") === two("user"))
.select (one("user"), intersect(one("arr"), two("arr")).as("intersect"))
.where(col("intersect") > 2).show
one.join(two)
.select (one("user"), two("user"), intersect(one("arr"), two("arr")).as("intersect")).
where('intersect > 2).show
source
share