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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Securing AI Agents in Production: A Developer’s Guide To Prompt Injection, RAG Leaks, and Tool Abuse
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough

Trending

  • Containment Is a Feature: Sandboxing, Egress Control, and Provenance for Autonomous Agents
  • What's Really in an Agent's Context Window
  • Seven AI Adoption Failures and How to Overcome Them
  • How to Tell If Code Is AI-Generated (and What to Ask)

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see 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
·
Updated May. 21, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Securing AI Agents in Production: A Developer’s Guide To Prompt Injection, RAG Leaks, and Tool Abuse
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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