DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Mule ESB 3.7 and DataWeave - Create Fixed-Width Files

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.

Manik Magar user avatar by
Manik Magar
·
May. 23, 16 · Integration Zone · Tutorial
Like (2)
Save
Tweet
8.02K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Enterprise service bus file IO

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ktor - A Kotlin Web Framework
  • AI Philosophy and Its Part in Digital Design
  • Everything You Should Know About APIs
  • Distributed Training on Multiple GPUs

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo