sparkArray的火花数组

时间:2018-04-09 20:39:18

标签: apache-spark apache-spark-sql spark-dataframe

我的spark中的数据帧非常复杂。我正在尝试使用一个占用2列的UDF,然后同时在每列的每一行上运行一个函数。

每列具有以下相同的架构:

root
 |-- A: array (nullable = true)
 |    |-- element: double (containsNull = true)

在某些情况下,数组将为空,在其他情况下,它将包含元素,计数会有所不同。

当我在列上执行.dtypes时:

test: Array[(String, String)] = Array((A,ArrayType(DoubleType,true)))

当我在其中一个栏目上选择(1)时,我得到了一个

Array[org.apache.spark.sql.Row] = Array([WrappedArray(1234, 4567, 789, 1346)])

当我在列上运行select时,我得到:

org.apache.spark.sql.DataFrame = [A: array<double>]

我的目标是运行一个使每列相同元素的函数。

def inRange = udf((A: ???, B: ??? ) => {
   //iterate over the array and run coolFunction(A(0),B(0))
 })

我在这个

中运行udf
df.withColumn("coolFunction", coolFunction(df("A"), df("B"))) 

2 个答案:

答案 0 :(得分:1)

您可以使用udf作为

定义collection.mutable.WrappedArray[Double]功能
def inRange = udf((A: collection.mutable.WrappedArray[Double], B: collection.mutable.WrappedArray[Double]) => {
  //iterate over the array and run coolFunction(A(0),B(0))
})

您还可以使用WrappedArrayIndexedSeq

Seq父类
def inRange = udf((A: collection.mutable.IndexedSeq[Double], B: collection.mutable.IndexedSeq[Double]) => {
  //iterate over the array and run coolFunction(A(0),B(0))
})

def inRange = udf((A: Seq[Double], B: Seq[Double]) => {
  //iterate over the array and run coolFunction(A(0),B(0))
})

答案 1 :(得分:0)

应该是:

def inRange = udf((A: Seq[Double], B: Seq[Double]) => {
    //iterate over the array and run coolFunction(A(0),B(0))
})

参考https://spark.apache.org/docs/latest/sql-programming-guide.html#data-types

相关问题