You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@flink.apache.org by Dominik Wosiński <wo...@gmail.com> on 2018/08/26 15:58:31 UTC
Dealing with Not Serializable classes in Java
Hey,
I was wondering how do You normally deal with fields that contain
references that are not serializable. Say, we have a custom serialization
schema in Java that needs to serialize *LocalDateTime* field with
*ObjectMapper.* This requires registering specific module for
*ObjectMapper* and this makes it not serializable (module contains some
references to classes that are not serializable).
Now, if You would initialize *ObjectMapper *directly in the field this will
cause an exception when deploying the job.
Normally I would do :
@Override
public byte[] serialize(Backup backupMessage) {
if(objectMapper == null) {
objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
}
...
}
But I was wondering whether do You have any prettier option of doing this?
Thanks,
Dominik.
Re: Dealing with Not Serializable classes in Java
Posted by Dominik Wosiński <wo...@gmail.com>.
Hey ;)
I have received one response that was sent directly to my email and not to
user group :
> Hi Dominik,
>
> I think you can put the unserializable fields into RichFunctions and
> initiate them in the `open` method, so the the fields won’t need to be
> serialized with the tasks.
>
> Best,
> Paul Lam
>
And my response about RichFunctions was meant for Paul :)
Pon., 27.08.2018, 10:38 użytkownik Chesnay Schepler <ch...@apache.org>
napisał:
> You don't need RichFunctions for that, you should be able to just do:
>
> private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
>
> On 27.08.2018 10:28, Dominik Wosiński wrote:
>
> Hey Paul,
> Yeah that is possible, but I was asking in terms of serialization schema.
> So I would really want to avoid RichFunction :)
>
> Best Regards,
> Dominik.
>
> pon., 27 sie 2018 o 10:23 Chesnay Schepler <ch...@apache.org>
> napisał(a):
>
>> The null check in the method is the general-purpose way of solving it.
>> If the ObjectMapper is thread-safe you could also initialize it as a
>> static field.
>>
>> On 26.08.2018 17:58, Dominik Wosiński wrote:
>>
>> Hey,
>>
>> I was wondering how do You normally deal with fields that contain
>> references that are not serializable. Say, we have a custom serialization
>> schema in Java that needs to serialize *LocalDateTime* field with
>> *ObjectMapper.* This requires registering specific module for
>> *ObjectMapper* and this makes it not serializable (module contains some
>> references to classes that are not serializable).
>> Now, if You would initialize *ObjectMapper *directly in the field this
>> will cause an exception when deploying the job.
>>
>> Normally I would do :
>>
>> @Overridepublic byte[] serialize(Backup backupMessage) {
>> if(objectMapper == null) {
>> objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); }
>> ...
>> }
>>
>> But I was wondering whether do You have any prettier option of doing
>> this?
>>
>> Thanks,
>> Dominik.
>>
>>
>>
>
Re: Dealing with Not Serializable classes in Java
Posted by Chesnay Schepler <ch...@apache.org>.
You don't need RichFunctions for that, you should be able to just do:
private static final ObjectMapper objectMapper =
new ObjectMapper().registerModule(new JavaTimeModule());
On 27.08.2018 10:28, Dominik Wosiński wrote:
> Hey Paul,
> Yeah that is possible, but I was asking in terms of serialization
> schema. So I would really want to avoid RichFunction :)
>
> Best Regards,
> Dominik.
>
> pon., 27 sie 2018 o 10:23 Chesnay Schepler <chesnay@apache.org
> <ma...@apache.org>> napisał(a):
>
> The null check in the method is the general-purpose way of solving it.
> If the ObjectMapper is thread-safe you could also initialize it as
> a static field.
>
> On 26.08.2018 17:58, Dominik Wosiński wrote:
>> Hey,
>>
>> I was wondering how do You normally deal with fields that contain
>> references that are not serializable. Say, we have a custom
>> serialization schema in Java that needs to serialize
>> /LocalDateTime/ field with /ObjectMapper./ This requires
>> registering specific module for /ObjectMapper/ and this makes it
>> not serializable (module contains some references to classes that
>> are not serializable).
>> Now, if You would initialize /ObjectMapper /directly in the field
>> this will cause an exception when deploying the job.
>>
>> Normally I would do :
>> @Override public byte[]serialize(Backup backupMessage) {
>> if(objectMapper ==null) {
>> objectMapper =new ObjectMapper().registerModule(new JavaTimeModule()); }
>> ...
>> }
>> But I was wondering whether do You have any prettier option of
>> doing this?
>>
>> Thanks,
>> Dominik.
>
>
Re: Dealing with Not Serializable classes in Java
Posted by Dominik Wosiński <wo...@gmail.com>.
Hey Paul,
Yeah that is possible, but I was asking in terms of serialization schema.
So I would really want to avoid RichFunction :)
Best Regards,
Dominik.
pon., 27 sie 2018 o 10:23 Chesnay Schepler <ch...@apache.org> napisał(a):
> The null check in the method is the general-purpose way of solving it.
> If the ObjectMapper is thread-safe you could also initialize it as a
> static field.
>
> On 26.08.2018 17:58, Dominik Wosiński wrote:
>
> Hey,
>
> I was wondering how do You normally deal with fields that contain
> references that are not serializable. Say, we have a custom serialization
> schema in Java that needs to serialize *LocalDateTime* field with
> *ObjectMapper.* This requires registering specific module for
> *ObjectMapper* and this makes it not serializable (module contains some
> references to classes that are not serializable).
> Now, if You would initialize *ObjectMapper *directly in the field this
> will cause an exception when deploying the job.
>
> Normally I would do :
>
> @Overridepublic byte[] serialize(Backup backupMessage) {
> if(objectMapper == null) {
> objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); }
> ...
> }
>
> But I was wondering whether do You have any prettier option of doing this?
>
> Thanks,
> Dominik.
>
>
>
Re: Dealing with Not Serializable classes in Java
Posted by Chesnay Schepler <ch...@apache.org>.
The null check in the method is the general-purpose way of solving it.
If the ObjectMapper is thread-safe you could also initialize it as a
static field.
On 26.08.2018 17:58, Dominik Wosiński wrote:
> Hey,
>
> I was wondering how do You normally deal with fields that contain
> references that are not serializable. Say, we have a custom
> serialization schema in Java that needs to serialize
> /LocalDateTime/ field with /ObjectMapper./ This requires registering
> specific module for /ObjectMapper/ and this makes it not serializable
> (module contains some references to classes that are not serializable).
> Now, if You would initialize /ObjectMapper /directly in the field this
> will cause an exception when deploying the job.
>
> Normally I would do :
> @Override public byte[]serialize(Backup backupMessage) {
> if(objectMapper ==null) {
> objectMapper =new ObjectMapper().registerModule(new JavaTimeModule()); }
> ...
> }
> But I was wondering whether do You have any prettier option of doing
> this?
>
> Thanks,
> Dominik.