Exemple #1
0
  void doProcess(Exchange exchange) throws Exception {
    Object body = exchange.getIn().getBody();
    Object key = exchange.getIn().getHeader(HdfsHeader.KEY.name());

    // must have ostream
    if (ostream == null) {
      ostream = setupHdfs(false);
    }

    boolean split = false;
    List<SplitStrategy> strategies = config.getSplitStrategies();
    for (SplitStrategy splitStrategy : strategies) {
      split |= splitStrategy.getType().split(ostream, splitStrategy.value, this);
    }

    if (split) {
      if (ostream != null) {
        IOHelper.close(ostream, "output stream", log);
      }
      StringBuilder actualPath = newFileName();
      ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
    }

    String path = ostream.getActualPath();
    log.trace("Writing body to hdfs-file {}", path);
    ostream.append(key, body, exchange.getContext().getTypeConverter());

    idle.set(false);

    // close if we do not have idle checker task to do this for us
    boolean close = scheduler == null;
    // but user may have a header to explict control the close
    Boolean closeHeader = exchange.getIn().getHeader(HdfsConstants.HDFS_CLOSE, Boolean.class);
    if (closeHeader != null) {
      close = closeHeader;
    }

    // if no idle checker then we need to explicit close the stream after usage
    if (close) {
      try {
        HdfsProducer.this.log.trace("Closing stream");
        ostream.close();
        ostream = null;
      } catch (IOException e) {
        // ignore
      }
    }

    log.debug("Wrote body to hdfs-file {}", path);
  }