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,

Dave
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

hi,
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
Highlighted
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.

Face
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.
regards,

Face
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.

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 Integration

Practical steps to developing your data integration strategy.

Download