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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Dynamically Evaluate Dataweave Scripts
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Rust’s Ownership and Borrowing Enforce Memory Safety

Trending

  • GitHub Shared Responsibility Model and Source Code Protection
  • OpenTelemetry for Microservices Tracing and Observability
  • Query Management Philosophy In Salesforce
  • Generative AI Leverage in Application Modernization
  1. DZone
  2. Coding
  3. Java
  4. Import a Function/Module in Dataweave

Import a Function/Module in Dataweave

This tutorial explains how many ways we can import Function or Module in Dataweave - Mule 4. Read the article below to find out how!

$$anonymous$$ user avatar by
$$anonymous$$
·
Nov. 17, 21 · Tutorial
Like (2)
Save
Tweet
Share
18.9K Views

Join the DZone community and get the full member experience.

Join For Free

Why Should We Use Import in the First Place?

In Dataweave, whenever we need to use some function/module either that is not part of the core or custom-made by you or your fellow developer you need to use the import keyword to import the respective function/module.

Before going into the details we need to understand that a Function is part of the Module and there are many other modules available in Dataweave for our use.

As an example in this article, we will be focusing on specific functions called SubString and reverse from a module Strings which is not imported to Dataweave by default. The same process is applicable to any other module.

For interactive learning use this platform by Mulesoft to execute the below code snippets. 


How Do You Import Any Function?

We can import any function into Dataweave using 3 types of approaches.

1. Import Module

This approach is recommended when we know we will be using functions from a specific module but are not sure which functions will be needed. 

This way we can simply import the module without specifying the functions’ names just by giving import dw::core::Strings in the header (before ---). When we do this, we need to refer to the imported module every time we want to use a function from the module. 

As we are using the substring function in the Strings module first we need to refer to the Strings module and then the function we are gonna use by keeping the scope resolution operator (::)  in between them Strings::substring.

Java
 
%dw 2.0
output application/json
import dw::core::Strings
---
{
   sub_string :  Strings::substring("I am Max Mule",5,13),
   reverse_string : Strings::reverse("tfoseluM")
}


Output: 

Output screenshot.


Dataweave will give the following error if we do not use the module name before the function name:

Error screenshot.


2. Import Everything from a Module

If you are going to use a function in 100 places or 100 functions from a module, it’s an extra effort to keep the module name prefixed to the function name every time we use it. But, by using this way we can import all functions into Dataweave and eliminate referencing of the module.

Here we use an asterisk (*) in the import statement to import all functions from module eg.  import * from dw::core::Strings. Now, we can directly use the respective function without referring to the module that is imported from.

Java
 
%dw 2.0
output application/json
import * from dw::core::Strings
---
{
   sub_string :  substring("I am Max Mule",5,13),
   reversed_string : reverse("tfoseluM")
}


Output:

Output screenshot.

Vice versa to the first approach Dataweave will give the following error if we use the module name before the function name.

Error 2 screenshot.


3. Import What You Need 

This approach is recommended when we know exactly which functions we are going to use from the module. Instead of an asterisk (*) we will specify the function names that we intend to use eg. import substring, reverse from dw::core::Strings

A point to remember while using this approach is if we can not use any other function of the imported module unless it is explicitly mentioned in the import statement.

Java
 
%dw 2.0
output application/json
import substring,reverse from dw::core::Strings
---
{
   sub_string :  substring("I am Max Mule",5,13),
   reversed_string : reverse("tfoseluM")
}


Output:

Output screenshot.

Dataweave will throw the following error if you call any function which is not defined in the import. Here we have deleted the reverse from the import statement while keeping the function call as it is.

Error screenshot.

Conclusion

In conclusion, it’s up to the developer to choose which approach is best suited based on the requirements.

Interactive Learning Strings Java (programming language) Asterisk (PBX) IT EGS (program) dev Snippet (programming) Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • Dynamically Evaluate Dataweave Scripts
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Rust’s Ownership and Borrowing Enforce Memory Safety

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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: