在Kettle的Java脚本中修改字段类型

先上代码,其他的以后再写
我没有系统的学过Java,写的不标准的地方请无视。

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Math;
import org.pentaho.di.core.row.value.ValueMetaFactory;
import org.pentaho.di.core.row.ValueMetaInterface;

private double unit_price_fob;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
  if (first) {
    first = false;
  }

  Object[] r = getRow();

  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.
  r = createOutputRow(r, data.outputRowMeta.size());
  String warning_mark = get(Fields.In, "warning_mark").getString(r);
  if (warning_mark == "null"){
	  warning_mark = "";
  }

  String error_mark = get(Fields.In, "error_mark").getString(r);
  if (error_mark == "null"){
	  error_mark = "";
  }
  
  String checked_cols = get(Fields.In, "checked_cols").getString(r);
  if (checked_cols == "null"){
	  checked_cols = "";
  }
  
  String num_str = get(Fields.In, "unit_price_fob").getString(r);
  
  if (num_str != "null" && num_str != null){
  
    Pattern p1 = Pattern.compile("^([\\d\\.]+)e-(\\d+)$");
    Matcher m1 = p1.matcher(num_str);

    if (m1.find()) {
      double dividend = Double.parseDouble(m1.group(1));
      double power = Double.parseDouble(m1.group(2));

      double divisor = Math.pow(10, power);
      
      unit_price_fob = dividend / divisor;
    }
    else{
      unit_price_fob = Double.parseDouble(num_str);
    }
    get(Fields.Out, "checked_cols").setValue(r, checked_cols+"/unit_price_fob");
    get(Fields.Out, "unit_price_fob").setValue(r, unit_price_fob);
  }
  
  int idx = getInputRowMeta().indexOfValue(getParameter("unit_price_cif"));
  RowMetaInterface row_meta = data.outputRowMeta;

  ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta("unit_price_cif", ValueMetaInterface.TYPE_NUMBER);
  row_meta.setValueMeta(idx, valueMeta);

  putRow(row_meta, r);
  return true;
}

版权声明:本文为weixin_39461443原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。