Created 03-29-2018 03:34 AM
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.
Created 03-31-2018 05:26 PM
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)
}
Created 03-31-2018 05:26 PM
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)
}