Search in sources :

Example 71 with ProcessSession

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

the class AbstractFlumeProcessor method onTrigger.

/*
     * Borrowed from AbstractProcessor. The FlumeSourceProcessor needs to implement this directly
     * to handle event driven sources, but it's marked final in AbstractProcessor.
     */
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    try {
        onTrigger(context, session);
        session.commit();
    } catch (final Throwable t) {
        getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, t });
        session.rollback(true);
        throw t;
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession)

Example 72 with ProcessSession

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

the class StandardFunnel method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    try {
        onTrigger(context, session);
        session.commit();
    } catch (final ProcessException e) {
        session.rollback();
        throw e;
    } catch (final Throwable t) {
        session.rollback();
        throw new RuntimeException(t);
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Example 73 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project kylo by Teradata.

the class ExecuteSparkContextJob method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLog();
    FlowFile flowFile = null;
    try {
        if (context.hasIncomingConnection()) {
            flowFile = session.get();
            if (flowFile == null) {
                return;
            }
        }
    } catch (NoSuchMethodError e) {
        logger.error("Failed to get incoming", e);
    }
    FlowFile outgoing = (flowFile == null ? session.create() : flowFile);
    final String appName = context.getProperty(APP_NAME).evaluateAttributeExpressions(flowFile).getValue().trim();
    final String classPath = context.getProperty(CLASS_PATH).evaluateAttributeExpressions(flowFile).getValue().trim();
    final String contextName = context.getProperty(CONTEXT_NAME).evaluateAttributeExpressions(flowFile).getValue().trim();
    final SparkContextType contextType = SparkContextType.valueOf(context.getProperty(CONTEXT_TYPE).getValue());
    final JobService jobService = context.getProperty(JOB_SERVICE).asControllerService(JobService.class);
    final String resultsOutputLocation = context.getProperty(RESULTS_OUTPUT_LOCATION).getValue();
    final String numExecutors = context.getProperty(NUM_EXECUTORS).evaluateAttributeExpressions(flowFile).getValue().trim();
    final String memPerNode = context.getProperty(MEM_PER_NODE).evaluateAttributeExpressions(flowFile).getValue().trim();
    final String numCPUCores = context.getProperty(NUM_CPU_CORES).evaluateAttributeExpressions(flowFile).getValue().trim();
    final int contextTimeout = context.getProperty(CONTEXT_TIMEOUT).evaluateAttributeExpressions(flowFile).asInteger();
    final boolean async = context.getProperty(ASYNC).asBoolean();
    String args = "";
    if (context.getProperty(ARGS).isSet()) {
        args = context.getProperty(ARGS).evaluateAttributeExpressions(flowFile).getValue().trim();
    }
    boolean createSuccess = jobService.createContext(contextName, numExecutors, memPerNode, numCPUCores, contextType, contextTimeout, false);
    if (createSuccess) {
        final SparkJobResult result = jobService.executeSparkContextJob(appName, classPath, contextName, args, async);
        if (result.success) {
            if (Objects.equals(resultsOutputLocation, FLOW_FILE_ATTRIBUTE_VALUE)) {
                outgoing = session.putAttribute(outgoing, appName + ".result", result.result);
            } else {
                outgoing = session.write(outgoing, outputStream -> IOUtils.write(result.result, outputStream, "UTF-8"));
            }
            session.transfer(outgoing, REL_SUCCESS);
        } else {
            session.transfer(outgoing, REL_FAILURE);
        }
    } else {
        session.transfer(outgoing, REL_FAILURE);
    }
}
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) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) ProcessSession(org.apache.nifi.processor.ProcessSession) AllowableValue(org.apache.nifi.components.AllowableValue) ComponentLog(org.apache.nifi.logging.ComponentLog) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) List(java.util.List) Relationship(org.apache.nifi.processor.Relationship) AbstractNiFiProcessor(com.thinkbiganalytics.nifi.processor.AbstractNiFiProcessor) Tags(org.apache.nifi.annotation.documentation.Tags) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) FlowFile(org.apache.nifi.flowfile.FlowFile) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 74 with ProcessSession

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

the class TestHttpRemoteSiteListener method testDuplicatedTransactionId.

@Test
public void testDuplicatedTransactionId() {
    HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    String transactionId = transactionManager.createTransaction();
    assertTrue("Transaction should be active.", transactionManager.isTransactionActive(transactionId));
    ProcessSession processSession = Mockito.mock(ProcessSession.class);
    FlowFileTransaction transaction = new FlowFileTransaction(processSession, null, null, 0, null, null);
    transactionManager.holdTransaction(transactionId, transaction, null);
    try {
        transactionManager.holdTransaction(transactionId, transaction, null);
        fail("The same transaction id can't hold another transaction");
    } catch (IllegalStateException e) {
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) Test(org.junit.Test)

Example 75 with ProcessSession

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

the class TestHttpRemoteSiteListener method testNoneExistingTransaction.

@Test
public void testNoneExistingTransaction() {
    HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    String transactionId = "does-not-exist-1";
    assertFalse("Transaction should not be active.", transactionManager.isTransactionActive(transactionId));
    ProcessSession processSession = Mockito.mock(ProcessSession.class);
    FlowFileTransaction transaction = new FlowFileTransaction(processSession, null, null, 0, null, null);
    try {
        transactionManager.holdTransaction(transactionId, transaction, null);
    } catch (IllegalStateException e) {
        fail("Transaction can be held even if the transaction id is not valid anymore," + " in order to support large file or slow network.");
    }
    transactionId = "does-not-exist-2";
    try {
        transactionManager.finalizeTransaction(transactionId);
        fail("But transaction should not be finalized if it isn't active.");
    } catch (IllegalStateException e) {
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) Test(org.junit.Test)

Aggregations

ProcessSession (org.apache.nifi.processor.ProcessSession)129 FlowFile (org.apache.nifi.flowfile.FlowFile)96 ProcessContext (org.apache.nifi.processor.ProcessContext)55 IOException (java.io.IOException)54 ProcessException (org.apache.nifi.processor.exception.ProcessException)51 Test (org.junit.Test)47 Relationship (org.apache.nifi.processor.Relationship)45 List (java.util.List)42 ArrayList (java.util.ArrayList)41 Map (java.util.Map)39 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)39 ComponentLog (org.apache.nifi.logging.ComponentLog)39 HashSet (java.util.HashSet)38 Set (java.util.Set)38 HashMap (java.util.HashMap)35 Collections (java.util.Collections)33 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)33 Tags (org.apache.nifi.annotation.documentation.Tags)33 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)31 MockFlowFile (org.apache.nifi.util.MockFlowFile)31