Assigning values to context variables

One Star

Assigning values to context variables

Hi all,
Just wondering if you can help me.
I have a DB with 2 columns in it, "filename" & "sql_statement".
I am reading one row from this DB and I want to assign the output to context variables I have created "con_filename" & "con_contents".
I want to then create a file using the value I have stored in "con_filename" and the file being created should be populated with "con_contents".
At the moment I'm selecting the data from the DB fine using a tPostgresqlInput
I am trying to assign the output to context variables here too using a tJavaRow. tJavaRow contains the following.
//Code generated according to input schema and output schema
output_row.filename = input_row.filename;
output_row.sql_statement = input_row.sql_statement;
context.filename = input_row.filename;
context.sql_statement = input_row.sql_statement;
I am then outputting directly to a tFileOutput but I'm getting both values into the file and a null file name. It appears that the context variables never get set.
How do you assign values to context variables.
Thanks in advance,

One Star

Re: Assigning values to context variables

Hi Dave
how many records are fetched from your DB select query?
If its more than 1, check if the last record gives a null or blank values.
The reason is that you are assigning the values to context in a flow. So for each record processed, the values of context changes.
Hope it helps you.
It will be better if you put a screen shot.
Seventeen Stars

Re: Assigning values to context variables

If you are using context variable to store some value from flow is not a 'best practices' even if it can work Smiley Wink
as lijolawrance said in fact the only value that you have is the lasted one into your context
For that use globalMap and a flowToIterate to catch value from the flow (have a look into codeViewer to be sure about the name (key map) of globalMap var).
It's always 'rowName.fieldName' (ex : row1.firstName)
hope it help
One Star

Re: Assigning values to context variables

Cheers for getting back to me on it guys.
I'll try using the global variables and will let you know how I get on.

One Star

Re: Assigning values to context variables

In reply to your question though LijoLawrance, I am reading in just the one row does this help my cause in making it easier. The value that is being read in is a dynamically changing filename that is created in postgresql.

One Star

Re: Assigning values to context variables

Hi Face,
You can change to what kzone has told.
In what i have told earlier will fail, if there is any null value (Sorry, i missed it).
But i still feel that u need to have a null check.


Talend named a Leader.

Get your copy


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


How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration


Definitive Guide to Data Quality

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