tJava - records count

One Star

tJava - records count

I am trying to count number of records in t JAVA components. Reading one row at a time.
If data value = 'POLICY' then policycount = policycount + 1  and then i just want the total at the end
elseif data value = 'CUST' then custcount = custcount + 1..
Please help.
One Star

Re: tJava - records count

Hi Sangita,
Using tJavaRow will make it easier as it supports both/either of the input row and output row from/to other components.
For your example, we can make use of the tJavaRow with an input row only. 
Here in tJavaRow component you can write your own logic in Java to calculate, hold, and print values like tJava. 
There is another component named tJavaFlex can also be used for this purpose. In that case the input column names are referred as per the link name construct, like
//Assigning a value from incoming row;
String tempvalue = row23.data_value;
//The rest of the logic remains the same

.
using tJavaRow component:
//Initialize variables to hold the count
//Note:- if the count is anticipated to exceed the integer limit,
//then recommended to use long data type
int pol_count = 0;
int cust_count = 0;
//conditional statement to check the data_value
//the input row column is represented by input_row.column_name
if ( ("POLICY").equals(input_row.data_value) ) {
//the policy count is being held using pol_count variable.
pol_count++;
}
else {
if ( ("CUST").equals(input_row.data_value) ) {
cust_count++;
}
}
//The held counts can be printed using the sysout
System.out.println("Policy_Count : "+ pol_count + "\nCust_Count : "+ cust_count);


Note: I am not a java person yet, so if there are any syntax errors, please correct them and let me know Smiley Happy
One Star

Re: tJava - records count

thanks for the reply.
when I try to print pol_count , I get 0 value, instead of 15(since total pol_count records are 15).
pol_counts get reset once I am out of if else statement.
once I find pol_count, I want to use this total and print when data value = "TRA" in the next if else statements.
Ex: if ( ("POLICY").equals(input_row.data_value) ) {
//the policy count is being held using pol_count variable.
pol_count++;
}
else {
if ( ("CUST").equals(input_row.data_value) ) {
cust_count++;
}
}


if ("TRA").equals(input_row.data_value) ) 
output_row.line = "policy_total"+pol_count+"cust total"+cust_count;


then I will wite output_row.line to flat file 

Thanks again.
Sixteen Stars

Re: tJava - records count

You can use gnan.ram's solution, but you should use the tJavaFlex instead of the tJavaRow. The reason for this is that you can instantiate your variables for counting in the "Start Code" section. This code is run at the start of the subjob, then the "Main Code" section is run for every row. With the tJavaRow, all of the code you use is run on every row which means variables are instantiated for every row.