Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

i want to create a mathematical formula in the context variable and use that formula in the job can anyone help please

talend for data integration, i want to create a mathematical formula in the context variable and use that formula in the job can anyone help please?

say formula is a+b and I want to store it in the context variable (context.c=a+b)

and then I want to use it in tjavaRow  int c= context.c(it should do the addition of a and b)

Labels (1)
6 Replies
Anonymous
Not applicable
Author

I'm afraid context variables are just variables. You cannot get them to act in this way. What is your use case? Maybe there is another way of achieving your requirement.

Anonymous
Not applicable
Author

I need to take formula in context and I have to load formula from file to context since formula may change according to the requirement. In short I need to take formula from file to talend job(tMap or tJavaRow).

Anonymous
Not applicable
Author

Can you give an example of the type of formula? Also, which product are you using? The Open Source Edition or the Enterprise Edition?

Anonymous
Not applicable
Author

Hello,

 

Please use a tJava in Pre Job and write your formula.

 

context.c = a+b;

 

Then use context.c in the job and all child jobs.

 

Thanks,

Subhadip

Anonymous
Not applicable
Author

I'm using talend open studio and formula some times (a*b*c)/d and some times (a*b*c*e)/d depends on the requirement. Can I get any solution to read the formula from file and execute the formula in talend job. Its going to be a great help...

Anonymous
Not applicable
Author

OK, I have found a solution for you. It requires writing a Talend routine and using a third party library. There is a Java library found here (http://mathparser.org/) which allows you to what you want. Download the appropriate Jar and add a Talend routine like below....

 

package routines;

import org.mariuszgromada.math.mxparser.*;

public class Equations {

	public static Double runExpression(String expression, double a, double b, double c, double d, double f){
		
		Argument aVal = new Argument("a");
		Argument bVal = new Argument("b");
		Argument cVal = new Argument("c");
		Argument dVal = new Argument("d");
		Argument fVal = new Argument("f");
		
		aVal.setArgumentValue(a);
		bVal.setArgumentValue(b);
		cVal.setArgumentValue(c);
		dVal.setArgumentValue(d);
		fVal.setArgumentValue(f);
		
		//System.out.println(aVal.getArgumentName()+aVal.getArgumentValue());
		Expression ex = new Expression(expression, aVal, bVal, cVal, dVal, fVal);
		//System.out.println(ex.getExpressionString());
		//System.out.println(ex.getHelp());
		return ex.calculate();
		 
	}
}

You will need to set up the routine Jar dependencies by right-clicking and select "Edit Routine Libraries".

 

Once the above is done, you can supply your equations to a Context Variable and use that Context Variable at runtime with code similar to below...

 

	double a = 5.0;
	double b = 2.0;
	double c = 3.0;
	double d = 4.0;
	double f = 2.0;
	
        context.myEquation = "(a*b*c)/d"; 
        System.out.println(routines.Equations.runExpression(context.myEquation",a,b,c,d,f));
        context.myEquation = "(a*b*c*f)/d";
	System.out.println(routines.Equations.runExpression(context.myEquation,a,b,c,d,f));

Note that I didn't use "e" in the equations above since "e" is a reserved value for the mXParser tool. You will have to bare that in mind when using this method.