get and set value to Context Variable

One Star

get and set value to Context Variable

can we make the variable value dynamic?
for example: myContextVar = row1.mysql_table_column
Seven Stars

Re: get and set value to Context Variable

yes
One Star

Re: get and set value to Context Variable

how?
One Star

Re: get and set value to Context Variable

Hi
You can add a context variable in your job firstly. Let's say it is called new1.
So the code in tJavaRow is as seen below.
context.new1 = input_row.mysql_table_column
Regards,
Pedro
One Star

Re: get and set value to Context Variable

Hi,
I'm setting the value as shown in the screenshots, but it is returning me as NULL.
Please check the screenshots for details!
Community Manager

Re: get and set value to Context Variable

Hi
On tJavaRow, you can set value to context variable like:
context.name=input_row.columnName; //not context.name=row7.columnName;
And then, you are able to get the value of context variable in next subjob.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: get and set value to Context Variable

Hi Shon,
Thanks for reply.
Actually, there is no "SubJob". I need the variable's value within the main job.
Ref. to the attached screenshot: I'm fetching one parameter from "dim_searchengine" via SQL and have to use in "dim_searchstrings" for lookup. That's it!
This is what I'm doing:
1. Set a variable in Context tab as, "se_param"
2. Added tJavaRow to the tMap of "dim_searchengine"
3. Inside the tJavaRow code, I set: context.se_param=row7.columnName; (row7 is refering to dim_searchengine )
any idea how to get se_param's value in "dim_searchstrings", e.g; context.get(context.se_param)
One Star

Re: get and set value to Context Variable

Hi,
As far as I can see both rows are not connected in any way, which means they are two separate subjobs. You're able to connect the first input table to the second with an onSubjobOk trigger.
If not, you might want to show us some more of the job so things become more clear.
Regards,
Arno
One Star

Re: get and set value to Context Variable

well i think one should be able to set a Global Variable or Context Variable whose value can be used on another TMAP Component... that's a simple thing i guess, but i'm not really getting how to Get/Set that Global/Context variable correctly
Community Manager

Re: get and set value to Context Variable

Hi
Notice the Java code on tJavaRow, it should be:
context.se_param=input_row.columnName
Can you show us a full screenshot of the job? I need to overview the whole job and see if it is a design problem.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: get and set value to Context Variable

Hi,
I'm using the same thing, whereas in my case, input_row = row7 and columnName = searchengineparams
full screenshot is attached, where I've even tried using: context.se_param = "hello";
but on tLogRow, I'm getting NULL... really annoying!
Community Manager

Re: get and set value to Context Variable

Hi
You can't get the value of context variable in the same subjob, in you case, set / get value in the same subjob, because latter component always initializes first than the previous components. Please read this page to know the order of Java code generation. Let's take some example jobs to explain this:
job1:
tFileInputDelimited--main--tJavaRow_1---main--tJavaRow_2
on tJavaRow_1:
context.name=input_row.name;
on tJavaRow_2:
System.out.println(context.name);
In this job, the result is null.
job2:
tFileInputDelimited--main--tJavaRow_1
|
onsubjobok
|
tJava_1
on tJavaRow_1:
context.name=input_row.name;
on tJava_1:
System.out.println(context.name);
in this job, there are two subjobs, we can get the value of context variable on another subjob tJava_1.
To achieve it, you need to redesign the job, for example, to use tFlowToIterate or split the job into multiple subjobs.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: get and set value to Context Variable

Thank you for explaining the procedure. I got it fixed via SubJob.