[resolved] Use Multiple outputs with tJavaRow

Five Stars

[resolved] Use Multiple outputs with tJavaRow

Hi everyone,
I am trying to get the data from a flat file (in this case there are no delimiters, they are fixed rows), for example:

DATEVALUEDESCRIPTION
2017020320000EFFECTIVEPAY

In a tJavaRow I am applying some validations of structure and depending on the result, I want to send the data to insert it into an ORACLE database. So, for example, I validate that the text "20170203" is a valid date and if it is a valid date I want to send it by an output X to store it with an prepared statement in some table, but if it is not a valid date then I want to send it by another Output to perform an insert with another component in a different table that will log the errors. However, I can not find the way to specify more than one output with the componet.

Could anyone help me, please?


Accepted Solutions
Five Stars

Re: [resolved] Use Multiple outputs with tJavaRow

rana.abhimanyu90 wrote:
As rhall_2.0 has mentioned using tmap variables, you can set flags(boolean vars) based on your validation requirements and create multiple outputs for tmap and based on those flag values for each row pass the value in respective output flows.

To everyone, thank you very much. I applied all your advices, it works fine... But rhall_2.0, I decided put all the validations into a tJavaRow, it seems easier... maybe it's not good for portability but I can manage the Exceptions easily.





All Replies
Moderator

Re: [resolved] Use Multiple outputs with tJavaRow

Hi,
From your description, you can use connection type "RunIf" which triggers a subjob or component in case the condition defined is met.
tfileinput-->tjavarow-->runif(valid data)-->inputrow-->output
                              -->runif(invalid data)-->inputrow-->output_2
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Ten Stars

Re: [resolved] Use Multiple outputs with tJavaRow

There is an easy way of doing this. First of all, don't use the tJavaRow for your validation logic. I'm assuming you are doing this because you have a Java background. If so, wrap your logic into a routine (a Java class using a static methods) and validate the data inside a tMap. This allows multiple outputs where you can easily specify logic to indicate exactly where you want the data to go. This tutorial from Talend might help https://help.talend.com/search/all?query=tMap
Five Stars

Re: [resolved] Use Multiple outputs with tJavaRow

xdshi wrote:
Hi,
From your description, you can use connection type "RunIf" which triggers a subjob or component in case the condition defined is met.
tfileinput-->tjavarow-->runif(valid data)-->inputrow-->output
                              -->runif(invalid data)-->inputrow-->output_2
Best regards
Sabrina

Hi xdshi.
Well, I know I can run a subjob with a tRunJob, but I'm not sure how to transfer the output data from a tJavaRow to a tRunJob. I had to use the runIf invoking a new File. Please see my screenshot

I obtain the list files with the tFileList, then I load the file in the tFileInputPositional with a ((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")) and the data validation is done with the JavaRow (rhall says that I can do the validations in a tMap, but tMap use the minimum java knowledge and I need import librarys such as java.util.regex or java.text.SimpleDateFormat) so, after the validations, in order to get the data from the tJavaRow, I just had to reload the file to get the line I need to insert in ORACLE.

Thank you very much for your help... However, I need find another solution.
Five Stars

Re: [resolved] Use Multiple outputs with tJavaRow

rhall_2.0 wrote:
There is an easy way of doing this. First of all, don't use the tJavaRow for your validation logic. I'm assuming you are doing this because you have a Java background. If so, wrap your logic into a routine (a Java class using a static methods) and validate the data inside a tMap. This allows multiple outputs where you can easily specify logic to indicate exactly where you want the data to go. This tutorial from Talend might help https://help.talend.com/search/all?query=tMap

Hi rhall, Thanks for your answer.
Just as I told xdshi, I can't implement complex Java logic with the tMap (at least, I don't know how). I can work with a routine made by me, but I can't specify the output into a tMap (If the data is valid, then send it to the tMap output X, if the data is invalid, then send it to the output Y)... That is what I got from your answer.
Ten Stars

Re: [resolved] Use Multiple outputs with tJavaRow

All of your logic can be contained with a tMap. Take a look at the link I posted n my last post. That will show you how you can push your data in different directions with a tMap. With regard to your complex logic, you can write routines (Java classes with static methods) which can be called inside the tMaps. You have the concept of tMap variables which are processed from top to bottom and hold their values between rows (very important and useful to remember). You can use these to call your routine methods. 
Ten Stars

Re: [resolved] Use Multiple outputs with tJavaRow

Can you add a column to the tJavaRow output that specifies the "direction" you want it to go?  If so, it would be a simple matter to split the output using a tMap with a simple conditional on your new column.
Six Stars

Re: [resolved] Use Multiple outputs with tJavaRow

As rhall_2.0 has mentioned using tmap variables, you can set flags(boolean vars) based on your validation requirements and create multiple outputs for tmap and based on those flag values for each row pass the value in respective output flows.
Five Stars

Re: [resolved] Use Multiple outputs with tJavaRow

rana.abhimanyu90 wrote:
As rhall_2.0 has mentioned using tmap variables, you can set flags(boolean vars) based on your validation requirements and create multiple outputs for tmap and based on those flag values for each row pass the value in respective output flows.

To everyone, thank you very much. I applied all your advices, it works fine... But rhall_2.0, I decided put all the validations into a tJavaRow, it seems easier... maybe it's not good for portability but I can manage the Exceptions easily.




Five Stars

Re: [resolved] Use Multiple outputs with tJavaRow

dazbellboy wrote:
rana.abhimanyu90 wrote:
As rhall_2.0 has mentioned using tmap variables, you can set flags(boolean vars) based on your validation requirements and create multiple outputs for tmap and based on those flag values for each row pass the value in respective output flows.

To everyone, thank you very much. I applied all your advices, it works fine... But rhall_2.0, I decided put all the validations into a tJavaRow, it seems easier... maybe it's not good for portability but I can manage the Exceptions easily.
https://www.talendforge.org/forum/img/members/411227/mini_blob_20170323-1857.png

https://www.talendforge.org/forum/img/members/411227/mini_blob_20170323-1858.png

https://www.talendforge.org/forum/img/members/411227/mini_blob_20170323-1900.png

That's how I choose the output in the tmap:
Ten Stars

Re: [resolved] Use Multiple outputs with tJavaRow

It really is up to you. I was being very literal and thought you wanted to do it all in one component. You can do it all with the tMap but it might be easier to understand doing it your way :-)

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog