The following examples show you how to read the contents of a flat file, and also how to write a flat file following the specified schema. Each example consists of a Mule application that uses DataWeave to read and write flat files.
When you work with flat files, configure the flat file schema to use for reading and writing flat file content. The flat file schema defines the structure of a flat file, and this enables Mule to write and read in this format.
These example applications are configured to work with the sample flat file data and the example flat file schema provided in Flat File Schemas.
The following example application consists of:
a flat file to JSON" />
" /> " outputMimeType='application/flatfile; schemapath=$'/> (1) /apps/$/$#["\n"]#[payload]' />
After its execution, the example application generates the following output:
< "Batch": [ < "TDR": [ < "Record Type": "BAT", "Sequence Number": 2, "Amount": 32876, "Account Number": "0123456789", "Batch Function": "D", "Type": "CR" >, < "Record Type": "BAT", "Sequence Number": 3, "Amount": 87326, "Account Number": "0123456788", "Batch Function": "D", "Type": "CR" >], "BCF": < "Record Type": "BAT", "Sequence Number": 4, "Batch Transaction Count": 2, "Batch Transaction Amount": 120202, "Unique Batch Identifier": "A000000001", "Batch Function": "T", "Type": "CR" >, "BCH": < "Record Type": "BAT", "Company Name": "ACME RESEARCH", "Sequence Number": 1, "Unique Batch Identifier": "A000000001", "Batch Function": "H" >>, < "TDR": [ < "Record Type": "BAT", "Sequence Number": 6, "Amount": 3582, "Account Number": "1234567890", "Batch Function": "D", "Type": "DB" >, < "Record Type": "BAT", "Sequence Number": 7, "Amount": 256, "Account Number": "1234567891", "Batch Function": "D", "Type": "CR" >], "BCF": < "Record Type": "BAT", "Sequence Number": 8, "Batch Transaction Count": 2, "Batch Transaction Amount": 3326, "Unique Batch Identifier": "A000000002", "Batch Function": "T", "Type": "DB" >, "BCH": < "Record Type": "BAT", "Company Name": "AJAX EXPLOSIVES", "Sequence Number": 5, "Unique Batch Identifier": "A000000002", "Batch Function": "H" >> ], "RQF": < "Record Type": "RQF", "File Batch Count": 2, "File Transaction Count": 8, "File Transaction Amount": 116876, "Unique File Identifier": "A000000001", "Type": "CR" >, "RQH": < "Record Type": "RQH", "File Creation Time": "10:10:00", "File Creation Date": "2018-09-01", "Currency": "USD", "Unique File Identifier": "A000000001" >>
The following example application contains:
a flat file" />
" /> "/> (1) , "File Creation Time": payload.RQH."File Creation Time" as Time, "Unique File Identifier": payload.RQH."Unique File Identifier", Currency: payload.RQH.Currency >, Batch: payload.Batch map ( batch , indexOfBatch ) -> < BCH: < "Record Type": batch.BCH."Record Type", "Sequence Number": batch.BCH."Sequence Number", "Batch Function": batch.BCH."Batch Function", "Company Name": batch.BCH."Company Name", "Unique Batch Identifier": batch.BCH."Unique Batch Identifier">, TDR: batch.TDR map (tdr, indexOfTdr) -> < "Record Type": tdr.TDR."Record Type", "Sequence Number": tdr.TDR."Sequence Number", "Batch Function": tdr.TDR."Batch Function", "Account Number": tdr.TDR."Account Number", "Amount": tdr.TDR.Amount, "Type": tdr.TDR.'Type' >, BCF: < "Record Type": batch.BCF."Record Type", "Sequence Number": batch.BCF."Sequence Number", "Batch Function": batch.BCF."Batch Function", "Batch Transaction Amount": batch.BCF."Batch Transaction Amount", "Type": batch.BCF.'Type', "Batch Transaction Count": batch.BCF."Batch Transaction Count", "Unique Batch Identifier": batch.BCF."Unique Batch Identifier">>, RQF: < "Record Type": payload.RQF."Record Type", "File Batch Count": payload.RQF."File Batch Count", "File Transaction Count": payload.RQF."File Transaction Count", "File Transaction Amount": payload.RQF."File Transaction Amount", Type: payload.RQF.Type, "Unique File Identifier": payload.RQF."Unique File Identifier" >> ]]> /apps/$/$#["\n"]#[payload]'/> (2)
After its execution, the example application generates the following output:
RQH201809011010A000000001USD BAT000001HACME RESEARCH A000000001 BAT000000D 0000000000 BAT000000D 0000000000 BAT000004T0000120202CR000002A000000001 BAT000005HAJAX EXPLOSIVES A000000002 BAT000000D 0000000000 BAT000000D 0000000000 BAT000008T0000003326DB000002A000000002 RQF0002000008000000116876CRA000000001
If you want to test the examples locally, follow these steps to create and configure your example Mule application:
fileReadPath=flatfile schemaPath=flatfileschema.ffd fileWritePath=flatfilenew jsonFileReadPath=flatFileJson.json fileWorkingDir=#["$/apps/$/"]