Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Loop through a list to pass a value into a processor

Loop through a list to pass a value into a processor

New Contributor

Hello,

I have a comma separated list that contains IDs. I want to pass the IDs one by one into my custom processor. Currently what I have is something like this:

UpdateAttribute --> ExecuteScript --> Custom Processor

In my updateAttribute, I have an attribute called 'list_of_account_ids' which contains the IDs.

Here is my ExecuteScript command:

flowFile = session.get();
if (flowFile != null) {
    try {
        var str = flowFile.getAttribute('list_of_account_ids');
        var str_array = str.split(',');
	var flowFile_list = [];
        for (var i = 0; i < str_array.length; i++) {
            // Trim the excess whitespace.
            str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
            // Add additional code here, such as:
            var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback");
            var IOUtils = Java.type("org.apache.commons.io.IOUtils");
            var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
            flowFile = session.putAttribute(flowFile, 'account_id', str_array[i]);
            flowFile = session.write(flowFile, new StreamCallback(function(inputStream, outputStream) {
                var content = "account_id=" + str_array[i];
                outputStream.write(content.getBytes(StandardCharsets.UTF_8));
            }));
        }

	session.transfer(flowFile, REL_SUCCESS);

    } catch (e) {
        log.error('Error: ' + e);
        session.transfer(flowFile, REL_FAILURE);
    }
}

The problem with this code is that only the last ID is getting sent to the processor, which is because the session.transfer is outside of the for loop. However, if I put the session.transfer in the loop, I am faced with an error saying that the session is not known. How do I solve this problem?

Thank you in advance.

Regards,

Kevin

Don't have an account?
Coming from Hortonworks? Activate your account here