Support Questions
Find answers, ask questions, and share your expertise

NiFi - ExecuteScript for getting max value of a Json array

Expert Contributor

Hi, as far I have investigated it is not possible in EvaluateJsonPath to get the maximum value of an element of an array. Searched for something like shown in the picture.

Now I'm searching for a solution with ExecuteScript like mentioned here:

https://community.hortonworks.com/questions/57755/apache-nifi-how-to-calculate-sum-or-average-of-val...

Because Im not familiar with the script languages are offered I'm looking for an example to do this.

Found this: https://community.hortonworks.com/articles/75032/executescript-cookbook-part-1.html

But I don't find how to get the max value.

Any help is very appreciated, thanks!

93258-hwc-jsonmaxvalue.png

14 REPLIES 14

Expert Contributor

@Matt Burgess Superb, it works perfect! One day I will try your original solution, sounds interesting. Bye!

Expert Contributor

Hi @Matt Burgess

following your above solution for returning the max-value in an attribute my script looks like this:


def flowFile = session.get()
if(!flowFile) return
try {
   def inputStream = session.read(flowFile)
   def objList = new groovy.json.JsonSlurper().parse(inputStream)
   def max = objList.max {Date.parse("yyyyMMddHHmmss",it.elem_stand)}
   inputStream.close()
   flowFile = session.putAttribute(flowFile, 'MAX_elem_stand', max.elem_stand.toString())
   session.transfer(flowFile, REL_SUCCESS)
} catch(e) {
   log.error("Error while determining max", e)
   session.transfer(flowFile, REL_FAILURE)
}


It worked fine with Json-format:

            [{
                "zn": 1,
                "elem_stand": "20190611140623",
                "elem_id": "1086"
            },
           {
                "zn": 2,
                "elem_stand": "20190624170807",
                "elem_id": "1089"
            } ]  

But now the input format is changed to:


{
        "name": "Belegart",
        "id": "269",
        "table": [
            {
                "zn": 1,
                "elem_stand": "20190611140623",
                "elem_id": "1086"
            },
           {
                "zn": 2,
                "elem_stand": "20190624170807",
                "elem_id": "1089"
            }
        ]
    }

And I can't find the right syntax to access the elem_stand within the array for checking max-value.

Please could you tell me how this is possible? Thanks!

Super Guru

In the script, you're creating a variable `objList` that (for the first input format) points at the top-level array of objects, so you can call max() directly on that array (I think technically it's a List under the hood). In the second input format, objList will be pointing to the top-level object, so you'll need to get the array member `table` out of the object. Update the "def max" line to this:

def max = objList.table.max {Date.parse("yyyyMMddHHmmss",it.elem_stand)}

Expert Contributor

@Matt Burgess It works! Thank you so much not only for the solution but also for the explanation!

Explorer

Thats nice hint on calculating an aggregation like MAX on a single FlowFile.

I wonder if QueryRecord can also performe aggregation on multiple FlowFiles and output just 1 FlowFile with answer ? Imagine ListS3 which can output multiple FlowFiles (1 per new file created since last time) and we would like to know which of these new n inbound which one is the most recent either using s3.modified or even filename itself shall its timestamp format yyyymmdd HH:mm:ss.SSS ?

I wonder how to get the most recent file ?