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

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • Mastering Redirects With Cloudflare Bulk Redirects
  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking

Trending

  • Java Virtual Threads and Scaling
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. A Comparison of Two Large CSV Files in MuleSoft

A Comparison of Two Large CSV Files in MuleSoft

One file is 5 MB and the other is 1 MB. How can we deal with such large files without setting aside a huge amount of time?

By 
Ujjawal Kant user avatar
Ujjawal Kant
·
Jan. 02, 17 · Opinion
Likes (4)
Comment
Save
Tweet
Share
9.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this scenario, we have two CSV files. The requirement is to replace the fourth field J_D with I_D in File 1 if first three fields in both the files are the same.

File 1: JDEFile

J_A,J_B,J_C,J_D
82301,8179613,20161219,555
82301,8179613,20161226,1155
82301,8179613,20170102,954
82301,8179613,20170109,668
82301,8179613,20170116,968
82301,8179613,20170123,602
82301,8179613,20170130,782
82301,8179613,20170206,815
82301,8179613,20170213,632

File 2: IL File

I_A,I_B,I_C,I_D
82301,8179613,20170213,632
82301,8179613,20170220,632
82301,8179613,20170206,810
82301,8179613,20170123,6021

Approach 1

<mulerequester:requestresource="file://D:/LOC/JDE_UPLOAD_REPORT.csv?autoDelete=false"doc:name="JD_File"/>
        <set-variable variableName="jdeFile" value="#[payload]"mimeType="application/csv" doc:name="JDEFile"/>
        <mulerequester:requestresource="file://D:/LOC/UK_EU_DSFCST_IL.csv?autoDelete=false"doc:name="IL_File"/>
        <set-variable variableName="ilFile" value="#[payload]"doc:name="ILFile" mimeType="application/csv"/>
        <dw:transform-message metadata:id="bb792c8d-6ebf-4343-a470-81746b5b1873" doc:name="FilterMap">
            <dw:input-variable mimeType="application/csv"variableName="jdeFile"/>
            <dw:input-variable mimeType="application/csv"variableName="ilFile"/>
            <dw:set-payload><![CDATA[
%dw 1.0
%output application/csv
%var D = null
%function checkPresence(A,B,C) flowVars.ilFile[?($[0] == A and $[1] ==B and $[2] == C)][0][3]  
---
flowVars.jdeFile map ((jdeFile , indexOfJdeFile) -> {
      R_A: jdeFile[0],
      R_B: jdeFile[1],
      R_C: jdeFile[2],
      R_D: checkPresence(jdeFile[0],jdeFile[1],jdeFile[2]) default 
             jdeFile[2]
})
]]></dw:set-payload>
</dw:transform-message>

Well, Approach 1 was not efficient for dealing with larger files. It takes more than 30 minutes to scan and compare. Let's move on to Approach 2.

Approach 2

File 1: JDEFile (size 5 MB).

File 2: IL File (size 1 MB).

XML flow.

The highlighted code shows the usage of a Java code inside a DataWeave script.

  • Read JD_File and store it in a flow variable named jdeFile as a string. Headers are explicitly added as the first line gets omitted in the responses of the DataWeave scripts.

  • Read IL_File and transform it into an ArrayList of HashMaps. 

  • Java code is used to add the elements of all the HashMaps in the ArrayList to one single HashMap. Refer to the file CreateHashMap.java. 

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:cluster="
      . . . . . .
      . . . . . .
 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
 <file:connector name="File" autoDelete="false"  streaming="false" validateConnections="true" doc:name="File"/>
 <configuration doc:name="Configuration">
  <expression-language>
               <global-functions>
                def getKey(value) {
                return payload.get(value)
                }
               </global-functions>
  </expression-language>
 </configuration>
    <flow name="filesrbFlow">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="1000" timeUnit="DAYS"/>
            <logger message="****************************Started*****************************8" level="INFO" doc:name="Logger"/>
        </poll>
        <mulerequester:request resource="file://D:/Accounts/POC/JDE_UPLOAD_REPORT.csv?autoDelete=false" doc:name="JD_File"/>
        <set-variable variableName="jdeFile" value="J_A,J_B,J_C,J_D#['\n']#[message.payloadAs(java.lang.String)]" mimeType="application/csv" doc:name="JDEFile"/>
        <mulerequester:request resource="file://D:/Accounts/POC/UK_EU_DSFCST_IL.csv?autoDelete=false" doc:name="IL_File"/>
        <set-payload value="I_A,I_B,I_C,I_D#['\n']#[message.payloadAs(java.lang.String)]" mimeType="application/csv" doc:name="Set Payload"/>
        <dw:transform-message metadata:id="af17dc95-43d6-4749-b97e-f7d4c8ef58b0" doc:name="IL_File_Map">
            <dw:input-payload mimeType="application/csv"/>
            <dw:input-variable mimeType="application/csv" variableName="jdeFile"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
      ABC: payload01[0] ++ payload01[1] ++ payload01[2],
      D: payload01[3]
})]]></dw:set-payload>

        </dw:transform-message>
        <logger message="*********************Starting FlowStep - Transforming File Now****************************" level="INFO" doc:name="Logger"/>
        <component class="filesrb.utils.CreateHashMap" doc:name="Java"/>
        <dw:transform-message metadata:id="bb792c8d-6ebf-4343-a470-81746b5b1873" doc:name="SwapCreate">
            <dw:input-variable mimeType="application/csv" variableName="jdeFile"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/csv header=false, separator = ","
