Five Stars BRM
Five Stars

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 here but the screenshot that provided the answer was taken down.

 

Thanks in advance.

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Twelve Stars

Re: Row output in tJava

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

Rilhia Solutions
5 REPLIES
Twelve Stars TRF
Twelve Stars

Re: Row output in tJava

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

TRF
Twelve Stars

Re: Row output in tJava

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.

Rilhia Solutions
Five Stars BRM
Five Stars

Re: Row output in tJava

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.

Twelve Stars

Re: Row output in tJava

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

Rilhia Solutions
Five Stars BRM
Five Stars

Re: Row output in tJava

Thank you.  That worked like a charm.