DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Reversing an Array: An Exploration of Array Manipulation
  • Java String: A Complete Guide With Examples
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

Trending

  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Web Crawling for RAG With Crawl4AI
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Data Engineering
  3. Data
  4. DataWeave and the Reduce Operator: Part I

DataWeave and the Reduce Operator: Part I

Reduce is a powerful operator that can be used on an array. Specifically, it can be used to process an :array and operate on each of its elements.

By 
Rahul Kumar user avatar
Rahul Kumar
·
Feb. 25, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
35.4K Views

Join the DZone community and get the full member experience.

Join For Free

Reduce is a powerful operator that can be used on an array (DataWeave array datatype).

Reduce can be used to process an :array and operate on each of its elements. It performs an aggregation operation on the elements of the array after performing a lambda operation (optional) on each of its element.

It can be used to create a Scalar, A Map or an Array. It can also be used as a substitute for the map operator. The array to object is also possible via the reduce operator. We use the reduce operator to do this kind of transformation, in addition to various other types.

In this series, I will provide examples of how to use the reduce operator effectively.

For the reduce operations, an accumulator should be chosen according to the aggregate result we want to achieve. Also, the aggregation operator — for example, + (Add), ++ (Concat), * (Multiply), etc. — should be chosen as per requirements.

The accumulator can be viewed as the output variable. It contains the output after all the iterations over the array on which reduce is applied. Accumulator should be carefully chosen depending on the type of Output which we want to get. Suppose we want a string as an end result then a string accumulator needs to be taken, if the end result needs to be an array then accumulator should be an Array and so on. Similarly, if the end result needs to be an Object then accumulator should be an Object.

Execute a Simple Arithmetic Sum of a List's Elements 

Let's say the list elements are [10, 20, 30, 40].

  • Expected output: 100 (sum of all elements of the array).

  • Accumulator: 0 (a number zero is chosen for the addition of subsequent elements).

  • Aggregate operation: + (addition).

  • Dataweave code: payload reduce $$ + $.

We can execute a lambda over each element of the array to get a modified element and add it continuously to the accumulator.

Perform a More Complex Arithmetic Operation

Input payload (a list):

[{number : 10, times : 8},{number : 2, times : 5},{number : 7, times : 7},{number : 6, times : 3},{number : 4, times : 7}]

Expected output:  185 (sum of each element resulting from "number" multiplied by "times").

Accumulator: 0 (a number zero is chosen for the addition of subsequent elements).

Aggregate operation: + (addition).

Dataweave code:

payload reduce ((val, acc = 0) -> acc + (val.number * val.times))  

Array to Array Transformation- Modifying a List

Input payload: 

[{"Capital": "New Delhi","Country": "India"}, {"Capital": "Paris","Country": "France"}, {"Capital": "London","Country": "England"}]

Required output:

["India-New Delhi","France-Paris","England-London"] 

Accumulator: [] (an empty array is chosen for addition of subsequent elements resulting from the lambda).

Aggregate operation: + (addition operation to obtain an array of the elements resulting from lambda; this is used to make an array push on the accumulating array using the modified element).

Dataweave code:

payload reduce ((val, acc = []) -> acc + ( (val.Country) ++ "-" ++ val.Capital ))

Note: This can also be done using the map operator.

I hope that this post helps you get started using the reduce operator,

In the next post, I will try to explain and exemplify some scenarios (:array to :object)  using the reduce operator.

Link for Part 2 of this series https://dzone.com/articles/dataweave-effective-use-of-reduce-operator-part-2

Operator (extension) Data structure Element

Opinions expressed by DZone contributors are their own.

Related

  • Reversing an Array: An Exploration of Array Manipulation
  • Java String: A Complete Guide With Examples
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!