gpt4 book ai didi

java - 如何将参数传递给 Spark 中 mapPartitions 的用户定义函数?

转载 作者:可可西里 更新时间:2023-11-01 14:22:02 34 4
gpt4 key购买 nike

在 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)),我会得到一个错误。

最佳答案

像这样使用柯里化(Currying)函数:

def userdefinedFunc(someConstant: Long)(iter: Iterator[(Long, Array[SAMRecord])]): Iterator[(Long, Long)]

然后 userdefinedFunc(someConstant) 将是一个类型为 (iter: Iterator[(Long, Array[SAMRecord])]) => Iterator[(Long, Long)] 可以传递给 mapPartitions。

关于java - 如何将参数传递给 Spark 中 mapPartitions 的用户定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31614953/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com