kettle_0">kettle版本
kettle 5.4
需求描述
excel中可以通过正态分布函数NORMDIST生成给定值的正态分布值,如下所示:
本文讲解如何通过kettle实现excel的NORMDIST函数。
实现效果
实现方式
1、生成记录
模拟数据输入
2、Java代码
使用注意:
如果直接将计算的double值输出,kettle默认会截断为小数点后1位,所以这里输出为字符串,保留小数点后5位。
详细代码:
import org.apache.commons.math3.distribution.NormalDistribution;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// Let's look up parameters only once for performance reason.
//
if (first) {
first=false;
}
// First, get a row from the default input hop
//
Object[] r = getRow();
// If the row object is null, we are done processing.
//
if (r == null) {
setOutputDone();
return false;
}
// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
// enough to handle any new fields you are creating in this step.
//
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
double x_in = get(Fields.In, "x_in").getNumber(r);
double mean_in = get(Fields.In, "mean_in").getNumber(r);
double standard_dev_in = get(Fields.In, "standard_dev_in").getNumber(r);
// Set the value in the output field
//
Number nd = getNd(x_in, mean_in, standard_dev_in) ;
logBasic("nd:"+nd);
// get(Fields.Out, "nd").setValue(outputRow, nd);
get(Fields.Out, "nd").setValue(outputRow, String.format("%.5f",nd));
// putRow will send the row on to the default output hop.
//
putRow(data.outputRowMeta, outputRow);
return true;
}
/**
* 计算正态分布值
*
* <pre>
* 方法同excel NORMDIST 函数
* </pre>
*
* @param x
* 需要计算其分布的数值
* @param mean
* 分布的算术平均值
* @param standard_dev
* 标准偏差
* @return 正态分布值
*/
public static double getNd(double x, double mean, double standard_dev) {
return new NormalDistribution(mean, standard_dev).density(x);
}
3、写日志
内容保留为空即可,默认输出全部变量