Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Row output in tJava

I have a tJava component that connects to an FTPS site.  I want to direct the listFiles() method to output rows in talend.  I don't think I can use tJavaRow (because the connect code would run every time) and also not tJavaFlex (because the fileList method is inside a loop in the java code).

 

Here is my Java code:

FTPFile[] files = ftpClient.listFiles("/myDir");
// iterate over the files
for (FTPFile file : files) {
String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + file.getTimestamp().getTime();
System.out.println(details); 
}

I want to put Name, size and date into rows defined by a schema that I can use later in my job.

 

This question was asked and answered by Shong https://community.talend.com/t5/Design-and-Development/tJava-to-output-multiple-rows/td-p/97918 but the screenshot that provided the answer was taken down.

 

Thanks in advance.

Labels (4)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Using your code as an example, this is how it would work....

Start Code

FTPFile[] files = ftpClient.listFiles("/myDir");
// iterate over the files
for (FTPFile file : files) {

Main Code

String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + file.getTimestamp().getTime();
System.out.println(details); 

//code to output the data to a row. Assuming row is "row1" and column is "details"
row1.details = details;

 

 End Code

}


The Main Code is run for every iteration of the loop. A row (in the example above "row1") is produced for every iteration.

I hope that helps

View solution in original post

6 Replies
TRF
Champion II
Champion II

Hi,
You should use a Java list to store the data issued from the ftpClient.listFiles.
For a complete example showing how to load the list into a global variable and how to retrieve the content later, have look at http://bekwam.blogspot.fr/2012/08/iterating-over-java-collection-with.html?m=1
Anonymous
Not applicable
Author

OK, I see one thing I have to question here.....but I am open to the possibility that there is a good reason for this choice. Why are you using a tJava to connect to an FTP site? Talend provides components to do this. It really isn't good practice to "reinvent the wheel" without a good reason. Using the Talend FTP components may mean you do not need to worry about generating rows this way. Ofcourse, I am open to the possibility that there is a good reason to do this, but I would be remiss if I did not mention this.

 

With regard to finding a way of generating rows, the tJavaFlex is perfect for this. I am not entirely sure what you meant by "because the fileList method is inside a loop in the java code" since the tJavaFlex is essentially a component that implements looping driven by you (if you are outputting data) or the data flow (if you are receiving data). Take a look at this unrelated tutorial (https://www.rilhia.com/quicktips/quick-tip-row-multiplication) to see how you can use the tJavaFlex. Essentially, the Start Code section is where you would start your loop and define your variables. The Main Code section is where your inner loop code goes. The End Code section is where your close loop and finish code goes. From looking at your code example you would quite easily be able to implement that using a tJavaFlex.

Anonymous
Not applicable
Author

Its an FTPS site and I just could not get the standard Talend components to work.  I tried hard, but I didn't have things like key files and so on.  I didn't need any of that to connect with my FTP client like FileZilla.  Then I tried tJava and had it working in a couple of hours, no key files required.

 

So what would the tJavaFlex code look like?  I get how the "Start code" would be the part that connects to the FTP site, but the part that lists files is itself a loop.  Would the "Main code" section of tJavaFlex be the part that begins with:  for (FTPFile file : files) {

or would this loop originator go in the start code and just the body of the loop in Main code?

 

Its an appealing suggestion because then I would have flows that I could use standard components on.

Anonymous
Not applicable
Author

Using your code as an example, this is how it would work....

Start Code

FTPFile[] files = ftpClient.listFiles("/myDir");
// iterate over the files
for (FTPFile file : files) {

Main Code

String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + file.getTimestamp().getTime();
System.out.println(details); 

//code to output the data to a row. Assuming row is "row1" and column is "details"
row1.details = details;

 

 End Code

}


The Main Code is run for every iteration of the loop. A row (in the example above "row1") is produced for every iteration.

I hope that helps

Anonymous
Not applicable
Author

Thank you.  That worked like a charm.

rnathan2020
Contributor
Contributor

Thanks for this. I have always had this question and today it was resolved. I just completed processing a  COBOL file using jRecord and emit multiple lines.