我有一个表,其中包含名为的数组类型列writer
其值如下array[value1, value2]
, array[value2, value3]
.... ETC。
我在做self join
获得数组之间具有共同值的结果。我试过:
sqlContext.sql("SELECT R2.writer FROM table R1 JOIN table R2 ON R1.id != R2.id WHERE ARRAY_INTERSECTION(R1.writer, R2.writer)[0] is not null ")
And
sqlContext.sql("SELECT R2.writer FROM table R1 JOIN table R2 ON R1.id != R2.id WHERE ARRAY_INTERSECT(R1.writer, R2.writer)[0] is not null ")
但得到了同样的例外:
线程“main”org.apache.spark.sql.AnalysisException 中出现异常:
未定义的函数:“ARRAY_INTERSECT”。该函数既不是
注册的临时功能或注册的永久功能
数据库“默认”。;第 1 行 位置 80
Spark SQL 可能不支持ARRAY_INTERSECTION
and ARRAY_INTERSECT
。我怎样才能实现我的目标Spark SQL
?
从 Spark 2.4 开始array_intersect https://spark.apache.org/docs/latest/api/sql/index.html#array_intersect函数可以直接在SQL中使用
spark.sql(
"SELECT array_intersect(array(1, 42), array(42, 3)) AS intersection"
).show()
+------------+
|intersection|
+------------+
| [42]|
+------------+
and Dataset
API:
import org.apache.spark.sql.functions.array_intersect
Seq((Seq(1, 42), Seq(42, 3)))
.toDF("a", "b")
.select(array_intersect($"a", $"b") as "intersection")
.show()
+------------+
|intersection|
+------------+
| [42]|
+------------+
其他语言中也存在等效函数:
-
pyspark.sql.functions.array_intersect https://spark.apache.org/docs/latest/api/python/pyspark.sql.html?highlight=array_intersect#pyspark.sql.functions.array_intersect在 PySpark 中。
-
SparkR::array_intersect https://spark.apache.org/docs/latest/api/R/column_collection_functions在 SparkR 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)