我有一个问题,Scala 中两个看似相同的结构如何在尝试序列化时产生不同的结果。
我的问题是与 Spark Dataframe 创建相关的更大问题的一部分,但我已设法将问题隔离到嵌套结构,该结构被转换为“不可序列化”的 Row 对象。 我使用以下代码块来发现问题的根源,但即使如此,我也不知道为什么会出现问题。
try {
val byteOut = new ByteArrayOutputStream()
val objOut = new ObjectOutputStream(byteOut)
objOut.writeObject(p)
objOut.close()
sys.error("Serialization successful")
} catch {
case e: Exception => e.printStackTrace()
}
工作时间:
Record(
Map(
language -> Project(Variable(x1,RecordCType(Map(language -> StringType, info -> BagCType(RecordCType(Map(users -> StringType, difficulty -> IntType, average_review -> DoubleType)))))),language),
info -> Project(Variable(x1,RecordCType(Map(language -> StringType, info -> BagCType(RecordCType(Map(users -> StringType, difficulty -> IntType, average_review -> DoubleType)))))),info)))
不可序列化的p:
Record(
Map(
lName -> Project(Variable(x5,RecordCType(Map(s1_index -> LongType, nested -> BagCType(RecordCType(Map(users -> IntType, inUse -> BoolType))), userNo -> IntType, lName -> StringType))),lName),
nested -> Project(Variable(x5,RecordCType(Map(s1_index -> LongType, nested -> BagCType(RecordCType(Map(users -> IntType, inUse -> BoolType))), userNo -> IntType, lName -> StringType))),nested)))
我知道记录之间存在一些细微差别,但在结构方面它们或多或少是相同的。堆栈跟踪指向:
- object not serializable (class: scala.collection.immutable.MapLike$$anon$2, value: Map(users -> IntType, inUse -> BoolType))
但是如果这在 p2 中不可序列化,那么为什么是:
Map(users -> StringType, difficulty -> IntType, average_review -> DoubleType))
能够在 p1 中毫无问题地进行序列化,对我来说没有区别。
我在问题中添加了一些下面的类来提供上下文:
case class Record(fields: Map[String, CExpr]) extends CExpr {
def tp: RecordCType = {
RecordCType(fields.map(f => f._1 -> f._2.tp))
}
}
case class Project(e1: CExpr, field: String) extends CExpr
case class Variable(name: String, override val tp: Type) extends CExpr
final case class BagCType(tp: Type) extends Type
final case class RecordCType(attrTps: Map[String, Type]) extends Type
sealed trait Type extends Serializable
trait CExpr { self =>
def tp: Type
def vstr: String = self.toString
val isCacheUnfriendly: Boolean = false
}
我一定是遗漏了一些/误解了 Scala 中序列化的一些内容。任何人都可以看到我的问题,甚至指出我正确的方向吗?