Four Stars

Javascript execution using tsystem

Hi all ,

I have a javascript file that contains a function which returns a value. I want to call the function from any talend component, get the returned value and use it in the job .I tried the same using tsystem component but i did not know how to call the js function from the tsystem command .Please help me on this.

 

Thanks in advance,

Praveen M.

  • Exchange components
9 REPLIES
Seven Stars

Re: Javascript execution using tsystem

generally - same as You run it from command line 

 

for example in case of windows 

it will be

"cmd /c cscript your file.js"

 

in case of Mac it will be

"rhino yourfile.js"
or
"node yourfile.js"



-----------
Four Stars

Re: Javascript execution using tsystem

Hi ,

 

Thanks. Am able to run the js file using the command in tsystem as u mentioned , but my issue is to return a value from the js function to any other component or job in talend. I used "echo" statement to throw the value , it worked .But i want to get the return value from the js function into my talend job.

 

Thanks in advance ,

Praveen M.

Seven Stars

Re: Javascript execution using tsystem

tSystem have settings for store Standard Output and Error Output - Console and/or variable

 

Enable store to variables, and in case of standard output - check 

((String)globalMap.get("tSystem_1_OUTPUT"))

 

-----------
Four Stars

Re: Javascript execution using tsystem

Thanks. I used ,

System.out.println((String)globalMap.get("tSystem_1_OUTPUT"))

in tjava component after writing the necessary file details in tsystem , it thrown me 'null'. Can you elaborate the method to get the value from js file a bit in detail ?  

 

Thanks ,

Praveen M.

Four Stars

Re: Javascript execution using tsystem

 

function test(){
var getValues = WScript.Arguments; return "The merged values are:"+getValues(0)+"-"+getValues(1)+"."; } test();

This is the sample code on my JS file .

"cmd /c cscript samplefile.js \"test\" \"run\""

This is the code on my tsystem .

 

Thanks ,

Praveen M.

 

 

 

Thanks ,

Praveen M.

Seven Stars

Re: Javascript execution using tsystem

Screen Shot 2017-04-19 at 9.04.57 PM.png

 

this is execution of You example in windows CMD shell

Why You expect anything other than null, if Your script return null :-)

 

tSystem catch output, but Your script do not have it

-----------
Four Stars

Re: Javascript execution using tsystem

I have checked the option to store the tsystem output to global variable , still it throws null. If my method throws null ,what is the correct method to store the tsystem's output in a variable and how to use it in tjava or any other component ? Thanks , Praveen M.
Seven Stars

Re: Javascript execution using tsystem


praveenvm wrote:
I have checked the option to store the tsystem output to global variable , still it throws null. If my method throws null ,what is the correct method to store the tsystem's output in a variable and how to use it in tjava or any other component ? Thanks , Praveen M.

You have some misunderstanding - Your function return information into main part of Your script, but Your script do not return anything to OS console!

 

tSystem store into variable or on console - Standard or Error Outputs

So for store something need to have this something :-)

 

 

My picture it is result run of Your script without talend - just under windows command line ... 

because Your script do not send to Output any information ( == print), Talend nothing to store into variable

-----------
Four Stars

Re: Javascript execution using tsystem

Thanks , i understand it now.

So how should i write to OS Console from my .Js file ?

I tried using echo , it does not work. Also what should be the code on t_java component to get the value from .js file

 

Thanks ,

Praveen M.