Successfully reported this slideshow.
Your SlideShare is downloading. ×

Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With MuleSoft

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 32 Ad

More Related Content

Similar to Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With MuleSoft (20)

More from Jitendra Bafna (20)

Advertisement

Recently uploaded (20)

Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With MuleSoft

  1. 1. DataWeave With MuleSoft Engineering Student MuleSoft Meetup Group July 16, 2022 16:30 IST (GMT+05:30)
  2. 2. Safe Harbour Statement ● Both the speaker and the host are organizing this meet-up in individual capacity only. We are not representing our companies here. ● This presentation is strictly for learning purposes only. Organizer/Presenter do not hold any responsibility that same solution will work for your business requirements. ● This presentation is not meant for any promotional activities. 2
  3. 3. A recording of this meetup will be uploaded to events page within 24 hours. Questions can be submitted/asked at any time in the Chat/Questions & Answers Tab. Make it more Interactive!!! Give us feedback! Rate this meetup session by filling feedback form at the end of the day. We Love Feedbacks!!! Its Bread & Butter for Meetup. Housekeeping 3
  4. 4. Organizers 4
  5. 5. About the Speaker Nitish Raj, Lead Software Engineer – EPAM Systems 5 • Started IT journey in 2014 as a Java developer • Started working on Mule 3.8 in 2016 • Recent MuleSoft Certified Developer Level 2 • Co-Leader of Engineering Student Meetup and Manchester Meetup group. • MuleSoft Mentor
  6. 6. Quick recap (Previous meetups) 1. What is MuleSoft and its core capabilities? Quick start with MuleSoft. 2. Start With API Design Using Restful API Modeling Language (RAML) 3. API Implementation using APIKIT router, Anypoint API Manager and API Policies 4. MuleSoft API testing 5. Error Handling With MuleSoft 6. DataWeave Basics 6
  7. 7. What is DataWeave? ● Functional programming language designed for transforming data. ● Expression language used to configure components and connectors ● Can be used to access and evaluate the data in the payload, attributes, and variables of a Mule event ● Case-sensitive ● Supports auto-complete ● It can be used to take one piece of data, and convert it into a different output format by adding or removing values. ● It is also available in other contexts, like as a command-line tool. ● Current version: 2.4 7
  8. 8. Sample Use Case 8 • User fills out details and orders an item • In the Backend, Mule APIs kick off to transform the data in required formats. • User details are sent to Salesforce in JSON • User Address details are the sent to Database in JAVA format • User items and billing details are sent to SAP in XML format.
  9. 9. Architecture of DataWeave Script ● A DataWeave script consists of a header and a body, separated by three dashes ● Header ○ Directives, e.g. Import, Input type, output type, Global vars, functions etc ● Body* ○ The body contains the expression that produces the resulting output, usually a data mapping or a transformation. 9 %dw 2.0 output application/json --- payload.message DataWeave version (Major) Header and Body Separator (Three dashes) Body (Generates the output) Header contains directives that apply to the body expression
  10. 10. Data Types 10 Data Type Definition & Example String Enclosed by double quotes E.g. “mulesoft” Boolean True or False, without any quotes Number 12345 Date DateTime |2020-10-01| |2020-10-01T23:57:59| Array* Set of values enclosed in square brackets E.g. [1,2,3,4,5 ] Object* Set of key value pairs enclosed in curly braces {} E.g. {“key”: “value”} {“name”:”nitish”}
  11. 11. Data Operators ● Data operators are symbols that enable you to perform operations on data to produce a result. ● It is the same as most common programming languages. 11 Operator Description + For addition - For subtraction * For multiplication / For division Operator Description < For less than. > For greater than. <= For less than or equal to. >= For greater than or equal to. == For equal to. ~= Equality operator that tries to coerce one value to the type of the other when the types are different Operator Description not Negates the result of the input. ! Negates the result of the input. and Returns true if the result of all inputs is true, false if not. or Returns true if the result of any input is true, false if not. Mathematical Operators Logical Operators Equality & Relational Operators
  12. 12. Quiz ● Which one of the below is a valid JSON object ○ [{ "name": "nitish“ }] ○ { "name": "nitish“ } ○ { "name"} ● Which are the examples of a valid Array below ○ [{ "name": "nitish“ }] ○ [1,2,3,4,5] ○ { "name"}
  13. 13. Mule 4 event structure revisit 13
  14. 14. Accessing Mule objects 14
  15. 15. Quiz ● How a query param with a key name can be read using the DataWeave expression? ○ attributes.queryParams.name ○ payload.queryParams.name
  16. 16. Writing expressions for JSON & Java Input & Output ● The data model can consist of three different types of data: objects, arrays, simple literals 16
  17. 17. Hands On - Walkthrough ● Exploring DataWeave options ○ Transform Message Component ○ Metadata propagation from various connectors (HTTP Listener etc) ● Preview feature ○ Checking Tooling & Restarting ● Checking your DataWeave expression using review feature ● Creating multiple transformations ● Sample Quiz 17
  18. 18. Quiz ● Where does the DataWeave code go? ● Can you save the code externally and reuse it?
  19. 19. DataWeave Selectors ● Traverse the structures of objects and arrays and return matching values. ● Can be used on Payload, variables and everywhere where DataWeave expression is needed 19 Selector Type Syntax Return Type Single-value .keyName Any type of value that belongs to a matching key Multi-value .*keyName Array of values of any matching keys All repeated, Not in nested structure (Only root key if matched) Descendants ..keyName Array of values of any matching descendant keys Not repeated, Only first in each main and nested structure Index [<index>] Value of any type at selected array index Range [<index> to <index>] Array with values from selected indexes Selector
  20. 20. If Else and Else if 20 Operator Description if else An if operator evaluates a conditional expression and returns the value under the if only if the conditional expression is true. Otherwise, it returns the expression under else. Every if expression must have a matching else expression. else if An else operator chains expressions together within an if-else construct by incorporating else if. %dw 2.0 output application/json var data = {"name": "nitish"} --- if (data.name == "nitish") "Success" else if (data.name[0] == "n") "Partial Success" else "Failure" if (criteria_expression) <return_if_true> else if (criteria_expression) <return_if_true> else <return if no other match>
  21. 21. Map ● Use the map function to apply a transformation to each element in an array. ● The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $. ○ Input can be: JSON or Java Array ○ Returns Array 21 payload map () { } Payload should be an array Function, applied to each element (Each data in array) Lambda
  22. 22. MapObject ● Iterates over an object using a mapper that acts on keys, values, or indices of that object. Accepts Object Returns Object ● Parameters ○ Object: Input to the operator ○ Mapper: Expression that provides the key, value or index used for mapping the specified object into an output object 22 %dw 2.0 output application/json var data = { "a":"b","c":"d"} --- data mapObject { ($$$): {($$): $} } { "0": { "a": "b" }, "1": { "c": "d" } } Indices Value key
  23. 23. Pluck ● Map an object into an array ● Iterates over an object and returns an array of keys, values, or indices from the object. ● Alternative to mapObject but returns Array instead of an array ● Parameters ○ Object: Input to operator ○ Mapper: Expression that provides the key, value or index used for mapping the specified object into an output object 23 %dw 2.0 output application/json var data = { "a":"b","c":"d"} --- data pluck { ($$$): {($$): $} } [ { "0": { "a": "b" } }, { "1": { "c": "d" } } ] Indices Value key
  24. 24. Flatten ● Converts a set of sub-arrays into a single flattened array ○ Syntax: flatten(arrayOfArrays) ■ Input: [ [1,2,3], [4,5,[6]], [], [null] ] ■ Output: [ 1, 2, 3, 4, 5, [6], null ] ■ Input should always be an array, that can be array of multiple arrays or any supported data types. ● E.g. Useful in merging output result from Scatter Gather 24 [1,2,3,4,5] [“A”,”B”,”C”,”D”,”E”] [1,2,3,4,5, “A”,”B”,”C”,”D”,”E”] Array 1 Array 2 Single Array
  25. 25. Writing expressions for XML output ● XML documents must contain one root element that is the parent of all other elements: ● Speciying attributes for XML output ○ @(attributeName: attributeValue) ■ Attribute Name: Key name of the data ■ Attribute Value: mapped value/ required value ■ Attributes must always be “quoted” 25 <root father=“Mr Muley”> <child> <subchild>.....</subchild> </child> </root>
  26. 26. Calling a flow from a DataWeave expression ● Lookup function ○ Lookup(“flowToBeCalled”, payload) ■ 3rd argument: Timeout in ms (Optional) ○ Arguments ■ Flow name to be called ■ Payload (data) to be sent ● Limitation ○ Can not call sub-flows ○ Default timeout is 10 seconds, Can be modified 26
  27. 27. Creating multiple transformations ● You can also create multiple transformations with one Transform Message component 27
  28. 28. Quiz ● Does the target of a transformation have to be the payload? ● Is DataWeave expression case sensitive? ● Difference between Map, Map Object and Pluck. ● Does Flatten remove null values? ● Where DWL files are stored in the project structure? A. src/main/resources B. src/test/resources C. sr/main/mule D. src/test/munit 28
  29. 29. Trivia Quiz 1. Can a DataWeave expression be stored in a variable? A. True B. False 2. Which DataWeave operator takes in Object(s) as input and outputs as Array A. Map B. Map Object C. Pluck D. Concat 29
  30. 30. Trivia Quiz 3. DataWeave lookup() operator can only call the below kind of flow A. Sub-flow B. Flow 4. How can an attribute be specified in XML data? A. @(map: mapObject) B. @(attributeValue: attributeName) C. @(attributeName: attributeValue) 30
  31. 31. Introduce yourself to your neighbor Networking time
  32. 32. Thank you

×