Home > OS >  how to perform if-else drop operation in Apache nifi
how to perform if-else drop operation in Apache nifi

Time:09-03

I have a use case where I have couple of key values and perform if-else operation on it. If condition is not matched then whole content will drop, else pass the content as a result.

Input JSON :

{
  "id": 30006,
  "SourceName": "network",
  "Number": 1,
  "SourceNameCopy": "network",
  "currenttime": "Thu Aug 30 21:19:27 IST 2022"
}

My Jolt Spec :

[
  {
    "operation": "shift",
    "spec": {
      "SourceNameCopy": {
        "network": {
          "@1": "&2",
          "#id": "id",
          "#SourceName": "SourceName",
          "#Number": "Number",
          "#currenttime": "currenttime"
        },
        "hardware": {
          "@1": "&2",
          "#id": "id",
          "#SourceName": "SourceName",
          "#Number": "Number",
          "#currenttime": "currenttime"
        }
      }
    }
  }
]

Expected output :

if condition matched :

{
  "id": 30006,
  "SourceName": "network",
  "Number": 1,
  "SourceNameCopy": "network",
  "currenttime": "Thu Aug 30 21:19:27 IST 2022"
}

Else (condition not matched) Drop the whole event as null.

Problem Statement : The Key values is getting as a string, it should contain actual value in output as a result.

CodePudding user response:

If your aim is to check out the match for value of SourceNameCopy versus fixed cases network or hardware, then add an OR operator(|) among them and compare as in the following case within a shift transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "SourceNameCopy": {
        "network|hardware": {
          "@2": "" // bring the whole value after going two levels up the tree
        }
      }
    }
  }
]

No need to include nothing about the other cases they would return as null spontaneously.

  • Related