Skip to content

Instantly share code, notes, and snippets.

@timpamungkas
Last active April 25, 2022 07:51
Show Gist options
  • Save timpamungkas/4fccd99bd3a9e6119a95d72469c25cba to your computer and use it in GitHub Desktop.
Save timpamungkas/4fccd99bd3a9e6119a95d72469c25cba to your computer and use it in GitHub Desktop.
Kafka KsqlDB UDF (scalar function)
import io.confluent.ksql.function.udf.Udf;
import io.confluent.ksql.function.udf.UdfDescription;
import io.confluent.ksql.function.udf.UdfParameter;
@UdfDescription(name = "loan_installment", category = "LOAN", author = "Timotius Pamungkas", version = "1.0.0", description = "User defined function for sample scalar loan business logic.")
public class LoanUdf {
@Udf(description = "Estimate monthly loan installment for loan principal amount X, "
+ "with loan interest rate per year is Y %, " + "over loan period Z month(s), "
+ "roundest to 2 digit decimal. All parameters are mandatory and can only accept value greater than 1")
public double estimateMonthlyLoanInstallment(
@UdfParameter(description = "Loan principal amount") double principalLoanAmount,
@UdfParameter(description = "Annual interest rate (in percent)") double annualInterestRate,
@UdfParameter(description = "Loan period in month") int loanPeriodMonth) {
return LoanCalculator.calculateMonthlyInstallment(principalLoanAmount, annualInterestRate, loanPeriodMonth);
}
@Udf(description = "Estimate monthly loan installment for loan principal amount X, "
+ "with loan interest rate per year is Y %, " + "over loan period Z month(s), "
+ "roundest to 2 digit decimal. All parameters are mandatory and can only accept value greater than 1. "
+ "This assume the loan period is 12 months.")
public double estimateMonthlyLoanInstallment(
@UdfParameter(description = "Loan principal amount") double principalLoanAmount,
@UdfParameter(description = "Annual interest rate (in percent)") double annualInterestRate) {
return LoanCalculator.calculateMonthlyInstallment(principalLoanAmount, annualInterestRate, 12);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment