您可以通过两种方式避免序列化:
- 假设其值可以是恒定的,请将格式化程序放在中
object
(使其成为“静态”)。这意味着可以在每个工作程序中访问静态值,而不是驱动程序将其序列化并发送给工作程序:
object MyUtils { val dtFormatter = DateTimeFormatter.ofPattern("<some non-ISO pattern>") } import MyUtils._ logs.flatMap(fileContent => { // can safely use formatter here })- 在匿名函数中按记录实例化它。这会带来一些性能损失(因为实例化将根据记录反复进行),因此仅在无法应用第一个选项时才使用此选项:
logs.flatMap(fileContent => { val dtFormatter = DateTimeFormatter.ofPattern("<some non-ISO pattern>") // use formatter here })


