Storm初始化报错java.io.NotSerializableException

来源:互联网 发布:电视淘宝 编辑:程序博客网 时间:2024/05/02 02:43

最近在做实时数据分析,使用到了twitter开源的storm,在初始化的时候报了一个序列化的错:

java.lang.RuntimeException: java.io.NotSerializableException: org.joda.time.format.DateTimeFormatter

报错信息很明显,因为DateTimeFormatter不支持序列化。但是我只是在bolt里使用到了这个时间类,不至于在初始化的时候就报错啊?


上网搜了下原因,是因为storm工作机制的问题。在你启动一个topology以后,supervisor会初始化这个bolt,并发送到worker,然后再调用bolt的prepare()方法。OK,从这里就能看出来发送到bolt这一步涉及到了序列化,因此会报错。


解决的办法就是,从supervisor发送到bolt这一步并不是初始化DateTimeFormatter类,可以先声明相关的对象,然后等发送到worker了以后,在bolt的prepare()里进行实例化,就可以避免此问题。下面是google来的一段话:


The supervisor instantiates the bolts, sends them to the workers, and then calls prepare() on all of them. Therefore, anything that isn't serializable that is instantiated before prepare() causes this process to fail.

原创粉丝点击