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.

JavaScript in ExecuteScript in Nifi

Solved Go to solution
Highlighted

JavaScript in ExecuteScript in Nifi

I am trying to write and execute a Java script in Nifi. Below is all I have defined in the body of Execute script processor. token_value is an attribute and the value for it is - { "Tokens": [ "12345acdb4567", "98765acdb1111" ] }

Flow File Content is - { "customer-accounts": [ { "company_identifier": "074", "product_code": "CCD", "account_number": "222222222222", "relationship_code": "JOF", "primary_ownership_indicator": "Y" }, { "company_identifier": "674", "product_code": "CCD", "account_number": "111111111111", "relationship_code": "EOD", "primary_ownership_indicator": "N" } ] }

Script I am using is as below. I am not familiar with JavaScript. Trying to get this to work.

ScriptBody :

var flowFile = session.get() ;

if (flowFile != null) { var TokenAttr = flowFile.getAttribute('token_value') ; }

function convert(flowFile, TokenAttr)

{ let accounts = flowFile['customer-accounts']; let convertedAccounts = accounts.map((account, index) => { account['account_number'] = TokenAttr.Tokens[index]; return account; }); return { 'customer-accounts' : convertedAccounts } }

flowFile = session.write(flowFile,console.log(JSON.stringify(convert(flowFile,TokenAttr))))

session.transfer(flowFile, REL_SUCCESS)

your help will be appreciated. Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: JavaScript in ExecuteScript in Nifi

I am able to resolve this.

var flowFile = session.get(); if (flowFile != null) { 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") var JString = Java.type("java.lang.String") var TokenAttr = flowFile.getAttribute('token_value') var JsonTokenAttr = JSON.parse(TokenAttr) ; flowFile = session.write(flowFile, new StreamCallback(function(inputStream, outputStream) { var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8) var obj = JSON.parse(text) var accounts = obj['customer-accounts']; var convertedAccounts = accounts.map(function(account, index) { account['account_number'] = JsonTokenAttr.Tokens[index];return account; }); var newObj = {'customer-accounts' : convertedAccounts } var builder = new JString() outputStream.write(JSON.stringify(newObj, null, '\t').getBytes(StandardCharsets.UTF_8)) })) session.transfer(flowFile, REL_SUCCESS) }

View solution in original post

1 REPLY 1
Highlighted

Re: JavaScript in ExecuteScript in Nifi

I am able to resolve this.

var flowFile = session.get(); if (flowFile != null) { 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") var JString = Java.type("java.lang.String") var TokenAttr = flowFile.getAttribute('token_value') var JsonTokenAttr = JSON.parse(TokenAttr) ; flowFile = session.write(flowFile, new StreamCallback(function(inputStream, outputStream) { var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8) var obj = JSON.parse(text) var accounts = obj['customer-accounts']; var convertedAccounts = accounts.map(function(account, index) { account['account_number'] = JsonTokenAttr.Tokens[index];return account; }); var newObj = {'customer-accounts' : convertedAccounts } var builder = new JString() outputStream.write(JSON.stringify(newObj, null, '\t').getBytes(StandardCharsets.UTF_8)) })) session.transfer(flowFile, REL_SUCCESS) }

View solution in original post

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