0

I have following json data and i want to convert data in expected result by ExecuteScript nifi processor

{
"time": "2017-01-01T01:14:55+00:00",
"any": {
"nested": "data"
}
}

expected Result

{
"time": 1483233295,
"any": {
"nested": "data"
}
}

I am using following groovy code but getting some error please help to find the solution

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");

flowFile = session.write(flowFile, new 
        StreamCallback(function(inputStream, outputStream) {

var inputJSON = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
var contentObj = JSON.parse(inputJSON);

contentObj.time = flowFile.getAttribute("timestamp");


outputStream.write(JSON.stringify(contentObj).getBytes(StandardCharsets.UTF_8));
}));

session.transfer(flowFile, REL_SUCCESS);
}

getting error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/jdoodle.groovy: 8: unable to resolve class StreamCallback
 @ line 8, column 36.
   flowFile = session.write(flowFile, new 
                                      ^

1 error
6
  • 1
    that seems to be Javascript, not Groovy Commented Jul 1, 2022 at 15:27
  • can you help to find the groovy script to do the same Commented Jul 1, 2022 at 15:52
  • why you think this is javascript Commented Jul 1, 2022 at 15:53
  • var flowFile = session.get(); Note var Commented Jul 1, 2022 at 16:13
  • Also, it looks like all the JavaScript examples here: community.cloudera.com/t5/Community-Articles/… Commented Jul 1, 2022 at 16:14

1 Answer 1

3

use ExecuteGroovyScript processor (it's optimized for groovy lang) with this kind of code:

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

def flowFile = session.get()
if (!flowFile) return

flowFile.write{rawIn, rawOut->
    def json = rawIn.withReader("UTF-8"){ r-> new JsonSlurper().parse(r) }
    json.time = Date.parse("yyyy-MM-dd'T'HH:mm:ssX", json.time).getTime()/1000
    rawOut.withWriter("UTF-8"){ w-> new JsonBuilder(json).writeTo(w) }
}

REL_SUCCESS << flowFile

this code converts format of the field time with Date format to unix epoch time inside json content of flowfile.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi daggett Thanks for your response
getting some error : ERROR [Timer-Driven Process Thread-8] o.a.n.p.groovyx.ExecuteGroovyScript ExecuteGroovyScript[id=c4849b42-0181-1000-9e76-01b5367ca62a] groovy.lang.MissingMethodException: No signature of method: Scriptffffffffd89aa9e8.var() is applicable for argument types: (org.apache.nifi.processors.groovyx.flow.GroovySessionFile)
Sorry, should be def instead of var. Copy-paste issue.
Soory Daggett its work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.