据我所知,Kettle 不会将字段添加到脚本的范围,除非该字段作为子字符串包含在脚本源代码中(即使注释中提到的字段也应该添加)。请参阅 determineUsedFields() 和 addValues() 方法 (https://github.com/pentaho/pentaho-kettle/blob/4.4.0/src/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesMod.java#L106)。
因此,您提供的确切脚本实际上会生成三个已定义值或三个未定义值,具体取决于该字段是否存在。只有在我从代码中完全删除带有字段名称的字符串并将字段名称传递到另一个字段中之后,我才能重现您的问题。
因此,操作行值的一种方法可能是在脚本中提及所有字段名称(例如在评论中),然后尝试按照您的尝试使用 setValue(似乎仅在兼容模式下工作)。
另一种可能的方式是使用row数组变量获取值,使用getInputRowMeta().indexOfValue(fieldName)获取字段索引,例如:
var idx = getInputRowMeta().indexOfValue(fieldName)
// WARNING: you may assign value of any type this way
// and the value will not be converted to a type defined
// in the field's ValueMeta:
row[idx] = 'New value'
但是,这种方法绕过了类型转换,这通常是在 getValueFromJScript() 方法中的 JS 步骤之外传递 JavaScript 值时执行的。
例如,以下代码将在输出中放入无效值,您甚至可能不会注意到它,直到某些后续步骤以某种不正确的方式处理该值:
// Let's assume that fieldName is name of the 0th input field.
// I'd expect, that the value would remain the same
// but in fact the `fieldName` references some wrapper oject
// which looks similar to its value
// but has a different type
row[0] = fieldName;
在后续的 JS 步骤中:
for(var i = 0; i < row.length; i++) {
Alert(row[i]) // alerts same value as the input, e.g. 'test'
Alert(row[i].class) // alerts undefined. While expected is 'java.lang.String'
// Some other subsequent steps may crash once this value encountered
}