簡體   English   中英

在Spark(Scala)中使用帶有Map的函數時,任務無法序列化

[英]Task not serializable when using a function with map in Spark (Scala)

另一個“任務無法序列化”的問題。 對不起,Scala和Spark太難了……

所以我有一個帶有方法的對象

object transformations {

  def someMethod()(df: DataFrame): DataFrame = {

      df.as[SomeCaseClass]
         .map(x => AnotherCaseClass(x.col1, x.col2, x.col3, x.col4.map(
          y => YetAnotherCaseClass(y.a.toString + '_' + y.b, y.c, y.d))
        )
        .toDF

  } 
}

我想將字符串操作帶到外面,以便可以重用。 所以,如果我做這樣的事情:

object transformations {

  def manipulateString()(a: Int, b: String): String = {
    return a.toString + '_' + b
  } 

  def someMethod()(df: DataFrame): DataFrame = {

      df.as[SomeCaseClass]
         .map(x => AnotherCaseClass(x.col1, x.col2, x.col3, x.col4.map(
          y => YetAnotherCaseClass(transformations.manipulateString(y.a, y.b), y.c, y.d))
        )
        .toDF

  } 
}

我收到任務無法序列化的錯誤。

正確的方法是什么?

謝謝!

問題出在transformations.manipulateString。 應該只是操縱字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM