TJavaRow with condition

Four Stars

TJavaRow with condition

Hi,

I have this example:

"reste"=Voix/(supp+1)

for example, for code1: "number" to distribute =2

1) for "rest" = 584 / (2+1)=194.66 
    for "rest" = 840 / (3+1) =210 
    for "rest" = 863 / (3+1) =215.75 we assign a "Number"=1 -> Supp = Supp+1 ( because 215,75 is the max)

2) for "rest" = 584 / (2+1)=194.66 
    for "rest" = 840 / (3+1) =210 we assign a "Number"=1 -> Supp = Supp+1 ( because 210 is the max)

    for "rest" = 863 / (3+1+1) =172.6

result:

code|Supp

   1   |   2

   1   |  4  (3+1)

   1    |  4 (3+1)  

and we repete for each group of code the same procedure precedent.

I try this code but il doesn't works1.PNG2.PNG

 

Community Manager

Re: TJavaRow with condition

Can you share the code as text please? It is easier to try out that way :-)

Four Stars

Re: TJavaRow with condition

output_row.code = input_row.code;
output_row.number = input_row.number;
output_row.VOIX = input_row.VOIX;
output_row.supp = input_row.supp;
output_row.rest = input_row.rest;


String code = input_row.code;
int num = input_row.number;
//double reste =Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

if( code.contains(context.current_code) ) {
context.to_distribute -= 1 ;
output_row.supp += ( 0 > context.to_distribute ? 0 : 1 );
context.reste= Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

} else if (context.reste >= input_row.rest) {
context.current_code = input_row.code;
context.to_distribute = input_row.number - 1;
context.reste= Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

output_row.supp += ( 0 > context.to_distribute ? 0 : 1 );
output_row.rest=context.reste;
}

Community Manager

Re: TJavaRow with condition

The main problem you have is that you are assigning the output_row values at the very beginning. These are not normal variables. You should only assign these once, once you have calculated your values. You are also trying to increment output_row values, this is not ideal either.

 

Your IF conditions look a little confusing, but that could just be the logic you want. What if neither of the conditions are true? Do you require an "else"?

 

I have adjusted your code so that it will work. The logic may not be quite correct (as explained briefly above), but this will change the values when the conditions are met.

 

//output_row.code = input_row.code;
//output_row.number = input_row.number;
//output_row.VOIX = input_row.VOIX;
//output_row.supp = input_row.supp;
//output_row.rest = input_row.rest;

int VOIX = input_row.VOIX;
int supp = input_row.supp;
double rest = input_row.rest;

String code = input_row.code;
int number = input_row.number;

if( code.contains(context.current_code) ) {
	context.to_distribute -= 1 ;
	context.reste= ((double)VOIX)/(((double)supp)+1.0+1.0);
	supp += ( 0 > context.to_distribute ? 0 : 1 );
	output_row.supp = supp;
	output_row.code = code;
	output_row.number = number;
	output_row.VOIX = VOIX;
	output_row.rest=context.reste;

} else if (context.reste >= rest) {
	context.current_code = code;
	context.to_distribute = number - 1;
	context.reste= ((double)VOIX)/(((double)supp)+1.0+1.0);
	supp += ( 0 > context.to_distribute ? 0 : 1 );
	output_row.supp = supp;
	output_row.rest=context.reste;
	output_row.code = code;
	output_row.number = number;
	output_row.VOIX = VOIX;

}
Four Stars

Re: TJavaRow with condition

But my problem is that I can not apply the logic of my example. how can I distribute "number" each time on the value of the maximum "rest"? is that this java code is enough or I have to apply a loop?

Community Manager

Re: TJavaRow with condition

I'm afraid I do not understand the logic. Your example does not give enough information as to why each of the equations take place? Maybe a simple example of an input table and expected output table would help? You have to remember that we do not have the business requirement or your understanding of what you are trying to achieve. A lot of the time I can infer from input to output (spotting the logical flow), but I just don't see it here I am afraid.

Nine Stars

Re: TJavaRow with condition

the input is :
code|number|rest|supp
1 |2 |150 |4
1 |2 |125 |1
1 |2 |110 |1
2 |3 |145 |0
2 |3 |130 |3
2 |3 |50 |1
2 |3 |10 |4

The output of the first transformation is :
1 |2 |150 |5
1 |2 |125 |2
1 |2 |110 |1
2 |3 |145 |1
2 |3 |130 |4
2 |3 |50 |2
2 |3 |10 |4

And you said : 

"reste"=Voix/(supp+1)

what is "Voix" ?

You added :
for example, for code1: "number" to distribute =2

1) for "rest" = 584 / (2+1)=194.66 // where is 2 from ? is it because number=2 ?
for "rest" = 840 / (3+1) =210 // where is 3 from ?
for "rest" = 863 / (3+1) =215.75 // where is 3 from ?

 

 

Regards
DGM
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.

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