如何将参数传递给 Spark 中 mapPartitions 的用户定义函数?
How to pass an argument to a user defined function for mapPartitions in Spark?
在 Spark 中,您可以为 mapPartitions
使用用户定义的函数。现在我的问题是如何向它传递参数。例如,目前我有这样的东西,它被称为使用 rdd.mapPartitions(userdefinedFunc)
.
def userdefinedFunc(iter: Iterator[(Long, Array[SAMRecord])]) : Iterator[(Long, Long)] =
{
val res = scala.collection.mutable.ArrayBuffer.empty[(Long, Long)]
// Code here
res.iterator
}
但是,我还想要一个常量作为该用户定义函数的参数,例如,它看起来如下。
def userdefinedFunc(iter: Iterator[(Long, Array[SAMRecord])], someConstant: Long) :
Iterator[(Long, Long)] =
{
val res = scala.collection.mutable.ArrayBuffer.empty[(Long, Long)]
// Code here
res.iterator
}
现在如何使用 mapPartitions
调用该函数。如果我只使用 rdd.mapPartitions(userdefinedFunc(someConstant))
.
,我会收到错误消息
像这样使用柯里化函数:
def userdefinedFunc(someConstant: Long)(iter: Iterator[(Long, Array[SAMRecord])]): Iterator[(Long, Long)]
那么 userdefinedFunc(someConstant)
将是一个类型为 (iter: Iterator[(Long, Array[SAMRecord])]) => Iterator[(Long, Long)]
的函数,您可以将其传递给 mapPartitions。
在 Spark 中,您可以为 mapPartitions
使用用户定义的函数。现在我的问题是如何向它传递参数。例如,目前我有这样的东西,它被称为使用 rdd.mapPartitions(userdefinedFunc)
.
def userdefinedFunc(iter: Iterator[(Long, Array[SAMRecord])]) : Iterator[(Long, Long)] =
{
val res = scala.collection.mutable.ArrayBuffer.empty[(Long, Long)]
// Code here
res.iterator
}
但是,我还想要一个常量作为该用户定义函数的参数,例如,它看起来如下。
def userdefinedFunc(iter: Iterator[(Long, Array[SAMRecord])], someConstant: Long) :
Iterator[(Long, Long)] =
{
val res = scala.collection.mutable.ArrayBuffer.empty[(Long, Long)]
// Code here
res.iterator
}
现在如何使用 mapPartitions
调用该函数。如果我只使用 rdd.mapPartitions(userdefinedFunc(someConstant))
.
像这样使用柯里化函数:
def userdefinedFunc(someConstant: Long)(iter: Iterator[(Long, Array[SAMRecord])]): Iterator[(Long, Long)]
那么 userdefinedFunc(someConstant)
将是一个类型为 (iter: Iterator[(Long, Array[SAMRecord])]) => Iterator[(Long, Long)]
的函数,您可以将其传递给 mapPartitions。