Implement a Counter in Dataweave 2.x and Above
In this article, see how to implement a counter in DataWeave.
Join the DZone community and get the full member experience.
Join For FreeThese days, I see quite a few posts that require using Counter in DataWeave. The problem faced is that users require a Sequence Number that is consistent throughout the different sections of data. Consider the below JSON:
xxxxxxxxxx
[{
"Item" : "ItemXYZ",
"SequenceNo" : 1,
"Parts": [
{ "Part" : "A1", "SequenceNo" : 2 }, { "Part" : "B1", "SequenceNo" : 3 },
{ "Part" : "A2", "SequenceNo" : 4 }, { "Part" : "B2", "SequenceNo" : 5 }
]
},
{
"Item" : "ItemRTY",
"SequenceNo" : 6,
"Parts": [
{ "Part" : "A1", "SequenceNo" : 7 }, { "Part" : "B1", "SequenceNo" : 8 },
{ "Part" : "A2", "SequenceNo" : 9 }, { "Part" : "B2", "SequenceNo" : 10 }
]
}
]
As you can see here, we have "SequenceNo," which increases every time, irrespective of the structure of JSON. If we have to add a SequenceNo in any data, we have limited options. There are workarounds like using a recursive function or calculating the counters using Complex logic to achieve a counter, but it hampers the performance. DataWeave is developed for transformations mostly, and it doesn't have built-in counters, which would save its last stored value throughout the execution.
So I have come up with a DataWeave solution that uses the Java Module. Java Module comes with an Invoke function for DataWeave, which I will be using in DataWeave. To include Java Module, add the below dependency in the pom.xml:
xxxxxxxxxx
<dependency>
<groupId>org.mule.module</groupId>
<artifactId>mule-java-module</artifactId>
<version>1.2.5</version> <!-- or newer -->
<classifier>mule-plugin</classifier>
</dependency>
We will use AtomicInteger for this example. Since it comes with a built-in increment function and is present under Java Util package, there is no need to add any extra dependencies. You can use any other implementation as well, but you would need to add a utility class and a function to use it inside the DataWeave.
Below is a simple usage example:
xxxxxxxxxx
%dw 2.0
import java!java::util::concurrent::atomic::AtomicInteger
var counter = AtomicInteger::new(0)
fun increment() = Java::invoke('java.util.concurrent.atomic.AtomicInteger', 'incrementAndGet()', counter, {})
output application/json
---
{
"field1" : "Field1Value",
"SNo" : increment(),
"Attributes" : {
"Field1" : "Field1Value",
"Field2" : "Field2Value",
"SNo" : increment(),
"Attributes" : {
"Field1" : "Field1Value",
"Field2" : "Field2Value",
"SNo" : increment()
}
}
}
This will produce an output like below:;
Used a counter variable to create an Instance of AtomicInteger and initialized to 0, increment() function will use Java invoke to increment the counter DataWeave variable. So we can use the increment() function anywhere without worrying about any other logic independently.
Below is another usage example:
xxxxxxxxxx
%dw 2.0
import java!java::util::concurrent::atomic::AtomicInteger
var counter = AtomicInteger::new(0)
fun increment() = Java::invoke('java.util.concurrent.atomic.AtomicInteger', 'incrementAndGet()', counter, {})
output application/json
---
[{
"Item" : "ItemXYZ",
"SNo" : increment(),
"Parts": [
{ "Part" : "A1", "SNo" : increment() }, { "Part" : "B1", "SNo" : increment() },
{ "Part" : "A2", "SNo" : increment() }, { "Part" : "B2", "SNo" : increment() }
]
},
{
"Item" : "ItemRTY",
"SNo" : increment(),
"Parts": [
{ "Part" : "A1", "SNo" : increment() }, { "Part" : "B1", "SNo" : increment() },
{ "Part" : "A2", "SNo" : increment() }, { "Part" : "B2", "SNo" : increment() }
]
}
]
Using the above script, we will get an output like below:
I will come up with some more utilities in the next post.
Stay tuned!
Further Reading
Opinions expressed by DZone contributors are their own.
Comments