Print variables from inside tMap

Six Stars

Print variables from inside tMap

I have a situation on the picture. How can I print out variable that was created in a tMap? Untitled.png


Accepted Solutions
Community Manager

Re: Print variables from inside tMap

I am confused by this. Why do you want to print them out in the tJavaRow without passing them to the tJavaRow? What is the issue with supplying them to the tJavaRow? The reason I ask is that you will not be able to achieve this without seriously compromising the "timing" of the flow of data. If you have 100 rows being processed by the tMap, you cannot assume that when the flow gets to the tJavaRow for row 1, that the value in the Var.var1 will be valid for row 1. It may very well be processing row 100 at that point, or may have finished.

 

"Row" links and "Iterate" links work in different ways and what you are wanting would require an "Iterate" link. Unfortunately tMap components do not supply these.

 

Now, you can use some Java and extend the first solution I sent you to achieve this, but it really sounds like an awful lot of work to achieve something for a reason I cannot fathom with the information you have given. But here is the approach you could use to solve this if you do need it.....

 

1) Create a routine method similar to the one I gave, but this time load the Var variable to a HashMap with a "key" which is unique in your dataset. The HashMap should be available to the whole routine.

2) Create another routine method which will return the value of the HashMap element when supplied the correct "key"

3) In your tJavaRow call the second method (see above) and supply the "key" field. This will return the value for your Var that relates to that particular datarow.

 

As you can see, all your are really doing here is circumnavigating the normal flow in order to do something that would be easier done by simply passing the value.

 

What is the reason that it needs to be done this way?


All Replies
Community Manager

Re: Print variables from inside tMap

I'm not sure I entirely understand this requirement, but if you are trying to simply print the value of a variable and use it in the tMap, you could try a little trick with a routine. For example....

 

public static String printStringValue(String data){
     System.out.println("My variable:"+data);
     return data;
}

Not the most elegant of methods, but it will works for Strings. Just create a new routine and add the above code. Then call that routine in your tMap when you use the variable. It will pass it back to the tMap untouched and print out to the output window.

Six Stars

Re: Print variables from inside tMap

please explain your scenario briefly.....

Six Stars

Re: Print variables from inside tMap

I have a tMap and inide I created some variables(in the middle column), Now I would like to print out those variable with tjava row?

Community Manager

Re: Print variables from inside tMap

The code I have given you is intended to be used when you use your tMap variables. So when you create them in the box in the middle, you will either use them in  further variables or in the output. With the method I have given you above (for Strings only) you can use your variables and print them out. For example, if you are concatenating as String in your tMap variable, it might look like this.....

 

row1.val1 + "_"+row1.val2

The above variable might be called "concatString". You may want to output that to the job AND see what it holds. With the method I have given you, you can simply add this code to your output column....

 

routines.MyTalendRoutines.printStringValue(Var.concatString)

I've assumed the name of the routine as "MyTalendRoutines".

Six Stars

Re: Print variables from inside tMap

Can you create an example job, export it and upload it ?

Community Manager

Re: Print variables from inside tMap

Take a look at the attached job.....

Six Stars

Re: Print variables from inside tMap

Can you have a look at this pictures and I have a question at the bottom. This is from the example you sent me.

Untitled.pngvariable to printUntitled1.pngI would print var1 in this tJavaRow, is that possible?

 

Community Manager

Re: Print variables from inside tMap

Maybe I have assumed you want to do something a little more complicated. All you need to do is add ....

 

Var.var1

...to your out12 newColumn expression. Then in your tJavaRow just use the following code....

System.out.println(input_row.newColumn);

The example I gave you enables you to print the value of your variable to the output from within your tMap. This is useful in debugging. I assumed that is what you wanted this for

Six Stars

Re: Print variables from inside tMap

Is it possible to print

Var.var1

without passing it to the out12 newColumn expression ?

Community Manager

Re: Print variables from inside tMap

Yes, in the first way I showed you

Six Stars

Re: Print variables from inside tMap

I mean not inside the tMap but inside tJavaRow?

Community Manager

Re: Print variables from inside tMap

I am confused by this. Why do you want to print them out in the tJavaRow without passing them to the tJavaRow? What is the issue with supplying them to the tJavaRow? The reason I ask is that you will not be able to achieve this without seriously compromising the "timing" of the flow of data. If you have 100 rows being processed by the tMap, you cannot assume that when the flow gets to the tJavaRow for row 1, that the value in the Var.var1 will be valid for row 1. It may very well be processing row 100 at that point, or may have finished.

 

"Row" links and "Iterate" links work in different ways and what you are wanting would require an "Iterate" link. Unfortunately tMap components do not supply these.

 

Now, you can use some Java and extend the first solution I sent you to achieve this, but it really sounds like an awful lot of work to achieve something for a reason I cannot fathom with the information you have given. But here is the approach you could use to solve this if you do need it.....

 

1) Create a routine method similar to the one I gave, but this time load the Var variable to a HashMap with a "key" which is unique in your dataset. The HashMap should be available to the whole routine.

2) Create another routine method which will return the value of the HashMap element when supplied the correct "key"

3) In your tJavaRow call the second method (see above) and supply the "key" field. This will return the value for your Var that relates to that particular datarow.

 

As you can see, all your are really doing here is circumnavigating the normal flow in order to do something that would be easier done by simply passing the value.

 

What is the reason that it needs to be done this way?

Six Stars

Re: Print variables from inside tMap

I thought this could be done easily. I thought that I saw how to do this somewhere on the internet. Thank for your reply.

Community Manager

Re: Print variables from inside tMap

I think you are confusing tMap variables with globalMap variables. You get globalMap variables created when you use a tSetGlobalVar and tFlowToIterate components. globalMap values are available anywhere in the job after they have been set. However when used with a tFlowToIterate, they are only valid for the iteration following the tFlowToIterate

Calling Talend Open Studio Users

The first 100 community members completing the Open Studio survey win a $10 gift voucher.

Start the survey

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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog