1

I'm working with tmap and I was wondering if it's possible to use this simple use case:

I create a variable in central tmap section, and I apply the same variable to multiple output fields.

enter image description here

In the above example, I have two String fields and I want to convert them to Upper case. Is it possible, via the use Variables, to write the function once and then call it in every output fields I want?

Cause otherwise the only way to do that is to re-write the function StringHandling.UPCASE( ) in every field in the output table.

I've tried to use the following syntax, but didn't work. Also in the talend community or forums online I didn't find a solution, maybe because there isn't any?

[![enter image description here](https://i.stack.imgur.com/xIPQ9.png)](https://i.stack.imgur.com/xIPQ9.png)

  • You can’t do it in tMap but you can do it using tJavaFlex in a subsequent step. That way, you can change your schema and add fields and you don’t have to worry about the schema changing, it will automatically deal with any new fields. – Skin Mar 23 '23 at 20:26

3 Answers3

3

Hi instead of having to rewrite the code in every field there is a functionnality in the tMap component that allows you to apply a routine or functions to the fields that you selected, you just have to select the field you want the function to be applied and then right click and you have the option to "apply routine" it looks like this

enter image description here

And it applies the function / routine to the field you selected automaticaly.

It works with talend standard routines but also with the custom routines you write

enter image description here

Emile Dadou
  • 307
  • 2
  • 12
1

Sadly there isn't a way to achieve what you want, without calling the function TalendString.UPCASE in every expression in your output fields.

Ibrahim Mezouar
  • 3,981
  • 1
  • 18
  • 22
0

For anyone wanting to do this in bulk, there is a way but it's not via the use of tMap.

Using one of the tJava# components, you can use Reflection in Java to do it dynamically for all fields of type String.

This can be applied to any part of the transformation flow.

This is a basic example ...

Flow

The first step initialises 3 rows of data that look like thus ...

Fixed Flow Input

The main tJavaFlex component looks like this (noting that the out connector for this component is named data_out) ...

Start code

Class outClass = data_out.getClass();
java.lang.reflect.Field[] outFields = outClass.getDeclaredFields();

Main code

for (int i = 0; i < outFields.length; i++) {
    java.lang.reflect.Field field = outFields[i];

    String fieldType = field.getType().getSimpleName();
    
    if (fieldType.equals("String")) {
        field.setAccessible(true);  
    
        String value = (String)field.get(data_out);
        
        if (value != null) {
            String stringValue = value.toUpperCase();   
            field.set(data_out, stringValue);
        }
    }
}

... it's also important to note that the Data Auto Propogate flag should be switched on. This will automatically fill the output structure which means you won't have to. All the tJavaFlex component needs to do is uppercase each string value.

Output

Starting job DynamicUppercase at 09:06 24/03/2023.
[statistics] connecting to socket on port 3613
[statistics] connected
.------+------+-------+-------+--------+------+------+-------.
|                           Before                           |
|=-----+------+-------+-------+--------+------+------+------=|
|Text1 |Text2 |Number1|Number2|Boolean1|Text3 |Text4 |Number3|
|=-----+------+-------+-------+--------+------+------+------=|
|Test 1|test 2|123    |456    |true    |test 3|Test 4|789    |
|Test 1|test 2|123    |456    |true    |test 3|Test 4|789    |
|Test 1|test 2|123    |456    |true    |test 3|Test 4|789    |
'------+------+-------+-------+--------+------+------+-------'

.------+------+-------+-------+--------+------+------+-------.
|                           After                            |
|=-----+------+-------+-------+--------+------+------+------=|
|Text1 |Text2 |Number1|Number2|Boolean1|Text3 |Text4 |Number3|
|=-----+------+-------+-------+--------+------+------+------=|
|TEST 1|TEST 2|123    |456    |true    |TEST 3|TEST 4|789    |
|TEST 1|TEST 2|123    |456    |true    |TEST 3|TEST 4|789    |
|TEST 1|TEST 2|123    |456    |true    |TEST 3|TEST 4|789    |
'------+------+-------+-------+--------+------+------+-------'

[statistics] disconnected

Job DynamicUppercase ended at 09:06 24/03/2023. [Exit code  = 0]
Skin
  • 9,085
  • 2
  • 13
  • 29