---
flowVars.jdeFile map ((jdeFile , indexOfJdeFile) -> {
      A: jdeFile[0],
      B: jdeFile[1],
      C: jdeFile[2],
      D: getKey(jdeFile[0] ++ jdeFile[1] ++ jdeFile[2]) default jdeFile[3]
     }
)]]></dw:set-payload>
        </dw:transform-message>
        <logger message="*********************Writing File Now****************************" level="INFO" doc:name="Logger"/>
        <file:outbound-endpoint path="D:\Accounts\RB\POC" outputPattern="RESPONSE.csv" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <logger message="*********************End Transforming &amp; Writing ****************************" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Java Code

package filesrb.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

      public class CreateHashMap implements Callable {
      public void run() {       
      }

      @SuppressWarnings("unchecked")
      @Override
      public Object onCall(MuleEventContext eventContext) throws Exception {
            // TODO Auto-generated method stub
            Map<String, String> ilfilemap  = new HashMap<String, String>();
            List<Map<String, String>> list = (ArrayList<Map<String, String>>) eventContext.getMessage().getPayload();
            for (Map<String, String> entry : list) {
                  String keyValue[] = new String[2];
                  int ctr=0;
                for (String key : entry.keySet()) {
                    String value = entry.get(key);
                    keyValue[ctr] = value;
                    ctr = ctr + 1;
                }
                ilfilemap.put(keyValue[0], keyValue[1]);
            }
            return ilfilemap;
      }
}

The above task can be done without using Java code. 

In this flow, we are adding values to a flowVariable in form of a HashMap using a Groovy script embedded inside the DataWeave script. Further, the same variable is used to get the value based on the key.

XML Flow

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:cluster=" 
    . . . . . .
    . . . . . . ">
    <file:connector name="File" autoDelete="false"  streaming="false" validateConnections="true" doc:name="File"/>
    <configuration doc:name="Configuration">
<expression-language>
               <global-functions>
                def getKey(value) {
                return flowVars.ilFile.get(value)
                }
                def putKeyValue(key,value) {
                  flowVars.ilFile.put(key,value)           
                }
               </global-functions>
            </expression-language>
 </configuration>
    <flow name="filesrbFlow">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="1000" timeUnit="DAYS"/>
            <logger message="****************************Started*****************************8" level="INFO" doc:name="Logger"/>
        </poll>
        <mulerequester:request resource="file://D:/Accounts/POC/JDE_UPLOAD_REPORT.csv?autoDelete=false" doc:name="JD_File_Read"/>
        <set-variable variableName="jdeFile" value="J_A,J_B,J_C,J_D#['\n']#[message.payloadAs(java.lang.String)]" mimeType="application/csv" doc:name="JDE_File_Var"/>
        <mulerequester:request resource="file://D:/Accounts/POC/UK_EU_DSFCST_IL.csv?autoDelete=false" doc:name="IL_File_Read"/>
        <set-variable variableName="ilFile" value="#[new java.util.HashMap()]" doc:name="IL_File_Var"/>
        <set-payload value="I_A,I_B,I_C,I_D#['\n']#[message.payloadAs(java.lang.String)]" mimeType="application/csv" doc:name="Set Payload"/>
        <dw:transform-message metadata:id="af17dc95-43d6-4749-b97e-f7d4c8ef58b0" doc:name="IL_File_Map">
            <dw:input-payload mimeType="application/csv"/>
            <dw:input-variable mimeType="application/csv" variableName="jdeFile"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
      ABC: putKeyValue(payload01[0] ++ payload01[1] ++ payload01[2], payload01[3])
})]]></dw:set-payload>

        </dw:transform-message>
        <logger message="*********************Starting FlowStep - Transforming File Now****************************" level="INFO" doc:name="Logger"/>

        <dw:transform-message metadata:id="bb792c8d-6ebf-4343-a470-81746b5b1873" doc:name="SwapCreate">
            <dw:input-variable mimeType="application/csv" variableName="jdeFile"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/csv header=false, separator = ","
---
flowVars.jdeFile map ((jdeFile , indexOfJdeFile) -> {
      A: jdeFile[0],
      B: jdeFile[1],
      C: jdeFile[2],
      D: getKey(jdeFile[0] ++ jdeFile[1] ++ jdeFile[2]) default jdeFile[3]

      }
)]]></dw:set-payload>
        </dw:transform-message>

        <logger message="*********************Writing File Now****************************" level="INFO" doc:name="Logger"/>
        <file:outbound-endpoint path="D:\Accounts\RB\POC" outputPattern="RESPONSE.csv" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <logger message="*********************End Transforming &amp; Writing ****************************" level="INFO" doc:name="Logger"/>
    </flow>
</mule>
CSV Comparison (grammar) MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • Mastering Redirects With Cloudflare Bulk Redirects
  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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: