Search in sources :

Example 11 with ProcessContext

use of org.apache.nifi.processor.ProcessContext 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 12 with ProcessContext

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

the class BinFiles method binFlowFiles.

private int binFlowFiles(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    int flowFilesBinned = 0;
    while (binManager.getBinCount() <= context.getProperty(MAX_BIN_COUNT).asInteger()) {
        if (!isScheduled()) {
            break;
        }
        final ProcessSession session = sessionFactory.createSession();
        final List<FlowFile> flowFiles = session.get(1000);
        if (flowFiles.isEmpty()) {
            break;
        }
        final Map<String, List<FlowFile>> flowFileGroups = new HashMap<>();
        for (FlowFile flowFile : flowFiles) {
            flowFile = this.preprocessFlowFile(context, session, flowFile);
            try {
                final String groupingIdentifier = getGroupId(context, flowFile, session);
                flowFileGroups.computeIfAbsent(groupingIdentifier, id -> new ArrayList<>()).add(flowFile);
            } catch (final Exception e) {
                getLogger().error("Could not determine which Bin to add {} to; will route to failure", new Object[] { flowFile }, e);
                session.transfer(flowFile, REL_FAILURE);
                continue;
            }
        }
        for (final Map.Entry<String, List<FlowFile>> entry : flowFileGroups.entrySet()) {
            final Set<FlowFile> unbinned = binManager.offer(entry.getKey(), entry.getValue(), session, sessionFactory);
            for (final FlowFile flowFile : unbinned) {
                Bin bin = new Bin(sessionFactory.createSession(), 0, Long.MAX_VALUE, 0, Integer.MAX_VALUE, null);
                bin.offer(flowFile, session);
                this.readyBins.add(bin);
            }
            flowFilesBinned += entry.getValue().size();
        }
    }
    return flowFilesBinned;
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) StandardValidators(org.apache.nifi.processor.util.StandardValidators) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) ValidationResult(org.apache.nifi.components.ValidationResult) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) Queue(java.util.Queue) DataUnit(org.apache.nifi.processor.DataUnit) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with ProcessContext

use of org.apache.nifi.processor.ProcessContext 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 14 with ProcessContext

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

the class ITConsumeKafka method validateGetAllMessagesPattern.

@Test
public void validateGetAllMessagesPattern() throws Exception {
    String groupName = "validateGetAllMessagesPattern";
    when(mockConsumerPool.obtainConsumer(anyObject(), anyObject())).thenReturn(mockLease);
    when(mockLease.continuePolling()).thenReturn(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE);
    when(mockLease.commit()).thenReturn(Boolean.TRUE);
    ConsumeKafka_1_0 proc = new ConsumeKafka_1_0() {

        @Override
        protected ConsumerPool createConsumerPool(final ProcessContext context, final ComponentLog log) {
            return mockConsumerPool;
        }
    };
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(KafkaProcessorUtils.BOOTSTRAP_SERVERS, "0.0.0.0:1234");
    runner.setProperty(ConsumeKafka_1_0.TOPICS, "(fo.*)|(ba)");
    runner.setProperty(ConsumeKafka_1_0.TOPIC_TYPE, "pattern");
    runner.setProperty(ConsumeKafka_1_0.GROUP_ID, groupName);
    runner.setProperty(ConsumeKafka_1_0.AUTO_OFFSET_RESET, ConsumeKafka_1_0.OFFSET_EARLIEST);
    runner.run(1, false);
    verify(mockConsumerPool, times(1)).obtainConsumer(anyObject(), anyObject());
    verify(mockLease, times(3)).continuePolling();
    verify(mockLease, times(2)).poll();
    verify(mockLease, times(1)).commit();
    verify(mockLease, times(1)).close();
    verifyNoMoreInteractions(mockConsumerPool);
    verifyNoMoreInteractions(mockLease);
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 15 with ProcessContext

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

the class ITConsumeKafka method validateGetErrorMessages.

@Test
public void validateGetErrorMessages() throws Exception {
    String groupName = "validateGetErrorMessages";
    when(mockConsumerPool.obtainConsumer(anyObject(), anyObject())).thenReturn(mockLease);
    when(mockLease.continuePolling()).thenReturn(true, false);
    when(mockLease.commit()).thenReturn(Boolean.FALSE);
    ConsumeKafka_1_0 proc = new ConsumeKafka_1_0() {

        @Override
        protected ConsumerPool createConsumerPool(final ProcessContext context, final ComponentLog log) {
            return mockConsumerPool;
        }
    };
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(KafkaProcessorUtils.BOOTSTRAP_SERVERS, "0.0.0.0:1234");
    runner.setProperty(ConsumeKafka_1_0.TOPICS, "foo,bar");
    runner.setProperty(ConsumeKafka_1_0.GROUP_ID, groupName);
    runner.setProperty(ConsumeKafka_1_0.AUTO_OFFSET_RESET, ConsumeKafka_1_0.OFFSET_EARLIEST);
    runner.run(1, false);
    verify(mockConsumerPool, times(1)).obtainConsumer(anyObject(), anyObject());
    verify(mockLease, times(2)).continuePolling();
    verify(mockLease, times(1)).poll();
    verify(mockLease, times(1)).commit();
    verify(mockLease, times(1)).close();
    verifyNoMoreInteractions(mockConsumerPool);
    verifyNoMoreInteractions(mockLease);
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Aggregations

ProcessContext (org.apache.nifi.processor.ProcessContext)115 Test (org.junit.Test)67 TestRunner (org.apache.nifi.util.TestRunner)56 ProcessSession (org.apache.nifi.processor.ProcessSession)49 FlowFile (org.apache.nifi.flowfile.FlowFile)40 MockFlowFile (org.apache.nifi.util.MockFlowFile)39 HashSet (java.util.HashSet)35 Relationship (org.apache.nifi.processor.Relationship)35 List (java.util.List)34 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)34 ArrayList (java.util.ArrayList)33 Set (java.util.Set)33 Tags (org.apache.nifi.annotation.documentation.Tags)31 IOException (java.io.IOException)30 HashMap (java.util.HashMap)30 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)30 ProcessException (org.apache.nifi.processor.exception.ProcessException)30 Collections (java.util.Collections)29 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)29 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)29