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

Forteen 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
Don't forget to give kudos/accept the solution when a replay is helpful.
Highlighted
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)
Community Manager

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,

Community Manager

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

 

Community Manager

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

Four Stars

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

Hi All,

In my case i can print global variable value on console, but i want to print this value into my tOutputExcel component. example: current_file_name

How to do it?

Please help me.

 

thanks 

Pravin

 

Four Stars

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

Hi Sir,

In my case i can print global variable value on console, but i want to print this value into my tOutputExcel component. example: current_file_name

How to do it?

Please help me.

 

thanks 

Pravin

 

Community Manager

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

Do you want to set the file name? If so, the value needs to be calculated and set in your globalMap in a subjob before the subjob where your Excel component is written to. You can then use the globalMap value to set the file name.

Four Stars

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

Thanks for reply...
I got the file name using following code
String fileName = ((String)globalMap.get("tFileList_3_CURRENT_FILE"));
context.file_name = fileName;
System.out.println(fileName);

 

but in output it shows as null value

[statistics] connecting to socket on port 3477
[statistics] connected
we_100_BA10213307.xml
.-----------------+----------+-------------+-----------------.
| tLogRow_2 |
|=----------------+----------+-------------+----------------=|
|date_specialValue|date_date |date_timeZone|current_file_name|
|=----------------+----------+-------------+----------------=|
|NONE |2018-07-30|CET |null |
|NONE |2018-07-30|CET |null |
|NONE |2018-07-30|CET |null |
'-----------------+----------+-------------+-----------------'

we_100_BA10213308.xml
.-----------------+----------+-------------+-----------------.
| tLogRow_2 |
|=----------------+----------+-------------+----------------=|
|date_specialValue|date_date |date_timeZone|current_file_name|
|=----------------+----------+-------------+----------------=|
|NONE |2018-07-30|CET |null |
|NONE |2018-07-30|CET |null |
|NONE |2018-07-30|CET |null |
'-----------------+----------+-------------+-----------------'

WE_100_BA10213313.xml
.-----------------+----------+-------------+-----------------.
| tLogRow_2 |
|=----------------+----------+-------------+----------------=|
|date_specialValue|date_date |date_timeZone|current_file_name|
|=----------------+----------+-------------+----------------=|
|NONE |2018-07-31|CET |null |
|NONE |2018-07-31|CET |null |
|NONE |2018-07-31|CET |null |
'-----------------+----------+-------------+-----------------'

[statistics] disconnected

 

why it will shown null value???

Community Manager

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

The filename can be seen in the output, but not in the table data. You will need to show me a screenshot of your job.

Four Stars

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

Yes. You are right. need to display file name in the table data.

Please find the attached screenshot of my job.

 

Community Manager

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

You need to add a tMap between your file component and the tLogRow. Add the context variable to the output of that tMap

Four Stars

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

Thank You Sir It's Working..... Smiley Happy

Community Manager

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

Not a problem. Glad to help. Can I ask that if a post provides a solution to a problem, that you accept the solution. There is a button under the post that you think provides the solution. This helps our other users easily find solutions to their problems :-)

 

2019 GARTNER 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

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog