Search in sources :

Example 1 with RecordBinManager

use of org.apache.nifi.processors.standard.merge.RecordBinManager in project nifi by apache.

the class MergeRecord method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    RecordBinManager manager = binManager.get();
    while (manager == null) {
        manager = new RecordBinManager(context, sessionFactory, getLogger());
        manager.setMaxBinAge(context.getProperty(MAX_BIN_AGE).asTimePeriod(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
        final boolean updated = binManager.compareAndSet(null, manager);
        if (!updated) {
            manager = binManager.get();
        }
    }
    final ProcessSession session = sessionFactory.createSession();
    final List<FlowFile> flowFiles = session.get(FlowFileFilters.newSizeBasedFilter(250, DataUnit.KB, 250));
    if (getLogger().isDebugEnabled()) {
        final List<String> ids = flowFiles.stream().map(ff -> "id=" + ff.getId()).collect(Collectors.toList());
        getLogger().debug("Pulled {} FlowFiles from queue: {}", new Object[] { ids.size(), ids });
    }
    final String mergeStrategy = context.getProperty(MERGE_STRATEGY).getValue();
    final boolean block;
    if (MERGE_STRATEGY_DEFRAGMENT.equals(mergeStrategy)) {
        block = true;
    } else if (context.getProperty(CORRELATION_ATTRIBUTE_NAME).isSet()) {
        block = true;
    } else {
        block = false;
    }
    try {
        for (final FlowFile flowFile : flowFiles) {
            try {
                binFlowFile(context, flowFile, session, manager, block);
            } catch (final Exception e) {
                getLogger().error("Failed to bin {} due to {}", new Object[] { flowFile, e });
                session.transfer(flowFile, REL_FAILURE);
            }
        }
    } finally {
        session.commit();
    }
    try {
        manager.completeExpiredBins();
    } catch (final Exception e) {
        getLogger().error("Failed to merge FlowFiles to create new bin due to " + e, e);
    }
    if (flowFiles.isEmpty()) {
        getLogger().debug("No FlowFiles to bin; will yield");
        context.yield();
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) StandardValidators(org.apache.nifi.processor.util.StandardValidators) AttributeStrategyUtil(org.apache.nifi.processors.standard.merge.AttributeStrategyUtil) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) RecordReader(org.apache.nifi.serialization.RecordReader) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ReadsAttributes(org.apache.nifi.annotation.behavior.ReadsAttributes) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) TriggerWhenEmpty(org.apache.nifi.annotation.behavior.TriggerWhenEmpty) AvroTypeUtil(org.apache.nifi.avro.AvroTypeUtil) FlowFile(org.apache.nifi.flowfile.FlowFile) FragmentAttributes(org.apache.nifi.flowfile.attributes.FragmentAttributes) RecordBinManager(org.apache.nifi.processors.standard.merge.RecordBinManager) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) RecordSetWriterFactory(org.apache.nifi.serialization.RecordSetWriterFactory) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) AllowableValue(org.apache.nifi.components.AllowableValue) Collectors(java.util.stream.Collectors) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) FlowFileFilters(org.apache.nifi.processor.util.FlowFileFilters) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) RecordReaderFactory(org.apache.nifi.serialization.RecordReaderFactory) Optional(java.util.Optional) Tags(org.apache.nifi.annotation.documentation.Tags) DataUnit(org.apache.nifi.processor.DataUnit) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) InputStream(java.io.InputStream) ReadsAttribute(org.apache.nifi.annotation.behavior.ReadsAttribute) FlowFile(org.apache.nifi.flowfile.FlowFile) RecordBinManager(org.apache.nifi.processors.standard.merge.RecordBinManager) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) IOException(java.io.IOException)

Example 2 with RecordBinManager

use of org.apache.nifi.processors.standard.merge.RecordBinManager in project nifi by apache.

the class MergeRecord method resetState.

@OnStopped
public final void resetState() {
    final RecordBinManager manager = binManager.get();
    if (manager != null) {
        manager.purge();
    }
    binManager.set(null);
}
Also used : RecordBinManager(org.apache.nifi.processors.standard.merge.RecordBinManager) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped)

Aggregations

OnStopped (org.apache.nifi.annotation.lifecycle.OnStopped)2 RecordBinManager (org.apache.nifi.processors.standard.merge.RecordBinManager)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)1 Requirement (org.apache.nifi.annotation.behavior.InputRequirement.Requirement)1 ReadsAttribute (org.apache.nifi.annotation.behavior.ReadsAttribute)1 ReadsAttributes (org.apache.nifi.annotation.behavior.ReadsAttributes)1 SideEffectFree (org.apache.nifi.annotation.behavior.SideEffectFree)1 TriggerWhenEmpty (org.apache.nifi.annotation.behavior.TriggerWhenEmpty)1 WritesAttribute (org.apache.nifi.annotation.behavior.WritesAttribute)1 WritesAttributes (org.apache.nifi.annotation.behavior.WritesAttributes)1