Search in sources :

Example 16 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class ConsumeAMQP method processResource.

/**
 * Will construct a {@link FlowFile} containing the body of the consumed AMQP message (if {@link GetResponse} returned by {@link AMQPConsumer} is
 * not null) and AMQP properties that came with message which are added to a {@link FlowFile} as attributes, transferring {@link FlowFile} to
 * 'success' {@link Relationship}.
 */
@Override
protected void processResource(final Connection connection, final AMQPConsumer consumer, final ProcessContext context, final ProcessSession session) {
    final GetResponse response = consumer.consume();
    if (response == null) {
        context.yield();
        return;
    }
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, out -> out.write(response.getBody()));
    final BasicProperties amqpProperties = response.getProps();
    final Map<String, String> attributes = buildAttributes(amqpProperties);
    flowFile = session.putAllAttributes(flowFile, attributes);
    session.getProvenanceReporter().receive(flowFile, connection.toString() + "/" + context.getProperty(QUEUE).getValue());
    session.transfer(flowFile, REL_SUCCESS);
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) HashMap(java.util.HashMap) ProcessSession(org.apache.nifi.processor.ProcessSession) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) Connection(com.rabbitmq.client.Connection) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) GetResponse(com.rabbitmq.client.GetResponse) FlowFile(org.apache.nifi.flowfile.FlowFile) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) GetResponse(com.rabbitmq.client.GetResponse)

Example 17 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class TestExtractMediaMetadata method testRelationships.

@Test
public void testRelationships() {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata());
    ProcessContext context = runner.getProcessContext();
    Set<Relationship> relationships = context.getAvailableRelationships();
    assertEquals(2, relationships.size());
    assertTrue(relationships.contains(ExtractMediaMetadata.SUCCESS));
    assertTrue(relationships.contains(ExtractMediaMetadata.FAILURE));
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 18 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class RouteOnAttribute method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final Map<Relationship, PropertyValue> propMap = this.propertyMap;
    final Set<Relationship> matchingRelationships = new HashSet<>();
    for (final Map.Entry<Relationship, PropertyValue> entry : propMap.entrySet()) {
        final PropertyValue value = entry.getValue();
        final boolean matches = value.evaluateAttributeExpressions(flowFile).asBoolean();
        if (matches) {
            matchingRelationships.add(entry.getKey());
        }
    }
    final Set<Relationship> destinationRelationships = new HashSet<>();
    switch(context.getProperty(ROUTE_STRATEGY).getValue()) {
        case routeAllMatchValue:
            if (matchingRelationships.size() == propMap.size()) {
                destinationRelationships.add(REL_MATCH);
            } else {
                destinationRelationships.add(REL_NO_MATCH);
            }
            break;
        case routeAnyMatches:
            if (matchingRelationships.isEmpty()) {
                destinationRelationships.add(REL_NO_MATCH);
            } else {
                destinationRelationships.add(REL_MATCH);
            }
            break;
        case routePropertyNameValue:
        default:
            destinationRelationships.addAll(matchingRelationships);
            break;
    }
    if (destinationRelationships.isEmpty()) {
        logger.info("Routing {} to unmatched", new Object[] { flowFile });
        flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, REL_NO_MATCH.getName());
        session.getProvenanceReporter().route(flowFile, REL_NO_MATCH);
        session.transfer(flowFile, REL_NO_MATCH);
    } else {
        final Iterator<Relationship> relationshipNameIterator = destinationRelationships.iterator();
        final Relationship firstRelationship = relationshipNameIterator.next();
        final Map<Relationship, FlowFile> transferMap = new HashMap<>();
        final Set<FlowFile> clones = new HashSet<>();
        // make all the clones for any remaining relationships
        while (relationshipNameIterator.hasNext()) {
            final Relationship relationship = relationshipNameIterator.next();
            final FlowFile cloneFlowFile = session.clone(flowFile);
            clones.add(cloneFlowFile);
            transferMap.put(relationship, cloneFlowFile);
        }
        // now transfer any clones generated
        for (final Map.Entry<Relationship, FlowFile> entry : transferMap.entrySet()) {
            logger.info("Cloned {} into {} and routing clone to relationship {}", new Object[] { flowFile, entry.getValue(), entry.getKey() });
            FlowFile updatedFlowFile = session.putAttribute(entry.getValue(), ROUTE_ATTRIBUTE_KEY, entry.getKey().getName());
            session.getProvenanceReporter().route(updatedFlowFile, entry.getKey());
            session.transfer(updatedFlowFile, entry.getKey());
        }
        // now transfer the original flow file
        logger.info("Routing {} to {}", new Object[] { flowFile, firstRelationship });
        session.getProvenanceReporter().route(flowFile, firstRelationship);
        flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, firstRelationship.getName());
        session.transfer(flowFile, firstRelationship);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 19 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class RouteOnContent method onPropertyModified.

@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
    if (descriptor.isDynamic()) {
        final Set<Relationship> relationships = new HashSet<>(this.relationships.get());
        final Relationship relationship = new Relationship.Builder().name(descriptor.getName()).build();
        if (newValue == null) {
            relationships.remove(relationship);
        } else {
            relationships.add(relationship);
        }
        this.relationships.set(relationships);
    }
}
Also used : Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) HashSet(java.util.HashSet)

Example 20 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class RouteText method onScheduled.

/**
 * When this processor is scheduled, update the dynamic properties into the map
 * for quick access during each onTrigger call
 *
 * @param context ProcessContext used to retrieve dynamic properties
 */
@OnScheduled
public void onScheduled(final ProcessContext context) {
    final String regex = context.getProperty(GROUPING_REGEX).getValue();
    if (regex != null) {
        groupingRegex = Pattern.compile(regex);
    }
    final Map<Relationship, PropertyValue> newPropertyMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        getLogger().debug("Adding new dynamic property: {}", new Object[] { descriptor });
        newPropertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor));
    }
    this.propertyMap = newPropertyMap;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) PropertyValue(org.apache.nifi.components.PropertyValue) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

Relationship (org.apache.nifi.processor.Relationship)106 ArrayList (java.util.ArrayList)41 HashSet (java.util.HashSet)40 HashMap (java.util.HashMap)32 FlowFile (org.apache.nifi.flowfile.FlowFile)32 Map (java.util.Map)31 IOException (java.io.IOException)26 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)26 Test (org.junit.Test)23 List (java.util.List)20 Set (java.util.Set)19 Connection (org.apache.nifi.connectable.Connection)18 TestRunner (org.apache.nifi.util.TestRunner)18 ProcessException (org.apache.nifi.processor.exception.ProcessException)17 ProcessSession (org.apache.nifi.processor.ProcessSession)15 InputStream (java.io.InputStream)14 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)12 Processor (org.apache.nifi.processor.Processor)12 Collections (java.util.Collections)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)10