簡體   English   中英

java.io.NotSerializableException:org.apache.storm.spout.SpoutOutputCollector

[英]java.io.NotSerializableException: org.apache.storm.spout.SpoutOutputCollector

我有一個spout發出一些查詢,但是出現了SpoutOutputCollector無法序列化的錯誤。 這似乎是不可能的。 異常消息是:

java.lang.RuntimeException:java.lang.RuntimeException:java.io.NotSerializableException:org.apache.storm.spout.SpoutOutputCollector

我嘗試在發出之前記錄日志,並且可以獲取日志。 但是,該任務將在日志消息后不久終止。 這是我的代碼。

    package tiger.server.queryreceiver;

    import static tiger.util.Util.QUERY_STREAM;

    import com.typesafe.config.Config;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import org.apache.storm.spout.SpoutOutputCollector;
    import org.apache.storm.task.TopologyContext;
    import org.apache.storm.topology.OutputFieldsDeclarer;
    import org.apache.storm.topology.base.BaseRichSpout;
    import org.apache.storm.tuple.Fields;
    import org.apache.storm.tuple.Values;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import tiger.graph.event.EventTemplate;
    import tiger.message.QueryMessage;
    import tiger.query.Query;

    public abstract class QueryReceiver extends BaseRichSpout {
      public static final Logger logger = LoggerFactory.getLogger(QueryReceiver.class);
      private static final long serialVersionUID = 5169597660682854052L;
      private final EventTemplate template;
      private final int queryInterval;
      protected SpoutOutputCollector collector;

      public QueryReceiver(EventTemplate template, int queryInterval) {
        this.template = template;
        this.queryInterval = queryInterval;
      }

      public static QueryReceiver fromConfig(Config config, EventTemplate template) {
        //this method will reture the FinalQueryReceiver based on the config
      }

      @Override
      public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
        this.collector = collector;
      }

      @Override
      public void nextTuple() {
        List<Query> queries = fetchQueries();
        if (queries != null) {
          logger.debug("*** emit queries {}", queries);
          collector.emit(QUERY_STREAM, new Values(new QueryMessage(queries)));
        }
        try {
          TimeUnit.MILLISECONDS.sleep(queryInterval);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      public abstract List<Query> fetchQueries();
    }

和實現類

    package tiger.server.queryreceiver;

    import java.util.Arrays;
    import java.util.List;
    import tiger.graph.event.Event;
    import tiger.graph.event.EventTemplate;
    import tiger.query.Condition;
    import tiger.query.Condition.AlwaysTrueMatcher;
    import tiger.query.Condition.SeqMatcher;
    import tiger.query.LastEventValueOp;
    import tiger.query.NFABuilder;
    import tiger.query.Nfa;
    import tiger.query.Query;

    public class FinalQueryReceiver extends QueryReceiver {

      private static final long serialVersionUID = -4372885166586608338L;
      private long start;
      private final long length;
      private final long slide;
      private int numSlide;

      public FinalQueryReceiver(EventTemplate template, int queryInterval, long start, long length) {
        super(template,queryInterval);
        this.start = start;
        this.length = length;
        this.slide = 0;
        this.numSlide = 1;
      }

      public FinalQueryReceiver(EventTemplate template, int queryInterval, long start, long length, long slide, int numSlide) {
        super(template, queryInterval);
        this.start = start;
        this.length = length;
        this.slide = slide;
        this.numSlide = numSlide;
      }

      @Override
      public List<Query> fetchQueries() {
        if(numSlide > 0 || numSlide < 0) {
          Nfa nfa = new NFABuilder()
              .start(new SeqMatcher() {
                @Override
                public boolean apply(List<Event> seq, Event event) {
                  return true;
                }
              }).op("equal", new LastEventValueOp())
              .followedByKleeneEnd(Condition.alwaysTrueMatcher(), Condition.alwaysTrueMatcher())
              .op("equal", new LastEventValueOp())
              .build();

          Query query = new Query(nfa, start, start + length);
          start += slide;
          if(numSlide > 0) {
            numSlide = numSlide - 1;
          }
          return Arrays.asList(query);
        }else {
          return null;
        }
      }
    }

我們可以獲取日志“發出查詢”,但是,工作人員將在日志之后立即死亡

一些發現之后進行更新:我在本地模式下運行該項目,並嘗試一個一個地發出queryMessage的屬性,最后,當我發出實例狀態時,我發現了同樣的問題。 狀態代碼如下:

    public abstract class State implements Serializable {

      public Condition.SeqMatcher self;
      public Condition.SeqMatcher next;
      public String op;
      public SeqOperator extractor;
      // the rest are all methods and the subclass will only override the methods. the self, next, op, and extractor attributions are all a interface which extend the Serializable.
   }

此外,我對java ObjectOutputStream對QueryMessage進行序列化做了一些測試,沒有發現異常。 因此,我認為這可能不是序列化問題,但是真正的問題是什么?

不能序列化的不是SpoutOutputCollector。 SpoutOutputCollector無法序列化您的查詢。

您沒有提供整個堆棧跟蹤,所以不能確定,但​​是我的猜測是QueryMessageQuery沒有實現Serializable接口。

您可以在這里找到Java序列化的不錯介紹: https : //www.baeldung.com/java-serialization

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM