Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mule ESB 3.7 and DataWeave - Create Fixed-Width Files

DZone's Guide to

Mule ESB 3.7 and DataWeave - Create Fixed-Width Files

In this post, we will see how we can generate a fixed-width file using DataWeave in Mule 3.7.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

In my previous post, I demonstrated how to read fixed width file using DataWeave in Mule ESB 3.7. In this post, we will see how we can generate fixed width file using DataWeave in Mule 3.7.

Note: DataWeave with Mule ESB 3.8 supports fixed width files, so if you are using Mule ESB 3.8 then you may want to look at Mule Documentation here for that. 

Let’s refer to our previous example and we will use the output of that flow to regenerate our original-like fixed width file.

Input:

We will use below JSON list as an input to our flow.

[
  {
    "Id": "001",
    "Day": "MON",
    "Shout": "STARTWORK"
  },
  {
    "Id": "001",
    "Day": "TUE",
    "Shout": "PROGRESSWORK"
  },
  {
    "Id": "001",
    "Day": "WED",
    "Shout": "TAKEABREAK"
  },
  {
    "Id": "001",
    "Day": "THU",
    "Shout": "RESUMEWORK"
  },
  {
    "Id": "001",
    "Day": "FRI",
    "Shout": "FINISHWORK"
  },
  {
    "Id": "001",
    "Day": "SAT",
    "Shout": "CHILLDOWN"
  },
  {
    "Id": "001",
    "Day": "SUN",
    "Shout": "GRABABEER"
  }
]

Our fixed width record structure for output is like this - 

Id - Start Position: 0, length: 3
Day - Start Position: 3, length: 4
Shout - Start Position:7, length: 20

DataWeave Transformation:

Here is the DataWeave script to convert our input JSON to fixed width file, we will go over each command in this script - 

%dw 1.0
%output application/csv header=false
---
 (payload map {
         Id: rightPad($.Id,3,' '),
         Day: rightPad($.Day,4,' '),
         Shout: rightPad($.Shout,20,' ')
     }) map {
        row: ($ pluck $ joinBy "")
     }

Let’s understand how this code works –

  • The '%output' directive defines our output to be application/csv, and we have turned off header printing in the output.
  • First we use the map function on the payload to format the values as we require in our final output. There is no built-in rightPad function in DataWeave so we write an MEL global function in our mule-config.xml that uses the Apache Commons StringUtils class. Below is our function definition. What it does is, for a given string value, it appends the padChar to make string value of given length. E.g., the Shout field should be of length 20 characters in the output. So if required, it will pad the input with empty space ‘ ‘ to make it 20 characters long. 
<configuration doc:name="Configuration">
    <expression-language>
        <global-functions>
            def rightPad(value,length,padChar) {
                return org.apache.commons.lang3.StringUtils.rightPad(value,length,padChar);
            }

        </global-functions>

    </expression-language>
</configuration>
  • Then we apply the map function to iterate over the object collection that we prepared in step 2. For every object in that collection, we will create another object with a single property of key ‘row’ and the value is constructed by joining all the values of every property in the source object. We use the pluck function to generate an array of property values and then create a string by joining array elements. If we don’t turn off headers in the output, the very first line in our output will have a value ‘row’ i.e. header.

Output

Here is the output from our transformation -

001MON STARTWORK 
001TUE PROGRESSWORK
001WED TAKEABREAK
001THU RESUMEWORK
001FRI FINISHWORK
001SAT CHILLDOWN
001SUN GRABABEER

If you prefer to see it in action, here is the video demonstrating this transformation -


Conclusion

DataWeave is a very powerful transformation language and we can use it to write any data format transformation we need. Even though DataWeave (until Mule ESB 3.7) does not support fixed width transformations, with some extra code we achieved that too. Hope this helps!

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule 3.7 ,file write ,integration

Published at DZone with permission of Manik Magar. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}