Spark Dataframe Arraytype 列

Spark Dataframe Arraytype columns

我想在数据框上创建一个新列,这是将函数应用于数组类型列的结果。

像这样:

df = df.withColumn("max_$colname", max(col(colname)))

该列的每一行都包含一个值数组?

spark.sql.function 中的函数似乎只能在列的基础上工作。

您可以在数组列上应用用户定义的函数。

1.DataFrame

+------------------+
|               arr|
+------------------+
|   [1, 2, 3, 4, 5]|
|[4, 5, 6, 7, 8, 9]|
+------------------+

2.Creating UDF

import org.apache.spark.sql.functions._
def max(arr: TraversableOnce[Int])=arr.toList.max
val maxUDF=udf(max(_:Traversable[Int]))

3.Applying 查询中的 UDF

df.withColumn("arrMax",maxUDF(df("arr"))).show

4.Result

+------------------+------+
|               arr|arrMax|
+------------------+------+
|   [1, 2, 3, 4, 5]|     5|
|[4, 5, 6, 7, 8, 9]|     9|
+------------------+------+