javascript脚本
脚本就是直接通过程序代码完成一些复杂的操作。
例子:生成日期维度数据,有日期,年,月,日,从2000年1月1日开始有1000条数据,保存到Excel
生成记录
效果:1000行数据
增加序列
计算器
字段选择移出date
js脚本
最后输出即可
Java脚本
Java脚本就是使用Java语言通过变成来完成对数据流的操作。
例子:从Excel中读取数据,生成newcode字段,如果code列为null就使用name列来替换,否则就在code列后边加上123,保存结果
Excel输入
Java脚本,Common use下边有个main,点击后右侧会自动有代码及示例。
下边需要自己先定义新字段
然后写代码逻辑,中文注释部分是对照上边示例写的。
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (first) {
first = false;
/* TODO: Your code here. (Using info fields)
FieldHelper infoField = get(Fields.Info, "info_field_name");
RowSet infoStream = findInfoRowSet("info_stream_tag");
Object[] infoRow = null;
int infoRowCount = 0;
// Read all rows from info step before calling getRow() method, which returns first row from any
// input rowset. As rowMeta for info and input steps varies getRow() can lead to errors.
while((infoRow = getRowFrom(infoStream)) != null){
// do something with info data
infoRowCount++;
}
*/
}
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());
/* TODO: Your code here. (See Sample)
// Get the value from an input field
String foobar = get(Fields.In, "a_fieldname").getString(r);
foobar += "bar";
// Set a value in a new output field
get(Fields.Out, "newcode").setValue(r, code);
*/
//获取code
String code = get(Fields.In,"code").getString(r);
//如果code是空,就拿name的值填充,否则code+=123
if(code == null || code.equals("")){
code = get(Fields.In,"name").getString(r);
}else{
code+=123;
}
//把code操作后的东西保存在newcode
get(Fields.Out, "newcode").setValue(r, code);
// Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
}
结果对比
执行SQL脚本
执行sql脚本可以执行一个update语句,用来更新某个表中的数据
例子:从user表获取所有数据,把uid都改成8
表输入
执行SQL语句
结果