Search in sources :

Example 1 with ByteCountingOutputStream

use of org.apache.nifi.stream.io.ByteCountingOutputStream in project nifi by apache.

the class CompressableRecordWriter method resetWriteStream.

/**
 * Resets the streams to prepare for a new block
 *
 * @param eventId the first id that will be written to the new block
 * @throws IOException if unable to flush/close the current streams properly
 */
protected void resetWriteStream(final Long eventId) throws IOException {
    try {
        if (out != null) {
            out.flush();
        }
        final long byteOffset = (byteCountingOut == null) ? rawOutStream.getBytesWritten() : byteCountingOut.getBytesWritten();
        final TocWriter tocWriter = getTocWriter();
        if (compressed) {
            // We don't have to check if the writer is dirty because we will have already checked before calling this method.
            if (out != null) {
                out.close();
            }
            if (tocWriter != null && eventId != null) {
                tocWriter.addBlockOffset(rawOutStream.getBytesWritten(), eventId);
            }
            final OutputStream writableStream = new BufferedOutputStream(new GZIPOutputStream(new NonCloseableOutputStream(rawOutStream), 1), 65536);
            this.byteCountingOut = new ByteCountingOutputStream(writableStream, byteOffset);
        } else {
            if (tocWriter != null && eventId != null) {
                tocWriter.addBlockOffset(rawOutStream.getBytesWritten(), eventId);
            }
            this.byteCountingOut = rawOutStream;
        }
        this.out = new DataOutputStream(byteCountingOut);
        resetDirtyFlag();
    } catch (final IOException ioe) {
        markDirty();
        throw ioe;
    }
}
Also used : GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) TocWriter(org.apache.nifi.provenance.toc.TocWriter) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream) DataOutputStream(java.io.DataOutputStream) GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) IOException(java.io.IOException) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream)

Example 2 with ByteCountingOutputStream

use of org.apache.nifi.stream.io.ByteCountingOutputStream in project nifi by apache.

the class EventFileCompressor method compress.

public static void compress(final File input, final TocReader tocReader, final File output, final TocWriter tocWriter) throws IOException {
    try (final InputStream fis = new FileInputStream(input);
        final OutputStream fos = new FileOutputStream(output);
        final ByteCountingOutputStream byteCountingOut = new ByteCountingOutputStream(fos)) {
        int blockIndex = 0;
        while (true) {
            // Determine the min and max byte ranges for the current block.
            final long blockStart = tocReader.getBlockOffset(blockIndex);
            if (blockStart == -1) {
                break;
            }
            long blockEnd = tocReader.getBlockOffset(blockIndex + 1);
            if (blockEnd < 0) {
                blockEnd = input.length();
            }
            final long firstEventId = tocReader.getFirstEventIdForBlock(blockIndex);
            final long blockStartOffset = byteCountingOut.getBytesWritten();
            try (final OutputStream ncos = new NonCloseableOutputStream(byteCountingOut);
                final OutputStream gzipOut = new GZIPOutputStream(ncos, 1)) {
                StreamUtils.copy(fis, gzipOut, blockEnd - blockStart);
            }
            tocWriter.addBlockOffset(blockStartOffset, firstEventId);
            blockIndex++;
        }
    }
    // Close the TOC Reader and TOC Writer
    CloseableUtil.closeQuietly(tocReader, tocWriter);
}
Also used : GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream) GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) FileInputStream(java.io.FileInputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream)

Example 3 with ByteCountingOutputStream

use of org.apache.nifi.stream.io.ByteCountingOutputStream in project nifi by apache.

the class RecordBin method offer.

