user4540719
user4540719

Reputation: 43

Jolt Spec for Nifi

I am new to nifi. I was trying to create a jolt spec but not getting it. Could anyone please help me.

Details as below: The Attributes in flow file : details, id, name , address, status

Flow file looks like : [{"to": "xxx1"},{"to": "xxx2"},{"to": "xxx3"},{"to": "xxxn"}]

Expecting below output:

       { "details": "personal", 
         "home":[
                  {"mobileno": "xxx1",
                   "id": "1",
                   "name" :"bbb",
                   "address": "Address1" },
                  { "mobileno": "xxx2",
                    "id": "2",
                   "name": "aaa",
                   "address": "address2" }
               ],
           "status": "enabled" } 

Am able to develop till this. But am not getting how to get "details" field

[{
  "operation": "shift",
  "spec": {

    "*": "home",
    "mobileno": "home[0].mobileno"
  }
}, {
  "operation": "default",
  "spec": {
    "status": "${status}",
    "home[]": {
      "*": {
        "name": "${name}",
        "id" : "${id},
        "address": "${address}"
      }
    }
  }
}]

Upvotes: 1

Views: 935

Answers (2)

mattyb
mattyb

Reputation: 12083

In addition to 7632695's answer, your shift spec won't match "mobileno" on the input, try the following:

[{
  "operation": "shift",
  "spec": {
    "*": {
      "to": "home[&1].mobileno"
    }
  }
}, {
  "operation": "default",
  "spec": {
    "status": "${status}",
    "details": "${details}",
    "home[]": {
      "*": {
        "name": "${name}",
        "id": "${id}",
        "address": "${address}"
      }
    }
  }
}]

Also, note that for a single flow file, the attributes are constant, so for each entry in the home array, each id, name, and address field will be the same. From your attributes, how would JOLT know to use id=1 for the first element and id=2 for the second, and so on?

If you want to use the index of the input array as the id, you can add this spec to your chain:

{
    "operation": "shift",
    "spec": {
      "home": {
        "*": {
          "$": "home[&1].id",
          "*": "home[&1].&"
        }
      },
      "*": "&"
    }
}

And if you want them to start at 1 instead of 0, you can add 1 to each of them by adding the following spec to your chain:

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "home": {
      "*": {
        "id": "=intSum(@0, 1)"
      }
    }
  }
}

Upvotes: 2

notNull
notNull

Reputation: 31490

In default operation you need to add details attribute.

try with below jolt spec

[{
  "operation": "shift",
  "spec": {

    "*": "home",
    "mobileno": "home[0].mobileno"
  }
}, {
  "operation": "default",
  "spec": {
    "status": "${status}",
    "details":"${details}",
    "home[]": {
      "*": {
        "name": "${name}",
        "id": "${id}",
        "address": "${address}"
      }
    }
  }
}]

Upvotes: 0

Related Questions