gpt4 book ai didi

scala - 为什么可变映射在 Spark 中的 UserDefinedAggregateFunction(UDAF) 中自动变为不可变

转载 作者:行者123 更新时间:2023-12-04 12:05:46 26 4
gpt4 key购买 nike

我试图在 Spark 中定义一个 UserDefinedAggregateFunction(UDAF),它计算一个组列中每个唯一值的出现次数。

这是一个例子:
假设我有一个数据框 df像这样,

+----+----+
|col1|col2|
+----+----+
| a| a1|
| a| a1|
| a| a2|
| b| b1|
| b| b2|
| b| b3|
| b| b1|
| b| b1|
+----+----+

我将有一个 UDAF DistinctValues
val func = new DistinctValues

然后我将它应用到数据帧 df
val agg_value = df.groupBy("col1").agg(func(col("col2")).as("DV"))

我期待有这样的事情:
+----+--------------------------+
|col1|DV |
+----+--------------------------+
| a| Map(a1->2, a2->1) |
| b| Map(b1->3, b2->1, b3->1)|
+----+--------------------------+

所以我想出了一个像这样的 UDAF,
import org.apache.spark.sql.expressions.MutableAggregationBuffer
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.types.StructField
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.ArrayType
import org.apache.spark.sql.types.StringType
import org.apache.spark.sql.types.MapType
import org.apache.spark.sql.types.LongType
import Array._

class DistinctValues extends UserDefinedAggregateFunction {
def inputSchema: org.apache.spark.sql.types.StructType = StructType(StructField("value", StringType) :: Nil)

def bufferSchema: StructType = StructType(StructField("values", MapType(StringType, LongType))::Nil)

def dataType: DataType = MapType(StringType, LongType)
def deterministic: Boolean = true

def initialize(buffer: MutableAggregationBuffer): Unit = {
buffer(0) = scala.collection.mutable.Map()
}

def update(buffer: MutableAggregationBuffer, input: Row) : Unit = {
val str = input.getAs[String](0)
var mp = buffer.getAs[scala.collection.mutable.Map[String, Long]](0)
var c:Long = mp.getOrElse(str, 0)
c = c + 1
mp.put(str, c)
buffer(0) = mp
}

def merge(buffer1: MutableAggregationBuffer, buffer2: Row) : Unit = {
var mp1 = buffer1.getAs[scala.collection.mutable.Map[String, Long]](0)
var mp2 = buffer2.getAs[scala.collection.mutable.Map[String, Long]](0)
mp2 foreach {
case (k ,v) => {
var c:Long = mp1.getOrElse(k, 0)
c = c + v
mp1.put(k ,c)
}
}
buffer1(0) = mp1
}

def evaluate(buffer: Row): Any = {
buffer.getAs[scala.collection.mutable.Map[String, LongType]](0)
}
}

然后我在我的数据框中有这个功能,
val func = new DistinctValues
val agg_values = df.groupBy("col1").agg(func(col("col2")).as("DV"))

它给出了这样的错误,
func: DistinctValues = $iwC$$iwC$DistinctValues@17f48a25
org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 32.0 failed 4 times, most recent failure: Lost task 1.3 in stage 32.0 (TID 884, ip-172-31-22-166.ec2.internal): java.lang.ClassCastException: scala.collection.immutable.Map$EmptyMap$ cannot be cast to scala.collection.mutable.Map
at $iwC$$iwC$DistinctValues.update(<console>:39)
at org.apache.spark.sql.execution.aggregate.ScalaUDAF.update(udaf.scala:431)
at org.apache.spark.sql.execution.aggregate.AggregationIterator$$anonfun$12.apply(AggregationIterator.scala:187)
at org.apache.spark.sql.execution.aggregate.AggregationIterator$$anonfun$12.apply(AggregationIterator.scala:180)
at org.apache.spark.sql.execution.aggregate.SortBasedAggregationIterator.processCurrentSortedGroup(SortBasedAggregationIterator.scala:116)
at org.apache.spark.sql.execution.aggregate.SortBasedAggregationIterator.next(SortBasedAggregationIterator.scala:152)
at org.apache.spark.sql.execution.aggregate.SortBasedAggregationIterator.next(SortBasedAggregationIterator.scala:29)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at org.apache.spark.shuffle.sort.BypassMergeSortShuffleWriter.write(BypassMergeSortShuffleWriter.java:149)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:73)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

它看起来像在 update(buffer: MutableAggregationBuffer, input: Row)方法,变量 bufferimmutable.Map ,程序累把它投到 mutable.Map ,

但我用了 mutable.Map初始化 buffer initialize(buffer: MutableAggregationBuffer, input:Row) 中的变量方法。是否与传递给 update 的变量相同?方法?还有 buffermutableAggregationBuffer ,所以它应该是可变的,对吧?

为什么我的 mutable.Map 变得不可变?有谁知道发生了什么?

我真的需要这个函数中的可变 Map 来完成任务。我知道有一种解决方法可以从不可变映射创建可变映射,然后更新它。但是我真的很想知道为什么在程序中可变的会自动转换为不可变的,这对我来说没有意义。

最佳答案

相信是MapType在您的 StructType . buffer因此持有 Map ,这将是不可变的。

您可以转换它,但为什么不让它保持不变并执行以下操作:

mp = mp + (k -> c)

添加一个条目到不可变 Map ?

下面的工作示例:
class DistinctValues extends UserDefinedAggregateFunction {
def inputSchema: org.apache.spark.sql.types.StructType = StructType(StructField("_2", IntegerType) :: Nil)

def bufferSchema: StructType = StructType(StructField("values", MapType(StringType, LongType))::Nil)

def dataType: DataType = MapType(StringType, LongType)
def deterministic: Boolean = true

def initialize(buffer: MutableAggregationBuffer): Unit = {
buffer(0) = Map()
}

def update(buffer: MutableAggregationBuffer, input: Row) : Unit = {
val str = input.getAs[String](0)
var mp = buffer.getAs[Map[String, Long]](0)
var c:Long = mp.getOrElse(str, 0)
c = c + 1
mp = mp + (str -> c)
buffer(0) = mp
}

def merge(buffer1: MutableAggregationBuffer, buffer2: Row) : Unit = {
var mp1 = buffer1.getAs[Map[String, Long]](0)
var mp2 = buffer2.getAs[Map[String, Long]](0)
mp2 foreach {
case (k ,v) => {
var c:Long = mp1.getOrElse(k, 0)
c = c + v
mp1 = mp1 + (k -> c)
}
}
buffer1(0) = mp1
}

def evaluate(buffer: Row): Any = {
buffer.getAs[Map[String, LongType]](0)
}
}

关于scala - 为什么可变映射在 Spark 中的 UserDefinedAggregateFunction(UDAF) 中自动变为不可变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629916/

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