News Ticker

Contrast DataWeave and Java mapping operations

Main points:

  • DataWeave 2.0 provides mapping capabilities
  • Java and DataWeave can achieve the same mappings
  • DataWeave mapping function is less verbose than Java

DataWeave map function

The DataWeave 2.0 (Mule 4) map function shares similarities with the map() method from Java’s Stream class.

Mapping is a transformative operation

The idea of mapping is to transform each element of an array and output a new array of transformed elements. An expression is provided that performs the transformation. It is applied to each element in the array and collected into another new array.

Apply a mapping to an array in Java

In Java, a transformative expression is applied by passing it to the map() method of the Stream class. It is applied in turn to each element of the array and collected to a new List. In the following code snippet the inline array is transformed into a stream so that mapping can be performed.

List<String> pets = Arrays.asList(
  new String[] { "cat", "dog", "fish" }
);

List<String> newPets = pets.stream()
  .map(e -> e.toUpperCase())
  .collect(Collectors.toList());

The transformation is performed by the lambda expression e -> e.toUpperCase() where the variable e represents each element in the array. The result of the transformation is added to a new List using a collector Collectors.toList().

There is a ‘short cut’ expression that you can use in place of the explicit lambda expression. It is String::toUpperCase, the above code would now look as follows.

pets.stream()
  .map(String::toUpperCase)
  .collect(Collectors.toList());

Apply a mapping to an array in DataWeave

In the DataWeave map function a transformative expression is applied to each element of an array and outputted to a new array containing these new transformed elements.

var pets = ["cat", "dog", "fish"]
---
pets map upper($)

The upper() function is applied to each element in the pets array and transformed. Each transformed element is put into a new array. This new array is the output of this operation. The dollar ($) symbol represents each element in the array as the map function iterates over the array. The upper() function is a lambda function from the dw::Core module. It is automatically imported into all DataWeave scripts.

Final thoughts

DataWeave has been designed to transform data and does so in a performant way. The code is concise and easy to understand. As you can see Java is more verbose but provides much more capabilities than data transformation. Consider the mapObject DataWeave function that maps an object to an object and the pluck DataWeave function which can be used to deconstruct an object by its index, keys and values.

RESTful API design and RAML

There are five things to consider when designing a RESTful API. In this blog post I introduce those important aspects with examples to illustrate how to implement them in a API specification.

If you are designing a API using RAML you will want to include examples to ensure that the specification provides the high level of documentation that a modern API should. In this blog post I discuss four ways to specify examples in a RAML API specification.

2 Trackbacks / Pingbacks

  1. Contrast DataWeave and Java mapping operations – LingarajTechHub
  2. DataWeave: the map function

Leave a Reply

%d bloggers like this: