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

  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Amazon OpenSearch Vector Search Explained for RAG Systems
  • Production-Grade RAG: Why Vector Search Isn't Enough (and How Hybrid Search Fills the Gaps)

Trending

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • Getting Started With GitHub Copilot CLI for Coding Tasks
  • Conversational Risk Accumulation: Stateful Guardrails Beyond Single-Turn LLM Checks
  • The Latency Tax That’s Hidden in Cloud-Native Systems (and the Hard Lessons I Learned to Minimize It)
  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

  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Amazon OpenSearch Vector Search Explained for RAG Systems
  • Production-Grade RAG: Why Vector Search Isn't Enough (and How Hybrid Search Fills the Gaps)

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