如何使用 Java 在 Spark SQL 中加入多个列以在 DataFrame 中进行过滤

How to Join Multiple Columns in Spark SQL using Java for filtering in DataFrame

我尝试使用

a.join(b,a.col("x").equalTo(b.col("x")) && a.col("y").equalTo(b.col("y"),"inner")

但是 Java 抛出错误提示 && 是不允许的。

Spark SQL 在 Column 上提供了一组标记为 java_expr_ops 的方法,这些方法专为 Java 互操作性而设计。它包括 and (see also or) 可以在这里使用的方法:

a.col("x").equalTo(b.col("x")).and(a.col("y").equalTo(b.col("y"))

如果你想使用多列连接,你可以这样做:

a.join(b,scalaSeq, joinType)

您可以将列存储在 Java-List 中并将 List 转换为 Scala seq。 Java-List 到 Scala-Seq 的转换:

scalaSeq = JavaConverters.asScalaIteratorConverter(list.iterator()).asScala().toSeq();

示例:a = a.join(b, scalaSeq, "inner");

注意:通过这种方式可以轻松支持动态列。