Support Questions

Find answers, ask questions, and share your expertise

Can I call javascript from MyProcesssor code in Apache Nifi?

avatar

I want to call javascript script from MyProcessor code in Apache Nifi.

I am calling following binary parser written in javascript in java which is npm module .Can package.son and npm modules part of NAR

Code: Calling following

Javascript.js

var Parser = require('binary-parser').Parser;

// Build an IP packet header Parser

var ipHeader = new Parser()

.endianess('big')

.bit4('version')

.bit4('headerLength')

.uint8('tos')

.uint16('packetLength')

.uint16('id')

.bit3('offset')

.bit13('fragOffset')

.uint8('ttl')

.uint8('protocol')

.uint16('checksum')

.array('src', {

type: 'uint8',

length: 4

})

.array('dst', {

type: 'uint8',

length: 4

});

// Prepare buffer to parse.

var buf = new Buffer('450002c5939900002c06ef98adc24f6c850186d1', 'hex');

// Parse buffer and show result

console.log(ipHeader.parse(buf));

MyProcessor Code:

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("JavaScript");

if (!(engine instanceof Invocable)) {

System.out.println("Invoking methods is not supported.");

return;

}

Invocable inv = (Invocable) engine;

String scriptPath = "binaryParser.js";

1 ACCEPTED SOLUTION

avatar

Hi @Mohit Sharma,

You should look at the ExecuteScript and InvokeScriptedProcessor processors. Both of these processors can execute code written in Javascript directly from NiFi. @Matt Burgess has written very helpful blog posts on this functionality.

View solution in original post

4 REPLIES 4

avatar

Hi @Mohit Sharma,

You should look at the ExecuteScript and InvokeScriptedProcessor processors. Both of these processors can execute code written in Javascript directly from NiFi. @Matt Burgess has written very helpful blog posts on this functionality.

avatar

Thanks Andy for your response .I did try above mentioned options but have doubt regarding npm modules and package.json are not working in InvokeScriptedProcessor

Can you provide some implementation examples using javascript part from above mentioned links

avatar
Master Guru

If you want to include NPM modules, check this link for more details on how to use them with Nashorn.

avatar
New Contributor

Thank you for your quick reply!

Javascript obfuscator