[resolved] Modify and Dumping Context Variables

One Star

[resolved] Modify and Dumping Context Variables

Hi,
I am loading loading context Variables from a delimited file into a toutputbuffer from a job. Afterwards,  I use tContextLoad to load the context variable into my job.  Then I use tcontextDump to write back to my file.  I am altering two fields, Begin and End Dates that are unix timestamps.  I use tjava to alter the fields in runtime during my job the fields are updated for using during the job.  Unfortunately, when I use tcontextDump to write the values to a file they remain the original values from when I first read the file.  Does anyone know how to force the Job to pass the new value to tContextDump and write the fields to the file?
my tJava
context.CBBeginTime = "1406259070";
context.CBEndTime = "1406259072";
System.out.println(context.CBEndTime);
System.out.println(context.CBEndTime);

Thanks
David

Accepted Solutions
One Star

Re: [resolved] Modify and Dumping Context Variables

I figured it out.  Laurent way works and it simple.  I wasn't thinking about catching the value in flow before ContextLoad or after the ContextDump.  Then use a tJavaRow to catch the row you want with a if statement.  Thanks Laurent. 
String BeginTime     = "CBBeginTime";
String EndTime         = "CBEndTime";
output_row.key = input_row.key;
output_row.value = input_row.value;
if(output_row.key.contains(BeginTime))
{
    output_row.value = "Hello";
}
if(output_row.key.contains(EndTime))
{
    output_row.value = "Hello";
}
As for the the other way, changing the value of the actual context variable and contextDump not dumping it.  I am not a java programmer but looked at the code and it looks like it grabs the context from father context variables at a global level then move them to a child and when it dumps it goes back up to the father variable so, it never sees the child context variable I modified, it only sees it in that job. 
Thanks everyone,
David

All Replies
Seventeen Stars

Re: [resolved] Modify and Dumping Context Variables

hi dytanaka,
tJava component cannot manage flow , Use tJavaRow instead
https://help.talend.com/search/all?query=tJavaRow&content-lang=en
regards
laurent
Four Stars

Re: [resolved] Modify and Dumping Context Variables

Hi David,
- Whether the contextLoad component is picking up the values from the subjob? use print operations to ensure...
- in tJava print values of context variables before alter as well to ensure that they are available.... it should work... 
- What is the schema for for ContextReader component? is it Key and Value pair only?
- Do you have any error in context loading?
Vaibhav
One Star

Re: [resolved] Modify and Dumping Context Variables

Thanks for the replies.
Laurent,
I don't understand I am not changing the flow.  I am alter the context variables in memory and then dumping it back to flow.  Are you saying that I can't modify the context variable and I have to change it as the key value pair?
Thank you
Vaibhav,
- Whether the contextLoad component is picking up the values from the subjob? use print operations to ensure...
- in tJava print values of context variables before alter as well to ensure that they are available.... it should work...
Here is the output.  You can see the variables load fine from the file.  Then I modify them.  I use them to pass begin and end dates to couchBase and it works fine.  Then I want to dump the new values I just used back to the parameter file. For some reason it uses the loaded variables and not the runtime variables that I altered to dump back to the file.
Starting job test25_Copy at 08:36 05/08/2014.
connecting to socket on port XXXX
connected
ContextReader: loading Context file XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0                        <---- Println Begin Date Before Modify
123456567087    <---- Println End   Date Before Modify
1406259070        <---- Println Begin Date After Modify
1406259072        <---- Println End   Date After Modify
. ---------------+-------------------------------------------.
|                         tLogRow_1                                      |   <------ ContextDump to LogRow
|=--------------+--------------------------------- --------=|
|key                |value                                                 |
|=--------------+------------------------------------------=|
|CBDesignDoc    |XXXXXXX                                         |
|CBDir          |XXXXXXXXXXXXXXXXXXXXXXXXXX         |
|tmpDir         |XXXXXXXXXXXXXXXXXXXXX                  |
|MySQLHost      |XXXXXXXXXXXXX                              |
|S3QL2Bucket    |XXXXXXXXXXXXXXXXXXXX                |
|QL2PickupFolder|XXXXXXXXXXXXXX                           |
|CBPassword     |                                                        |
|MySQLPassword  |XXXXXXXXXX                                 |
|CBDataBucket   |XXXXXXXXXX                                   |
|QL2Folder      |XXXXXXXXXXX                                    |
|MySQLUsername  |XXXX                                            |
|QL2DupData     |XXXXXXXXXXXXXXXXXXXXXXX          |
|CBURI          |XXXXXXXXXXXXXXXXXX                        |       
|CBBeginTime    |0             <---- What it actually wrote to file
|S3AccessKey    |XXXXXXXXXXXXXXXXXXXX                 |
|QL2RejectData  |XXXXXXXXXXXXXXXXXXXXXX            |
|CBUsername     |XXXXXXXXXXXXX                              |
|S3SecretKey    |XXXXXX                                             |
|contextName    |XXXXXXX                                          |
|contextDir     |XXXXXXXXXXXXXXXXXXXXXXXXX                  |
|CBEndTime      |123456567087  <---- What it actually wrote to file
|contextStr     |Default                                                  |
|MySQLDB        |XXXXXXXXXXXXXXXX                           |
|MySQLPort      |XXXXX                                      |
'---------------+-------------------------------------------'
1406259070      <---- Println Begin Date After Dump and Write
1406259072      <---- Println End Date After Dump and Write
disconnected
Job test25_Copy ended at 08:36 05/08/2014.
- What is the schema for for ContextReader component? is it Key and Value pair only?
           As above just a simple key value pair.
- Do you have any error in context loading?
           No errors.

Thank you
David
One Star

Re: [resolved] Modify and Dumping Context Variables

I figured it out.  Laurent way works and it simple.  I wasn't thinking about catching the value in flow before ContextLoad or after the ContextDump.  Then use a tJavaRow to catch the row you want with a if statement.  Thanks Laurent. 
String BeginTime     = "CBBeginTime";
String EndTime         = "CBEndTime";
output_row.key = input_row.key;
output_row.value = input_row.value;
if(output_row.key.contains(BeginTime))
{
    output_row.value = "Hello";
}
if(output_row.key.contains(EndTime))
{
    output_row.value = "Hello";
}
As for the the other way, changing the value of the actual context variable and contextDump not dumping it.  I am not a java programmer but looked at the code and it looks like it grabs the context from father context variables at a global level then move them to a child and when it dumps it goes back up to the father variable so, it never sees the child context variable I modified, it only sees it in that job. 
Thanks everyone,
David
One Star

Re: [resolved] Modify and Dumping Context Variables

For information, there seems to be a bug in the context object.

 

There are two ways to modify a context variable from a tJava :

 

 

 

context.new1 = "TOTO";

In this first case, tContextDump does not seem to take into account the change of the value of new1. (restores the initial value of new1 at the beginning of the job) 

 

context.setProperty( "new1", "TOTO" );

However, the tcontextDump component works with this second solution!

 

(Test in TOS version 7.0.1)

Forteen Stars TRF
Forteen Stars

Re: [resolved] Modify and Dumping Context Variables

You may also use tContextLoad to load new context variables which will be available through tContextDump.
Many ways to obtain the same result, just have to choose which one is the more suitable for your case.

TRF