Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
dgreenx
Creator
Creator

How to use List in Talend Var data types or List of Value in Context variables?

Hello, all;

I am looking for some guidance on using (loading, processing) the data type "List" in the tMap Variables (middle section of tMap).

I am also looking for some guidance on using (loading, processing) the data type "List of Value" in the context variables. Is this the same data type?

Were these data types created just for JSON type processing? Can they be used for Array processing? Is this in the documentation somewhere and I just have not found it?

All of this is for trying to come up with a plan to load an array to process in a Custom Code Routine. I have written the routine (Class) to process through an array to return my calculated amount, but I need to find out how to load an array variable. Arrays are not shown as data types, but hopefully I can use the List data type or the List of Value data type to load the data and convert to an array and pass to the customer java class.

I wast hinking I could use tJavaRow to load the array variable, which would be ideal, but I have not found a way to create the array variable. Maybe I should just use tJavaFlex and create the array variable in the Start Code, use it in the Main Code and remove or ignore it in the End Code. Is this the best way to go with arrays based on single rows of data? If so, I would still like to know how to use the List variables made available in Talend.

Thanks greatly,

dg

David Green

Labels (5)
6 Replies
Anonymous
Not applicable

Hi

List of values in context is used only to select context value before running job  (when checking context checkbox). for more details, please refer to this question.

We usually use a tJavaFlex to create a Java list in the Start code, add values from input row in the Main code, finally, convert the list to array if needed for used later on other component.

 

Regards

Shong

dgreenx
Creator
Creator
Author

Thanks, shong.

So that leaves out List from the Context perspective on what I want to do.

 

Now I just need an answer on how to use the List in the tMap Var section. Anyone know how to use a List there. Is it required to import the utils.List code to use it. I'm not a java programmer but I can research the internet for a solution if I know how I should use the variable data type List. I'm sure this is obvious to java programmers with experience with the List data type, but I just need a jump start to understand how to use it.

 

Thanks to all in advance,

dg

Anonymous
Not applicable

@david green​ 

if you use the List data type, the expression should be List value, for example, I write a customize function in a routine and call this function in the expression field of List Var

public class MyRoutine {

 

  /**

   * helloExample: not return value, only print "hello" + message.

   * 

   * 

   * {talendTypes} String

   * 

   * {Category} User Defined

   * 

   * {param} string("world") input: The string need to be printed.

   * 

   * {example} helloExemple("world") # hello world !.

   */

static List<String> list = new ArrayList<String>();

 

  public static List buildList(String name) {

   list.add(name);

   return list;

  

  }

}

 

0695b00000L2QSnAAN.png 

 

Regards

Shong

dgreenx
Creator
Creator
Author

Thanks again, shong.

 

So to use a Variable of type List, I need to import java.util.ArrayList and java.util.List.

 

Here is something I build based on your example above. It loads a List variable of size 52 (weeks of the year).

It is a list of integers to hold whole number quantities. You pass in an integer array and use a

for loop to load the list.

/**

    * Must import the following:

    * import java.util.ArrayList;

    * import java.util.List;

    *

    * Must create a list of the desired size since default is 10

    * prior to creating the method buildList

    *

    */

 

    static List<Integer> myList = new ArrayList<Integer>(52);

 

    public static List buildList(Integer[] myIntArray) {

      for (Integer i=0; i < 52; i++) {

      myList.add(i,myIntArray[i]);

      }

      return myList;

   }

 

This is not what I was hoping for. I was hoping for an easy way in Talend to

load the list variable without code but I ended up using code anyway.

Instead of using List, I used basic array processing. I used the tJavaFlex component.

In that component, I created my array in the Start Code section, loaded my array in the

Main Code section and did nothing in the End Code section. Then I created

a method to call from the tMap out code areas.

 

Thanks for all your help.

dgreenx
Creator
Creator
Author

 

0695b00000L2eqKAAR.png0695b00000L2ermAAB.png0695b00000L2et4AAB.png0695b00000L2euRAAR.png

Anonymous
Not applicable

Great, we usually create the list on tJavaFlex and add each row value to the list in main code.