DataWeave Transformations in Mulesoft


In this article, we will see how to perform transformations using DataWeave in Mulesoft. Let create a flow to get cars information from derby database using derby configurations.

Note:On the project Right Click and  Select Build Path > Add to External JAR and Select derbyclient.jar; you should now see the JAR file in the project's Referenced Libraries.

Drag and drop the Http and Database connector into the message flow canvas, configure the as before and in order to configure derby database add new connector configuration in Database properties window and In the Choose Global Type dialog box, select Connector Configuration > Derby Configuration and click OK. 

In the Global Element Properties dialog box, select Configure via spring-bean. Click the Add button next to DataSource Reference. On the Bean page of the Global Element Properties dialog box, set the ID and name to DerbyDB. Click the Browse for Java class button next to class.

In the Class browser dialog box, search and select the matching item for StandardDataSource – org.enhydra.jdbc.standard. On the Bean page of the Global Element Properties dialog box, click the Add button under Bean Subelements and select Add Property and configure properties as shown. 

In the Database properties view, set the operation to Select. Add a query to select all records from the Cars table. SELECT * FROM Cars ,as shown
Test the application, run the project.
In Postman, make another request you should get some garbled plain text displayed – the tool's best representation of Java objects.
Lets quick drag a object to Json transform and re-run the project. In Postman, send the same request; you should see the cars data represented as JSON.
Drag a Transform Message component from the Mule Palette and drop it after the Database endpoint. Double-click the Transform Message component in the canvas. In the Transform Message properties view, look at the input section and review the payload metadata; it should match the data returned by the Database endpoint. Add metadata for the transformation output by Clicking the Define metadata link in the output section.
In the Select metadata type dialog box, click the Add button. In the Create new type dialog box, set the type id to CarsOP. Click Create type. Back in the Set metadata type dialog box, set the type to JSON.Change the Schema selection to Example. Select Cars-example.json and click Open; you should see the example data for the metadata type.
Click Select; you should now see output metadata in the output section of the Transform Message properties view. Map fields with the same names by dragging them from the input section and dropping them on the corresponding field in the output section.
Click the Preview button in the output section and switch to payload in the input section, play around the input data to verify the mapping logic and on the output section, you should see a sample response for the transformation.
Run the project and verify the output.
Lets develop a new flow, to render the data in XML format. This would implement a URI parameterization and query filtering based on the URI passed, as shown below.
In the transformation, we will configure the output metadata with xml format file as shown,
Lets map the data by mapping the input to output and run the project.
In Postman, make another request you should XML response but its not what we expected.
Now, we would modify the mapping logic as shown below and retest the project.
In Postman, make another request you should XML response with proper hierarchical representation of information of cars. 
Below screen shot shows (Medium) complex transformation of Order Request to Order Details XML To XML transformation.



No comments:

Post a Comment

Note: only a member of this blog may post a comment.