public boolean offer(final FlowFile flowFile, final RecordReader recordReader, final ProcessSession flowFileSession, final boolean block) throws IOException, MalformedRecordException, SchemaNotFoundException {
    if (isComplete()) {
        logger.debug("RecordBin.offer for id={} returning false because {} is complete", new Object[] { flowFile.getId(), this });
        return false;
    }
    final boolean locked;
    if (block) {
        writeLock.lock();
        locked = true;
    } else {
        locked = writeLock.tryLock();
    }
    if (!locked) {
        logger.debug("RecordBin.offer for id={} returning false because failed to get lock for {}", new Object[] { flowFile.getId(), this });
        return false;
    }
    boolean flowFileMigrated = false;
    try {
        if (isComplete()) {
            logger.debug("RecordBin.offer for id={} returning false because {} is complete", new Object[] { flowFile.getId(), this });
            return false;
        }
        logger.debug("Migrating id={} to {}", new Object[] { flowFile.getId(), this });
        Record record;
        while ((record = recordReader.nextRecord()) != null) {
            if (recordWriter == null) {
                final OutputStream rawOut = session.write(merged);
                logger.debug("Created OutputStream using session {} for {}", new Object[] { session, this });
                this.out = new ByteCountingOutputStream(rawOut);
                recordWriter = writerFactory.createWriter(logger, record.getSchema(), out);
                recordWriter.beginRecordSet();
            }
            recordWriter.write(record);
            recordCount++;
        }
        // This will be closed by the MergeRecord class anyway but we have to close it
        // here because it needs to be closed before we are able to migrate the FlowFile
        // to a new Session.
        recordReader.close();
        flowFileSession.migrate(this.session, Collections.singleton(flowFile));
        flowFileMigrated = true;
        this.flowFiles.add(flowFile);
        if (isFull()) {
            logger.debug(this + " is now full. Completing bin.");
            complete("Bin is full");
        } else if (isOlderThan(thresholds.getMaxBinMillis(), TimeUnit.MILLISECONDS)) {
            logger.debug(this + " is now expired. Completing bin.");
            complete("Bin is older than " + thresholds.getMaxBinAge());
        }
        return true;
    } catch (final Exception e) {
        logger.error("Failed to create merged FlowFile from " + (flowFiles.size() + 1) + " input FlowFiles; routing originals to failure", e);
        try {
            // This will be closed by the MergeRecord class anyway but we have to close it
            // here because it needs to be closed before we are able to migrate the FlowFile
            // to a new Session.
            recordReader.close();
            if (recordWriter != null) {
                recordWriter.close();
            }
            if (this.out != null) {
                this.out.close();
            }
            if (!flowFileMigrated) {
                flowFileSession.migrate(this.session, Collections.singleton(flowFile));
                this.flowFiles.add(flowFile);
            }
        } finally {
            complete = true;
            session.remove(merged);
            session.transfer(flowFiles, MergeRecord.REL_FAILURE);
            session.commit();
        }
        return true;
    } finally {
        writeLock.unlock();
    }
}
Also used : ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) OutputStream(java.io.OutputStream) Record(org.apache.nifi.serialization.record.Record) MergeRecord(org.apache.nifi.processors.standard.MergeRecord) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) IOException(java.io.IOException)

Example 4 with ByteCountingOutputStream

use of org.apache.nifi.stream.io.ByteCountingOutputStream in project nifi by apache.

the class HttpOutput method setOutputStream.

public void setOutputStream(OutputStream outputStream) {
    interruptableOut = new InterruptableOutputStream(outputStream);
    this.countingOut = new ByteCountingOutputStream(interruptableOut);
}
Also used : InterruptableOutputStream(org.apache.nifi.remote.io.InterruptableOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream)

Example 5 with ByteCountingOutputStream

use of org.apache.nifi.stream.io.ByteCountingOutputStream in project nifi by apache.

the class OrcFlowFileWriter method getStream.

@VisibleForTesting
public OutputStream getStream() throws IOException {
    if (rawWriter == null) {
        rawWriter = new ByteCountingOutputStream(flowFileOutputStream);
        rawWriter.write(OrcFile.MAGIC.getBytes());
        headerLength = rawWriter.getBytesWritten();
        writer = new OutStream("metadata", bufferSize, codec, new DirectStream(rawWriter));
        protobufWriter = CodedOutputStream.newInstance(writer);
    }
    return rawWriter;
}
Also used : ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ByteCountingOutputStream (org.apache.nifi.stream.io.ByteCountingOutputStream)14 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)9 BufferedOutputStream (java.io.BufferedOutputStream)6 ContentClaim (org.apache.nifi.controller.repository.claim.ContentClaim)5 DisableOnCloseOutputStream (org.apache.nifi.controller.repository.io.DisableOnCloseOutputStream)5 FlowFileAccessOutputStream (org.apache.nifi.controller.repository.io.FlowFileAccessOutputStream)5 TaskTerminationOutputStream (org.apache.nifi.controller.repository.io.TaskTerminationOutputStream)5 ProcessException (org.apache.nifi.processor.exception.ProcessException)5 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 FlowFileAccessException (org.apache.nifi.processor.exception.FlowFileAccessException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SynchronizedByteCountingOutputStream (org.apache.nifi.stream.io.SynchronizedByteCountingOutputStream)3 File (java.io.File)2 Path (java.nio.file.Path)2 StandardContentClaim (org.apache.nifi.controller.repository.claim.StandardContentClaim)2 DisableOnCloseInputStream (org.apache.nifi.controller.repository.io.DisableOnCloseInputStream)2 FlowFileAccessInputStream (org.apache.nifi.controller.repository.io.FlowFileAccessInputStream)2 LimitedInputStream (org.apache.nifi.controller.repository.io.LimitedInputStream)2