Example of mapping a multiple-record-type flat file - 6.3

Talend Data Mapper User Guide

EnrichVersion
6.3
EnrichProdName
Talend Big Data Platform
Talend Data Fabric
Talend Data Management Platform
Talend Data Services Platform
Talend MDM Platform
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend Studio

The following example shows how to create a map that writes from a simple multiple-record-type flat file to another multiple-record-type flat file. It shows how to set up the structures manually and uses features like terminator and initiator in the mapping.

Create the input structure

  1. In the Mapping perspective, in the Data Mapper view, expand the Hierarchical Mapper node, right-click Structures, click New and then click Structure.

  2. In the [New Structure] dialog box that opens, select Create a new structure where you manually enter elements, and then click Next.

  3. Name the structure multiple_record_input, and then click Next.

  4. Select the Flat Files (including csv) representation, and then click Finish.

Create the elements of A_Record

  1. Right-click in the multiple_record_input structure you just created, and then click New Element to create a new element and name it Root.

  2. Right-click the Root element, and then click New Element.

    Enter A_Record in the Name field and A in the Initiator field. Do not select the Include Initiator check box.

  3. Right-click the A_Record element, and then click New Element.

    Name this new element Text and set the values of the Size Min/Max fields to 1 and 10 respectively.

  4. Right-click the A_Record element, and then click New Element.

    Name this new element Number, set the values of the Size Min/Max fields to 1 and 5 respectively, and then enter \n in the Terminator field.

Create the elements of B_Record

  1. Right-click the A_Record element, and then click New Element.

    Enter B_Record in the Name field and B in the Initiator field, and then enter -1 for Max in the Occurs Min/Max fields to make it loop.

  2. Right-click the B_Record element, and then click New Element.

    Name this new element Text and set the values of the Size Min/Max fields to 1 and 15 respectively.

  3. Right-click the B_Record element, and then click New Element.

    Name this new element Code, set the values of the Size Min/Max fields to 1 and 2 respectively, and then enter \n in the Terminator field.

Create the elements of C_Record

  1. Right-click the B_Record element, and then click New Element.

    Enter C_Record in the Name field and C in the Initiator field, and then enter -1 for Max in the Occurs Min/Max fields to make it loop.

  2. Right-click the C_Record element, and then click New Element.

    Name this new element Quantity, set the values of the Size Min/Max fields to 1 and 5 respectively, and then select Integer (32) for the Data Type field.

  3. Right-click the C_Record element, and then click New Element.

    Name this new element Filler, and then enter \n in the Terminator field.

Create the elements of D_Record

  1. Right-click the D_Record element, and then click New Element.

    Enter D_Record in the Name field and D in the Initiator field, and then enter -1 for Max in the Occurs Min/Max fields to make it loop.

  2. Right-click the D_Record element, and then click New Element.

    Name this new element Unit_Price, set the values of the Size Min/Max fields to 1 and 5 respectively, and then select Decimal for the Data Type field.

  3. Right-click the D_Record element, and then click New Element.

    Name this new element Filler, and then enter \n in the Terminator field.

Create the elements of Z_Record

  1. Right-click the Root element, and then click New Element.

    Enter Z_Record in the Name field and Z in the Initiator field.

  2. Right-click the Z_Record element, and then click New Element.

    Name this new element Text, and then set the values of the Size Min/Max fields to 1 and 12 respectively.

  3. Right-click Z_Record, and then click New Element.

    Name this new element Filler, and then enter \n in the Terminator field.

  4. Press CTRL+S to save your structure.

Select a sample document

  1. Copy the following into a text file and save it under the name mutiple_record_type.txt.

    ATexthdr   00001
    BTextdetail1    01
    C00010
    D00999
    BTextdetail2    02
    C00001
    D00100
    ZTextFooter 
  2. Click the arrow next to the Show Document button, and then click Select Sample Document > Import Document from File.

  3. In the [Select Input Document Instance] dialog box, click Browse and then browse to the directory where you saved the file multiple_record_type.txt.

  4. Select the multiple_record_type.txt file in the dialog box, and then click Finish.

Create the output structure

  1. In the Mapping perspective, in the Data Mapper view, expand the Hierarchical Mapper node, right-click Structures, click New and then click Structure.

  2. In the [New Structure] dialog box that opens, select Create a new structure where you manually enter elements, and then click Next.

  3. Name the structure multiple_record_outut, and then click Next.

  4. Select the Flat Files (including csv) representation, and then click Finish.

Create the elements of AB_Record

  1. Right-click in the multiple_record_output structure you just created, and then click New Element.

    Name this new element Root.

  2. Right-click the Root element, and then click New Element.

    Enter AB_Record in the Name field.

  3. Right-click the AB_Record element, and then click New Element.

    Name this new element Type and set the values of each of the Size Min/Max fields to 2.

  4. Right-click the AB_Record element, and then click New Element.

    Name this new element Text, set the values of the Size Min/Max fields to 1 and 10 respectively, and enter \n in the Terminator field.

Create the elements of AC_Record

  1. Right-click the AB_Record element, and then click New Element.

    Name this new element AC_Record, and then enter -1 for Max in the Occurs Min/Max fields to make it loop.

  2. Right-click the AC_Record element, and then click New Element.

    Name this new element Type and set the values of each of the Size Min/Max fields to 2.

  3. Right-click the AC_Record element, and then click New Element.

    Name this new element Text, set the values of the Size Min/Max fields to 1 and 15 respectively.

  4. Right-click the AC_Record element, and then click New_Element.

    Name this new element Quantity, set the values of each of the Size Min/Max fields to 5, and then select Integer (32) for the Data Type field.

  5. Right-click the AC_Record element, and then click New Element.

    Name this new element Total_Price, set the values of each of the Size Min/Max fields to 5, and then select Integer (32) for the Data Type field.

  6. Right-click the AC_Record element, and then click New Element.

    Name this new element Code_Text, set the values of the Size Min/Max fields to 1 and 20 respectively, and enter \n in the Terminator field.

Create the elements of AZ_Record

  1. Right-click the Root element, and then click New Element.

    Name this new element AZ_Record.

  2. Right-click the AZ_Record element, and then click New Element.

    Name this new element Type and set the values of each of the Size Min/Max fields to 2.

  3. Right-click the AZ_Record element, and then click New Element.

    Name this new element Text, and then set the values of each of the Size Min/Max fields to 12.

  4. Right-click the AZ_Record element, and then click New Element.

    Name this new element Total_Value, set the values of each of the Size Min/Max fields to 7, select Decimal for the Data Type field, and enter \n in the Terminator field.

  5. Press CTRL+S to save your structure.

Create the map

  1. In the Data Mapper view, expand the Hierarchical Mapper node, right-click Maps, click New and then click Map.

  2. Select Standard Map - Maps instances of an input structure to an output structure, and then click Next.

  3. Name the map multiple_record_type, and then click Finish.

Build the mapping expressions

  1. In the map that opens, drag the multiple_record_input structure from the Data Mapper view and drop it into the Input side of the map.

  2. Drag the multiple_record_output structure and drop it into the Output side of the map.

  3. Under the A_Record element on the Input side, drag and drop the input element Text to the output element Text that is under the AB_Record element on the Output side.

  4. Under the B_Record element on the Input side, drag and drop the input element Text to the output element Text that is under the AC_Record element on the Output side.

  5. Under the B_Record element on the Input side, drag and drop the input element Code to the output element Code_Text that is under the AC_Record element on the Output side.

  6. Under the C_Record element on the Input side, drag and drop the input element Quantity to the output element Quantity that is under the AC_Record element on the Output side, and then click Yes when asked if you want to change the output map element looping.

  7. Under the Z_Record element on the Input side, drag and drop the input element Text to the output element Text that is under the AZ_Record element on the Output side.

Add functions

  1. Click the Total_Price element under the AC_Record element on the Output side to select it.

  2. In the Functions view on the left, expand the Arithmetic folder, and then drag the Multiply function and drop it onto the Value tab of the Total_Price element.

  3. Drag the Quantity element from under the C_Record element on the Input side and drop it into First Value - Drop/paste in the Multiply function on the Value tab.

  4. Drag the Unit_Price element from under the D_Record element on the Input side and drop it into Second Value - Drop/paste in the Multiply function on the Value tab, and then click Yes when asked if you want to change the output map element looping.

  5. Click the Type element under the AB_Record element on the Output side to select it.

  6. In the Functions view, expand the General folder, and then drag the Constant function and drop it onto the Value tab of the Type element.

  7. Double-click the Constant function in the Value tab, enter AB in the Value field of the [Expression Constant Properties] dialog box that opens, and then click OK.

  8. Click the Type element under the AC_Record element on the Output side to select it.

  9. In the Functions view, expand the General folder, and then drag the Constant function and drop it onto the Value tab of the Type element.

  10. Double-click the Constant function in the Value tab, enter AC in the Value field of the [Expression Constant Properties] dialog box that opens, and then click OK.

  11. Click the Type element under the AZ_Record element on the Output side to select it.

  12. In the Functions view, expand the General folder, and then drag the Constant function and drop it onto the Value tab of the Type element.

  13. Double-click the Constant function in the Value tab, enter AZ in the Value field of the [Expression Constant Properties] dialog box that opens, and then click OK.

  14. Click the Total_Value element under the AZ_Record element on the Output side to select it.

  15. In the Functions view, expand the Aggregate folder, and then drag the AgSum function and drop it onto the Value tab of the Total_Value element.

  16. Expand the Arithmetic folder, and then drag the Multiply function and drop it on the AgSum function.

  17. Click the AC_Record element on the Output side to select it.

  18. On the Loop tab, right click SimpleLoop and then click Copy.

  19. Return to the Total_Value element under the AZ_Record on the Output side and, on the Value tab, under the Multiply function, right-click Loop Expression - Drop/paste here, and then click Paste.

  20. Drag the Quantity element under the C_Record element on the Input side and drop it onto First Value - Drop/paste here, under the Multiply function on the Value tab.

  21. Drag the Unit_Price element under the D_Record element on the Input side and drop it onto Second Value - Drop/paste here, under the Multiply function on the Value tab.

  22. Press CTRL+S to save your changes.

Test run

  1. Right-click the Root element on the Output side and click Test Run. This runs the map using the sample document you created earlier as the input.

  2. Check the result compares to the screen shot below.