Search code examples
apache-nifi

Nifi Validate Records against set of value


I want to validate nifi file fields against a set of values,

Ex : If the nifi field AGE in [7,8,9] and CLASS in [2,3,4] consider this is valid record if not invalid record

Any idea how to achieve this in nifi

I tried ValidateRecord processor but couldn't find the solution


Solution

  • Use a ScriptedValidateProcessor(https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-scripting-nar/1.25.0/org.apache.nifi.processors.script.ScriptedValidateRecord/additionalDetails.html)

    Example:

    Input JSON:

    [{
     "name": "test",
     "age": 8,
     "class": 3
    },
    {
     "name": "test2",
     "age": 6,
     "class": 7
    }
    ]
    

    ScriptedValidateProcessor:

    • Record Reader: JsonTreeReader
    • Record Writer: JsonRecordSetWriter
    • Script Language: Groovy
    • Script Body:
    if (record.getValue("class").toInteger() >= 2 && 
       record.getValue("class").toInteger() <= 4 && 
       record.getValue("age").toInteger() >= 7 && 
       record.getValue("age").toInteger() <= 9) {
       return true;
    } else {
       return false;
    }
    

    Output (2 relationships - valid and invalid):

    • valid:
    [ {
      "name" : "test",
      "age" : 8,
      "class" : 3
    } ]
    
    • invalid:
    [ {
      "name" : "test2",
      "age" : 6,
      "class" : 7
    } ]