How to modify a context variable in a component and keep it later on ?

Highlighted
Five Stars

How to modify a context variable in a component and keep it later on ?

Hello.

 

in my process, I'm trying to create an output file, named with some context variables.

This variables are defined in general context by default value "". 

I modify their values in tJavaRow and want to use the new value in tFileOutpuMSXML.

 

Here is tJavaRow description : I modify context.idDoc and context.currentDate.

tJavaRow.PNG

 

And in tFileOutputMSXML, I set the file name like this (context.path_OUT is also a variable but only set in global context).

 

tFileOutputMSXML.PNG

 

Result is that file name is only "_.xml" . None of the values were reported on the tFileOutputMSXML.

How should I proceed to create the right output file ? 


Accepted Solutions
Employee

Re: How to modify a context variable in a component and keep it later on ?

Hi,

 

Attached a skeleton flow which can help in your use case.

image.png

 

 

You need to select the number of rows in row generator as 1. This will make sure that for each incoming record, a new flow with one record is generated in each iteration. 

 

In the tjavarow, you can fetch the columns coming out of Oracle DB by giving Control+Space and search based on tFlowToIterate component name. All the components will be tagged to tFlowtoTerate and you can use this data to generate multiple files.

 

     I would suggest to start with a simple scenario of tFlowtoIterate (mentioned in help documents) to understand the functionalities of the component and then build your use case after that.

 

Warm Regards,

 

Nikhil Thampi


All Replies
Employee

Re: How to modify a context variable in a component and keep it later on ?

Hi,

 

     Could you please verify whether the data is correctly getting loaded to the context variables by printing using a String.out in tjavarow. 

 

      Please also note that tjavarow will process the data at row level but tFileOutputMSXML will produce a single file for all the records unless you add the iteration before that component.

 

      Could you please verify these details and let us know the update?

 

Warm Regards,

 

Nikhil Thampi

Five Stars

Re: How to modify a context variable in a component and keep it later on ?

Hi.
Thanks for your answer.

 

So, I added these lines in the tJavaRow code :

System.out.println(context.idDoc);
System.out.println(context.currentDate);

 

And in the logs, I get :

...
80
2018-08-14

...

These are expected values.

Community Manager

Re: How to modify a context variable in a component and keep it later on ?

If you want to name a file using a context variable you MUST have the context variable's value set BEFORE the subjob with the file component has started. This is because the file output components create the files at the beginning of the subjob. 

 

If you need to set data into multiple files, then you will need to either use a tLoop or a tFlowToIterate component. In these cases you will still need to set the context variable before the subjob (or the loop or iteration) starts. 

Five Stars

Re: How to modify a context variable in a component and keep it later on ?

OK thanks.

Actually, that's right. I need to have several output files. So, I guess I will use tFlowToIterate.

But how can I use it in my case ? Should I call it after the tJavaRow ? I can't create a iterate link from the tJavaRow.

So, do I have to insert another component between both components ? 

Excuse me for all my questions, but I am brand new in Talend technology. 

Employee

Re: How to modify a context variable in a component and keep it later on ?

Hi,

 

Attached a skeleton flow which can help in your use case.

image.png

 

 

You need to select the number of rows in row generator as 1. This will make sure that for each incoming record, a new flow with one record is generated in each iteration. 

 

In the tjavarow, you can fetch the columns coming out of Oracle DB by giving Control+Space and search based on tFlowToIterate component name. All the components will be tagged to tFlowtoTerate and you can use this data to generate multiple files.

 

     I would suggest to start with a simple scenario of tFlowtoIterate (mentioned in help documents) to understand the functionalities of the component and then build your use case after that.

 

Warm Regards,

 

Nikhil Thampi

Five Stars

Re: How to modify a context variable in a component and keep it later on ?

Hi.

 

So, I made the architecture you recommend, but I still don't have the right result. Let me explain :

process.PNG

 

On the property of tFlowToIterate, I added useful key values, and especially numMessage.keyValues.PNG

 

On the tRowGenerator, I have one row, like you specified. Other specifications are not important to me.

rowGenerator.PNG

 

And finally, I want to get back value of numMessage in tFileOutputMSXML to create the name of the file, like this (path_OUT is defined in context) :

tFileOutputMSXML.PNG

 

 

And still, the file name is null.xml.

 

I launched the job in Java debug, and in fact, the value of the file name is still set before value of numMessage in tFlowToIterate.

 

It is very strange to me because I also made a test based on existing documentation, and in this case, I could name the file with values set in tFlowToIterate. Here is the working case based on this scenario https://help.talend.com/reader/o2I5HrOFZtZItmjCxsjUtQ/5AnaNCZdnZGHZjMlcH_nLw

workingCase.PNG

 

 

 

Employee

Re: How to modify a context variable in a component and keep it later on ?

Hi,

 

     Could you please print the output of the data using a System.out.print in tjavarow and see the results?

 

      We need to verify whether the data is coming as null for this column before iteration itself or whether the data is not loading properly to the variable of tflowtoiterate component.

 

Warm Regards,

 

Nikhil Thampi

Five Stars

Re: How to modify a context variable in a component and keep it later on ?

Hi.

I added System.out.print in tjavarow as you recommand.

In the logs, I have the right values :

 numMessage : -6
numMessage : -20

 

But anyway, I managed to by-pass this issue. I created a new job containing tRowGenerator, tJavaRow and tileOutputMSXML, so that variables are set before on tFlowToIteratee. 

And now, it's working.

Thanks

 

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 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download