Creating recursive loops from a flat file - 7.3

Talend Data Mapper Functions Reference Guide

Version
7.3
Language
English
Product
Talend Big Data Platform
Talend Data Fabric
Talend Data Management Platform
Talend Data Services Platform
Talend MDM Platform
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Design and Development > Designing Jobs
Last publication date
2023-01-05

Use a FlatToHierarchyLoop function to create recursive loops based on levels defined in a flat file.

Before you begin

You have created a map with an input and an output structure. You can use the samples provided below.

About this task

In this example you have a CSV file describing a table of contents with titles, page numbers and numbers corresponding to the level of each element. You want to create an XML file in which elements are nested based on their level. You can use the following sample as input:
title,page,level
Introduction,3,1
Part 1,10,1
Chapter 1,10,2
Section 1,10,3
Section 2,31,3
Section 3,56,3
Chapter 2,80,2
Section 1,80,3
Section 2,102,3
Part 2,125,1
Chapter 1,125,2
Chapter 2,160,2
Section 1,160,3
Section 2,185,3
Conclusion,213,1
You can use the following XML sample to create your output structure:
<contents>
	<entry>
		<title/>
		<page/>
	</entry>
	<entry>
		<title/>
		<page/>
	</entry>
</contents>

Procedure

  1. Click the output entry element and drag and drop a FlatToHierarchyLoop function in its Loop tab.
  2. Drag and drop the input row element on the Input Map Element argument.
  3. Drag and drop the input level element on the Level Number element.
    The function uses the number in the level column of the input file to determine how to structure the recursive loops in the output.
  4. Drag and drop the input row element on the output entry element to automatically map title and page.

Results

Your map is configured and should look like this:
You can use the Test Run feature to see the result. In this example, the following output is returned:
<contents>
  <entry>
    <title>Introduction</title>
    <page>3</page>
  </entry>
  <entry>
    <title>Part 1</title>
    <page>10</page>
    <entry>
      <title>Chapter 1</title>
      <page>10</page>
      <entry>
        <title>Section 1</title>
        <page>10</page>
      </entry>
      <entry>
        <title>Section 2</title>
        <page>31</page>
      </entry>
      <entry>
        <title>Section 3</title>
        <page>56</page>
      </entry>
    </entry>
    <entry>
      <title>Chapter 2</title>
      <page>80</page>
      <entry>
        <title>Section 1</title>
        <page>80</page>
      </entry>
      <entry>
        <title>Section 2</title>
        <page>102</page>
      </entry>
    </entry>
  </entry>
  <entry>
    <title>Part 2</title>
    <page>125</page>
    <entry>
      <title>Chapter 1</title>
      <page>125</page>
    </entry>
    <entry>
      <title>Chapter 2</title>
      <page>160</page>
      <entry>
        <title>Section 1</title>
        <page>160</page>
      </entry>
      <entry>
        <title>Section 2</title>
        <page>185</page>
      </entry>
    </entry>
  </entry>
  <entry>
    <title>Conclusion</title>
    <page>213</page>
  </entry>
</contents>