Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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.
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......
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.
@TalendSpree,are you looking for the below one?
@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!
Hi @TalendSpree
I achieved this using tMemorize and tJavaFlex but as you have mentioned you want the solution without using tJavaFlex code.
Sachin D
@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`.
Thanks @TalendSpree
Please check below screenshots.
I quickly tried it, if you need you can change little code in tJavaFlex if required.
Thanks,
click Accept the solution if it helps
and
kudos are welcome when a reply is helpful.
Sachin D
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......
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.
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 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?
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 🙂
Thanks @rhall ! I meant increase the number of rows.
Do you have any other tips & tricks on Talend components?