Reading Time: 7 minutes

In this article, you will learn how to use method chaining in DataWeave with reference to Java 8 lambda expressions and how to convert from Java Streams into DataWeave chaining. 

Java 8 introduced the Stream API, which is used to process a stream of objects. Various methods can be chained together such as filter, limit, map, and reduce, and operate on a pipeline of objects. To learn more about lambdas, read DataWeave Lambdas for Java programmers.

Getting started

latest report
Learn why we are the Leaders in API management and iPaaS

To get started we need some sample data to work with and a Java class.

Sample DataWeave data

We will use the JSON array below in the DataWeave expressions we will write. As you can see its an array of five JSON objects representing members of the Simpson family.

[
{"lastName":"Simpson",
"firstName":"Homer",
"age":39
},
{"lastName":"Simpson",
"firstName":"Lisa",
"age": 8
},
{"lastName":"Simpson",
"firstName":"Bart",
"age":10
},
{"lastName":"Simpson",
"firstName":"Marge",
"age":36
},
{"lastName":"Simpson",
"firstName":"Maggie",
"age": 1
}
]

Sample Java class and data

The simple Customer Java class contains a static factory method that returns a List of Customer objects populated with the same data as shown in the JSON array above.


package com.mulesoft.training;

import java.util.ArrayList;
import java.util.List;

public class Customer {

private String lastName;
private String firstName;
private int age;

public Customer(String lastName, String firstName, int age) {
this.lastName = lastName;
this.firstName = firstName;
this.age = age;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

// Other accessor and mutator methods omitted for brevity

public String toString() {
return(lastName+" "+firstName+" ("+age+")");
}

public static List createSample() {
List array = new ArrayList<>();

array.add(new Customer("Simpson","Homer",39));
array.add(new Customer("Simpson","Lisa",8));
array.add(new Customer("Simpson","Bart",10));
array.add(new Customer("Simpson","Marge",36));
array.add(new Customer("Simpson","Maggie",1));

return array;
}
}

From Java Streaming to DataWeave function chaining

Now, we have the data and class needed, let’s start with reviewing how the Stream API is used to process a pipeline of data. 

Java Streams

To use chained method on a Collection in Java, we first need to create an instance of the Stream API, then we can start to chain methods together to process a data stream.

In the following example, a Stream is created from the List returned by calling the createSample() method on the Customer class and a filter is applied to each object that removes any object where the age property is less than or equal to 18. Then each object is printed to the console.

// First create an List of elements
List array = Customer.createSample();
// Create your stream
Stream myStream = array.stream();
// Chaining with stream
myStream.filter(c->c.getAge()>18).forEach(c->System.out.println(c.toString()));

// Everything on one instruction
Customer.createSample().stream().filter(c->c.getAge()>18).forEach(c->System.out.println(c.toString()));

Two members of the Simpson family remain after filtering: Marge and Homer.

DataWeave function chaining

To replicate function chaining in DataWeave there is just one requirement: the function must take exactly two arguments:

  • The first argument is the object to work with, in our case, the array of customers.
  • The second argument must be a lambda function that describes how each element in the data stream is to be processed, in our case we want to filter based on age, so therefore we use the filter function.

In DataWeave there are two ways to apply a filter.

// classical way to call a function
filter(payload , (value, index) -> value.age > 18)

// specific Dataweave way to call a two arguments function
payload filter (value, index) -> value.age > 18

Furthermore, DataWeave allows a simplification of the lambda expression by deleting the “->.” In which case, the first argument of the lambda expression is replaced by $, and the second argument by $$.

// An example using the $ argument
payload filter $.age > 18

With such simplified notation it is now really easy to chain multiple functions.

// Function chaining with DataWeave
payload filter $.age > 18 orderBy $.age

The DataWeave expression above performs the same process as the Java code described earlier.

Conclusion

In this article, we saw how a Java programmer can easily convert code containing method chaining into similar DataWeave code using function chaining.

If you want to know more about DataWeave 2.0,  register for the DataWeave training course.