Iterating Over a Java Collection with Talend Open Studio
When working with a third-party library of Java code in Talend Open Studio, you might have need to work with Java Collections. This post shows how to pass a java.util.List around a Talend job, loop over its contents, and print them.
You can integrate systems with Talend Open Studio via third-party libraries ("jar files"). For instance, a method call like this
List loanAdapter.getAllProviders()
might be used to return a java.util.List of objects that you can format, transport, or manipulate with Talend Open Studio. While the underlying implementation of loanAdapter might call a database or a web service -- things you can do conveniently with Talend Open Studio -- there may be additional business logic, parameter handling, or other difficulty in breaking apart the composed functionality of such a class. In this case, it may be easier to call a Java API.
This job creates a java.util.List, loops over the List, and outputs the results. It's a demo job. A real job might involve a database logging call or a web service call to request a loan application review.
tSetGlobalVar
The job starts with a tSetGlobalVar component. You may have seen this as a way to set up a simple structure like a file name (String), but you can put any Java object in the globalMap, including a List.
I'm using a fully-qualified class name here. There are a few components like tJava that will allow you to slip in an "import java.util.*" to shorten the syntax.
tJava_1
tJava_1 fills the list with the values A, B, and C. If you'd like to see a more interesting post that fills up a List, visit this post.
tLoop
There are several ways to iterate over a List in Java: an Iterator, enhanced for loop. I'm using a counter that is increased with each iteration. Be sure to use -1 on the size() for the end of the loop. Java Collections are zero-based.
tJava_2
This is the part of the program that actually does something. In a real job, this would be the starting point for processing which could include loading a file, calling a web service, adding a message to a queue, or writing a record to a database. My example simply "printlns". Note the assignment at the top of the screenshot to save the typing and retrieving a variable from the globalMap.
This simple job is best written by using the off-the-shelf Talend components by running a tForEach into a tLogRow. However, this example's purpose is to show you how to manipulate a Java Collection. While it's not too useful to print out A, B, and C, I've integrated third party libraries in Talend , and if you had one that returned a Java Collection like a List, you may need to do something like this.
For another look at data structures, be sure to check out these posts
You can integrate systems with Talend Open Studio via third-party libraries ("jar files"). For instance, a method call like this
List
might be used to return a java.util.List of objects that you can format, transport, or manipulate with Talend Open Studio. While the underlying implementation of loanAdapter might call a database or a web service -- things you can do conveniently with Talend Open Studio -- there may be additional business logic, parameter handling, or other difficulty in breaking apart the composed functionality of such a class. In this case, it may be easier to call a Java API.
This job creates a java.util.List, loops over the List, and outputs the results. It's a demo job. A real job might involve a database logging call or a web service call to request a loan application review.
Job Iterating Over a Collection and Printing A, B, and C |
Creating a List Object and Saving it under "mylist" |
tJava_1
tJava_1 fills the list with the values A, B, and C. If you'd like to see a more interesting post that fills up a List, visit this post.
Adding Items to a List |
There are several ways to iterate over a List in Java: an Iterator, enhanced for loop. I'm using a counter that is increased with each iteration. Be sure to use -1 on the size() for the end of the loop. Java Collections are zero-based.
Defining a Loop in Talend Open Studio |
This is the part of the program that actually does something. In a real job, this would be the starting point for processing which could include loading a file, calling a web service, adding a message to a queue, or writing a record to a database. My example simply "printlns". Note the assignment at the top of the screenshot to save the typing and retrieving a variable from the globalMap.
This simple job is best written by using the off-the-shelf Talend components by running a tForEach into a tLogRow. However, this example's purpose is to show you how to manipulate a Java Collection. While it's not too useful to print out A, B, and C, I've integrated third party libraries in Talend , and if you had one that returned a Java Collection like a List, you may need to do something like this.
For another look at data structures, be sure to check out these posts
No comments:
Post a Comment