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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Dynamic Arrays, Spill, and LET: What Changed in Excel and Why It Matters for Java Applications
  • Select AI and Vector Search on a Legacy Oracle Schema: What It Actually Takes
  • Designing Rayfall: One Expression Language for a Columnar Database
  • Vector Database Indexing Explained: Why It Matters More Than the Embeddings Themselves

Trending

  • Why DDoS Protection Is an Architectural Decision for Developers
  • Ampere System Profiler: A Guide to System-Level Profiling
  • Why AI Projects Stall Between Proof of Concept and Production
  • How to Monitor AI Models Without Drowning in Alerts
  1. DZone
  2. Data Engineering
  3. Data
  4. Create an Array of Numbers in DataWeave

Create an Array of Numbers in DataWeave

Learn how to create an array of numbers in DataWeave.

By 
Bharath Mandava user avatar
Bharath Mandava
·
Updated May. 21, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
27.1K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes we need a simple array to write up/test functionality. In such cases, we create an array, as shown below, and use it.

int[] arr = new int[] {1,2,3,4,5};

But what if we need it in a large size, something like 10,000? In such cases, we create an array with an expected size, and it starts populating data in a simple for/while loop such as below.

int[] arr = new int[10000];
for( int i = 0; i< arr.length; i++ ) {
arr[i] = i+1;
}

The same situation has happened to me in a Transform Message component while I was trying to work on a function that takes an array of numbers as input payload and sends to batch job. To test my batch step performance, I needed a big payload array like 1,000,000. This is how I was able to figure out how to create an array using DataWeave directly. At the same time, I didn't want to go for a Java method just to test my functionality. 

A custom recursive function populates the data until the specified max number.

Here is the code for this.

%dw 2.0 
output application/json
fun prepareList(list:Array, maxSize: Number) = if(sizeOf(list) >= maxSize )
list
else
prepareList(list ++ [(sizeOf(list) + 1) as Number],maxSize)
---
prepareList([],1000)

The output displayed in Transform Message's preview:

[
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
.
.
.
  997,
  998,
  999,
  1000
]

Let me know your thoughts in the comments.

Data structure

Opinions expressed by DZone contributors are their own.

Related

  • Dynamic Arrays, Spill, and LET: What Changed in Excel and Why It Matters for Java Applications
  • Select AI and Vector Search on a Legacy Oracle Schema: What It Actually Takes
  • Designing Rayfall: One Expression Language for a Columnar Database
  • Vector Database Indexing Explained: Why It Matters More Than the Embeddings Themselves

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook