transformapache-nififile-format

Can we write/prepare Fixed Width Length file type in Nifi?


I have an input JSON, and I want to convert that JSON to a Fixed width length file format.

I wanted to know if there is any way I can convert the JSON file to Fixed Width length file format in Nifi, either by using some processor or property while writing the records.

The Fixed width length file format is text format.

Here is the sample Input Json

[
  {
    "orderId": "1234567890",
    "orderName": "Test1"
  },
  {
    "orderId": "12235",
    "orderName": "Test2"
  },
  {
    "orderId": "12236",
    "orderName": "Test3"
  }
]

Here is the expected output:

1234567890 Test1     
12235      Test2     
12236      Test3     

Any leads would be helpful.

Thanks in advance!


Solution

  • I have since figured out a way to do this without writing a custom Record Writer. I used ConvertRecord processor, from JSONTreeReader to FreeFormTextRecordSetWriter. The JSON looks like this:

    [
        {
            "userId": "68156",
            "firstName": "Vesta",
            "lastName": "Herzog"
        },
        {
            "userId": "64191",
            "firstName": "Jenifer",
            "lastName": "Bosco"
        }
    ]
    

    Then, configure the Text property of the FreeFormTextRecordSetWriter as such:

    ${userId:padRight(10,' ')}${lastName:padRight(15,' ')}${firstName:padRight(15,' ')}
    

    This generates a 40 character wide fixed width file, with the userID starting at character 1, lastname starting at character 11, and firstname starting at character 26.

    68156     Herzog        Vesta
    64191     Jenifer       Bosco
    

    A generic fixed width file writer that could be configured would be far better, particularly if you have multiple fixed width formats to generate. However, this gets the job done.