How to get previous row (tuple)'s value without using Custom Code components?

Five Stars

How to get previous row (tuple)'s value without using Custom Code components?

I want to use the previous tuple(s)' value in an expression in tMap.

 

For eg.,

 

Input:

A   |   2

B   |   1

C   |   4

D   |   1

E   |    3

 

Output (previous 2 tuples added):

A   |   2   |   2

B   |   1   |   3

C   |   4   |   7

D   |   1   |   6

E   |   3   |   8

 

How to extract it in tMap expression? Or is there any other component to do it? I do not want to use stuff like tJavaXXXX or tPerlXXXX etc Custom Code or Database query components, which are shortcuts.


Accepted Solutions
Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

To do this in a tMap you can use the tMap variables. These are more powerful than they look because of two key features; they keep their values between rows and they are processed from top to bottom in order. As such you can configure them to hold the last value of individual columns and compare them against the current value. 

 

Below is an example of how this can be done......

last_value.png

Notice the order in which the variables are assigned and how the _last variables (representing the last row) are set by the _this variables (which are actually set by the current row) before those variables are set by the current row. 

 

 


All Replies
Thirteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

@TalendSpree,are you looking for the below one?Untitled.png

Manohar B
Five Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

@manodwhb This is an accumulation of all previous values. What I'm looking for is a cumulative sum of previous 'n' values. Thanks for taking the time to implement my problem, though!

Seven Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Hi @TalendSpree

 

I achieved this using tMemorize and tJavaFlex but as you have mentioned you want the solution without using tJavaFlex code. 

 

tMemorize_1.JPG

 

Sachin D

Five Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

@SachinD Thanks for taking the time to implement my problem! I would actually like to know how you're using `tMemorizeRows` and it's output in `tJavaFlex`.

Seven Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Thanks @TalendSpree 

 

Please check below screenshots.

I quickly tried it, if you need you can change little code in tJavaFlex if required.

 

Capture_1.JPGCapture_3.JPGCapture_2.JPGCapture_4.JPG

Thanks,

 

 

click Accept the solution if it helps
and
kudos are welcome when a reply is helpful.

 

Sachin D

Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

To do this in a tMap you can use the tMap variables. These are more powerful than they look because of two key features; they keep their values between rows and they are processed from top to bottom in order. As such you can configure them to hold the last value of individual columns and compare them against the current value. 

 

Below is an example of how this can be done......

last_value.png

Notice the order in which the variables are assigned and how the _last variables (representing the last row) are set by the _this variables (which are actually set by the current row) before those variables are set by the current row. 

 

 

Five Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Thanks @SachinD ... can we use `globalMap("tMemRows")` in `tMap` component? I've tried, but I get the following error.

Exception in component tMap_1 (tMemRows)
java.lang.NullPointerException
at local_project.tmemrows_0_1.tMemRows.tFileInputDelimited_1Process(tMemRows.java:1126)
at local_project.tmemrows_0_1.tMemRows.runJobInTOS(tMemRows.java:1669)
at local_project.tmemrows_0_1.tMemRows.main(tMemRows.java:1518)

 

 

@rhall_2_0 Thank you for the help! This is closest to what I was looking for. Only issue is, if I want to change the number of accumulations, I'll have to do it manually. Any suggestions?

 

 

Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

What do you mean by the number of accumulations? Do you mean the number of held rows or the number of columns to hold? You can achieve increases in both, but you are right that you will have to do it manually. This is where you have to decide which is the best approach to take; to use other components (like the memorize rows component) and a bit of Java or keep it all in the tMap. This is why I like Talend because you have this choice :-)

Five Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Thanks @rhall_2_0 ! I meant increase the number of rows.

Do you have any other tips & tricks on Talend components? Smiley Very Happy

Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

There are a few, but I don't tend to think about them as "tricks", just how I use them to solve certain problems. Nothing that I can just "list" I'm afraid :-)

Seven Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Thanks @rhall_2_0

I too tried your solution, and it works perfectly fine.

I tried such thing with tMap for first time and now understood power of tMap.

 

Thanks for sharing such solution, please keep sharing such good tricks.

 

Also Thanks @TalendSpree for posting this topic.

 

Sachin D.

 

 

Six Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Hi, can you share the configuration of the tmap please??

Six Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

Hello, can you share the TMap editor . Thank you
Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

There is a screenshot above that shows the configuration. Click on it and it will allow you to zoom in.

Six Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

how to i access the next row elements? is there any method where instead of previous row i want the next row . thnk you in advance
Sixteen Stars

Re: How to get previous row (tuple)'s value without using Custom Code components?

No, you can't do that. But what is wrong with this method? You are essentially accessing the "next" row, just not in the way you think it should work. The other way this could be done is to simply use a sorting component to reverse the rows, then reverse then back after the component you wish to carry out your logic in.