DataWeave 1.0 in Mule3

   DataWeave 1.0   

  • Overview
  • DataWeave is the MuleSoft expression language for accessing and transforming data that travels through a Mule app.
  • DataWeave is tightly integrated with the Mule runtime engine, which runs the scripts and expressions in your Mule app.
  • DataWeave is a simple, powerful tool used to query and transform data inside of Mule.
  • With DataWeave and Mule Expression Language (MEL), we can take your application’s data transformation ability to the next level.
  • DataWeave supports a variety of transformations:
    1. simple one-to-one, one-to-many or many-to-one mappings from an assortment of data structures.
    2. Normalization
    3. Grouping
    4. Joins
    5. Partitioning
    6. Pivoting
    7. Filtering
  • DataWeave as : XML Reference
  • Check Maven Dependency
    If we are using maven based mule project, then check the pom dependency. For example, I have data weave in my project is :

  • Next is to verify the Namespace and Schema Location
    In the Xml file, validate that Datweave schema is present or not.
    Note: If we drag and drop the Transform component in mule canvas, this schema details will add automatically.

  • Add root XML tag of DataWeave
    The "dw:transform-message" element is the top-level XML tag for the Transform Message component.

  • Transformations
    After the root tag added we can use any child tag based on our requirements. For example we can these any of these Child tags : dw:set-payloaddw:set-variabledw:set-session-variable

  • Add external .dwl file
    We can use Dataweave logic in the same xml file or we can import as .dwl file.
    This example that writes a DataWeave script inline within a "dw:set-payload" transformation element:
    Here is same example, calling an external .dwl file:

  • Define Input type
    "dw:input-payload" can specify optional parameters about the input. In this we may have different types of mimeType that Mule supports, for example :

  • Define Reader configuration
    "dw:reader-property" settings can tell the reader how to parse the input. If we use any dataType which we want to read based on some specific patterns, then we should use "dw:reader-property".
  • DataWeave as : Transform Message Component
The Transform Message component carries out transformations over the input data it receives.
We can explicitly write out a transformation in DataWeave language, or you can use the UI to build it implicitly through dragging and dropping elements.
For beginner, I would say this is an easiest approach to starts with DataWeave.
It offers us a preview of our output that is built on sample data and is updated in real time as you make changes to your transformation.
  • MEL DataWeave functions
MEL adds TWO functions to use Datweave, which can help to crack complex transformations.
  • The DW Function
    To invoke an expression written in DataWeave use dw() function.

    Full attribute reference for the function:
    Attributes Description
    DataWeave Expression The actual DataWeave expression to carry out
    Output Type The type of the output of the transform
    Reader Properties For inputs such as CSV, sometimes some properties are needed to know how to parse the input
    Writer Properties For inputs such as CSV, sometimes some properties are needed to compose the output

    Example:

  • The Split Function
    As name give an idea, this function use to split the data into parts.

    It takes at least TWO arguments: a DataWeave expression and the desired output type.

    Example : split('payload','application/json')
    The function has the following attributes:
    Attributes Description
    DataWeave Expression The actual DataWeave expression to carry out
    Output Type The type of the output of the transform
    Reader Properties For inputs such as CSV, sometimes some properties are needed to know how to parse the input
    Writer Properties For inputs such as CSV, sometimes some properties are needed to compose the output

If you got some knowledge about DataWeave 1.0, please follow with me in next topic to understand about DataWeave Operators.

Comments