且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用带有Java(+ lombok)的不可变类的spark中的反序列化错误

更新时间:2023-01-17 12:48:31

简单的出路

只需给它提供无参数的no-args构造函数即可.这将是可变的,但比您提供所有二传手的方式要少一些混乱.当您使用 Kryo作为反序列化器(我认为您已经这样做)时,可以将此构造函数设为私有.

easy way out

Just give it no-args constructor without any setters. It will be mutable but in slightly less chaotic fashion than if you provided all the setters. When you use Kryo as your deserializer (which I think you do already) you can keep this constructor private.

所有参数构造函数仍然可以使用空值和荒谬的值来调用.如果要对对象的有效性强加某些约定,请显式使用验证.如果您追求的是不变性,那么使用no-arg构造函数的成员将不再是最终的.

All-args constructor still can be called with nulls and insensical values. If you want to impose some contract on validity of the object, use validation explicitly. If it is immutability you are after, your members will not be final anymore using no-arg constructor.


对象创建的动态性质

反序列化通过调用最简单的(no-args)构造函数来进行,因为对于使用的实用程序的作者来说,这是一个更容易实现的过程,而不是将对所有参数的调用都具有所有必要的属性,而对所有参数的构造函数进行汇编并且不能保证分配给对象属性.


dynamic nature of object creation

The deserialization works by calling the simplest (no-args) constructor as this is much easier process to implement for the authors of utility used, rather than assembling one call to the all-args constructor with all the necessary properties order of which could be arbitrary and the assignment to object properties not guaranteed.

相反,他们创建了vanila对象,并通过设置器或反射器将其填充,以确保名称在序列化版本和对象版本之间匹配.All-args构造函数执行此操作的可靠性较低,并且难以实现.

Instead they create the vanila object and populate it via setters or reflection making sure the names match between serialised and object versions. All-args constructor would do this less reliably and would be much harder to implement.

如果需要保持不变性,则必须使用自定义对象创建.请查看 Kryo用于创建自定义对象的示例:

If you need to keep your immutability, you have to use custom object creation. Please have a look at Kryo's example for custom object creation:

Registration registration = kryo.register(SomeClass.class);
registration.setInstantiator(new ObjectInstantiator<SomeClass>() {
  public SomeClass newInstance () {
    return new SomeClass("some constructor arguments", 1234);
  }
});