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
Highlighted
Community Manager

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. 

 

 

View solution in original post


All Replies
Highlighted
Moderator

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
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Highlighted
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!

Highlighted
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

Highlighted
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`.

Highlighted
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

Highlighted
Community Manager

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. 

 

 

View solution in original post

Highlighted
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?

 

 

Highlighted
Community Manager

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 :-)

Highlighted
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

Highlighted
Community Manager

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 :-)

Highlighted
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.

 

 

Highlighted
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??

Highlighted
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
Highlighted
Community Manager

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.

Highlighted
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
Highlighted
Community Manager

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.

Highlighted
Five Stars

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

Hello,

 

Thanks @rhall_2_0  your solution help me a lot but my need is a bit different.

Here's what I've with the current solution :

 

OUTPUT 1 :

A   |   2   |   2

B   |   1   |   3

C   |   4   |   7

D   |   1   |   6

 

But what I need is :

 

A   |   2   |   2

A   |   1   |   3

B   |   1   |  1

C   |   1   |   1

C   |   3   |   4

 

So I would like to "reset" the count for each different kind of input (A, B, C).

 

Highlighted
Community Manager

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

Hi @Slope,

 

This looks like it might be a slightly different problem. Can you raise this in a new thread and completely explain the problem please?

 

Thanks

Highlighted
Five Stars

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

Hi,

 

I created a new post before replying on this one.

Here's the link : https://community.talend.com/t5/Design-and-Development/Aggregation-on-one-attribut-and-keeping-date/...

 

 

Kind regards

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

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

Downloads and Trials

Test drive Talend's enterprise products.

Downloads