How to pass the output value from tjava to excel file

Six Stars

How to pass the output value from tjava to excel file

tjava code:

Date lastDec =TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd"));
System.out.println("param1: "+TalendDate.TO_CHAR(lastDec, "MMM-yy"));

Date prevYear = TalendDate.ADD_TO_DATE(lastDec, "MM", -3);
System.out.println("param2: "+TalendDate.TO_CHAR(prevYear, "MMM-yy"));

Date prevYear1 = TalendDate.ADD_TO_DATE(lastDec, "YYYY", -1);
System.out.println("param3: "+TalendDate.TO_CHAR(prevYear1, "MMM-yy"));

Date Year1 = TalendDate.ADD_TO_DATE(lastDec, "YYYY", -1);
System.out.println("param4: "+TalendDate.TO_CHAR(Year1, "MMM-yy"));

Date Year2 = TalendDate.ADD_TO_DATE(Year1, "YYYY", +1);
System.out.println("param5: "+TalendDate.TO_CHAR(Year2, "MMM-yy"));

Date Year3 = TalendDate.ADD_TO_DATE(Year2, "YYYY", +1);
System.out.println("param6: "+TalendDate.TO_CHAR(Year3, "MMM-yy"));

Date Year4 = TalendDate.ADD_TO_DATE(Year3, "YYYY", +1);
System.out.println("param7: "+TalendDate.TO_CHAR(Year4, "MMM-yy"));

 

OUTPUT:

param1: Jun-18
param2: Mar-18
param3: Jun-17
param4: Jun-17
param5: Jun-18
param6: Jun-19
param7: Jun-20

 

Quaetions

1. how to pass this output values to excel file.

2. How to pas this output values as context variable to another component

Thirteen Stars

Re: How to pass the output value from tjava to excel file

@Darmesh,1) from tjava,you cannnot pass the output to excel file.

if you want to know more about tjava,please find the below link.

https://help.talend.com/reader/wDRBNUuxk629sNcI0dNYaA/sSh_6LAVLHtLJc5jL6e21Q

 

2) in tJava,you can put the values into global and get using below global functions.

globalMap.put("application", "Web");

(String)globalMap.get("application")

Manohar B
Six Stars

Re: How to pass the output value from tjava to excel file

Hi Manohar,

 

Thanks for your reply.

 

my requirement is, i want to pass the output values of tjava to the sql query.

sel * from tablename where <output from tjava>;

can you tell me with help of components and step by step to perform.

Tags (1)
Sixteen Stars

Re: How to pass the output value from tjava to excel file

You can use the globalMap for this. To add a value you would use this code (using your code for param7 as an example)...

 

globalMap.put("param7", TalendDate.TO_CHAR(Year4, "MMM-yy"));

To retrieve this in the WHERE clause you can use the following....

"Select
myColumns
From myTable
WHERE param7 = '" + ((String)globalMap.get("param7"))+"'"

The above code is meant as an example, I am assuming you understand how to structure SQL queries in Talend :-)

Six Stars

Re: How to pass the output value from tjava to excel file

still i am not getting the result. 

kindly correct me with steps.

error :  lastDec cannot be resolved to a variable

 

a.PNGb.PNG

Six Stars

Re: How to pass the output value from tjava to excel file

Hi Dharmesh,

If you have not resolve the error :  lastDec cannot be resolved to a variable.

 

In tJavaRow, you have used 'lastDec =' as variable name. and you are trying to use it using 'lastDec'

Please use 'lastDec' and 'lastQ3' in your globalMap.put.

 

And you same in the globalMap.get.

 

Thanks,

Sixteen Stars

Re: How to pass the output value from tjava to excel file

Your problems are these....

 

1) You are using a tJavaRow and not a tJava. A tJavaRow is for Java used with data coming from a row. It looks like your data is essentially derived from the current date.

2) Your globalMap variables are configured incorrectly. By the way it is quite difficult to correct code from screen shots. If you include your code I could have tidied it up for you. But essentially your names are not correct. The names (in quotes) need to be EXACTLY the same when you are getting and putting data there. 

3) When setting the second globalMap, you are getting the error lastDec cannot be resolved. This variable does not exist as a Date. I assume you are wanting to compute a date from the date you computed in your first bit of code. An easier way of doing this is to not group all of your code together as you have done and do it in steps. First create a Date from TalendDate.getCurrentDate(). Then manipulate it to get the lastDec date and set that as a Date variable called lastDec. Then convert that to a String and store in the globalMap. After you have done that, you can use the lastDec date to prepare your next variable.

4) When using your globalMaps in your query you seem to be looking for values with keys ...."test_log.lastDec" and "test_log.lastQ3". These do not exist. 

Six Stars

Re: How to pass the output value from tjava to excel file

can you tell me how to pass the below code to globalmap.put

 

Date lastDec =TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd"));
System.out.println("param1: "+TalendDate.TO_CHAR(lastDec, "MMM-yy"));

 

Date prevYear1 = TalendDate.ADD_TO_DATE(lastDec, "YYYY", -1);
System.out.println("param3: "+TalendDate.TO_CHAR(prevYear1, "MMM-yy"));

 

Date prevYear = TalendDate.ADD_TO_DATE(lastDec, "MM", -3);
System.out.println("param2: "+TalendDate.TO_CHAR(prevYear, "MMM-yy"));

 

Date Year2 = TalendDate.ADD_TO_DATE(TalendDate.setDate(TalendDate.getCurrentDate(), 03, "MM"), "YYYY", 0);
System.out.println("para5: "+TalendDate.TO_CHAR(Year2, "MMM-yy"));

 

Date Year1 = TalendDate.ADD_TO_DATE(Year2, "YYYY", -1);
System.out.println("param4: "+TalendDate.TO_CHAR(Year1, "MMM-yy"));

 

Date Year3 = TalendDate.ADD_TO_DATE(Year2, "YYYY", +1);

System.out.println("param6: "+TalendDate.TO_CHAR(Year3, "MMM-yy"));

 

Date Year4 = TalendDate.ADD_TO_DATE(Year3, "YYYY", +1);
System.out.println("param7: "+TalendDate.TO_CHAR(Year4, "MMM-yy"));

 

example like thisa.PNGb.PNG

 

Sixteen Stars

Re: How to pass the output value from tjava to excel file

Do those date values get stored in your database as Strings or as Dates? It looks like you are going about this in a very complicated way requiring far too much unnecessary Java. This could all be done with the following flow which would remove the need for a lot of the Java....

 

1) tFixedFlowInput with a Date column and any other columns you may need (with "Use Inline Table" ticked). Create a "row" for every different "param" you want to supply your database. Add the code for each "param" in each row of the tFixedFlowInput.

2) Then map that to a tMySQLOutput (or via a tMap to the tMySQLOutput). 

 

This way you do not need to write your insert statements and you do not need to use the globalMap.

Six Stars

Re: How to pass the output value from tjava to excel file

can you show with an example, how to do it