Pentaho:如何自动读取列名并将其保存在变量中?

时间:2016-10-13 11:31:47

标签: pentaho pentaho-spoon

我是Pentaho的新手,我需要帮助。

我从数据库表中获取数据,然后我需要计算这些表的列数并将其名称保存到变量中。有人知道怎么做吗?或者如何在Java Script步骤中读取列名?谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

首先,请注意,在设置/更改它的同一转换中,您将无法看到变量值。基本上,变换的变量值在其初始化中被读取。因此,您可以更改父/祖父母作业的变量值,但不能更改相同的转换。

选项1(流步骤的元数据结构):

一种选择是使用流步骤http://wiki.pentaho.com/display/EAI/Metadata+Structure+of+Stream的元数据结构 该步骤将为您提供以下字段:

  • 位置:入站数据流中字段的位置
  • 字段名称:入站数据流中的字段名称
  • 评论:未记载
  • 类型:字符串,整数,数字等
  • 长度:字段长度
  • 精度:如果不适用,适用于整数,数字和-1 (例如字符串)
  • 原产地:元数据源自的步骤。

此步骤的输出随后可用于设置变量值。

选项2(Javascript):

Java Script步骤具有getInputRowMeta()功能 返回RowMetaInterface对象。

此外,您可以调用getValueMetaList()方法,该方法将返回ValueMetaInterface个对象的列表。然后,您可以遍历列表中的所有项目,并为每个对象调用getName()以获取每个输入字段的名称。

示例(这里的大部分代码实际上只是为了说明输出):

// get instance of RowMetaInterface, which represents metadata of your input row:
var rowMetaInterface = getInputRowMeta();
// get List of ValueMetaInterface objects, which represent each of your input fields:
var valueMetaList = rowMetaInterface.getValueMetaList();
// get this object to be able to iterate over the list:
var iterator = valueMetaList.iterator();

// I am going to save all results into this JS object, then convert it to JSON string and pass into a variable
var allFields = [];
while (iterator.hasNext()) {
    // get current ValueMetaInterface object:
    var valueMetaInterface = iterator.next();
    // The name of current field:
    var fieldName = valueMetaInterface.getName();
    // getType() returns just an internal integer number of the type:
    var typeIndex = valueMetaInterface.getType();
    // typeName will hold the actual String name of the type:
    var typeName = org.pentaho.di.core.row.ValueMetaInterface.typeCodes[typeIndex];

    // You proabably don't need the following part. It is here just for illustration purpose:
    // Prepare data to be converted to JSON:
    var field = {};
    // wrap Java String into a JavaScript String object to be able to convert them into Json
    field.fieldName = String(fieldName);
    field.fieldType = String(typeName);
    allFields.push(field)
}

var allFieldsJson = JSON.stringify(allFields);
/*  scope of the variable:
    "s": System scope
    "r": root
    "p": parent
    "g": grandparent*/
var variableScope = "r";
setVariable("inputFields", allFieldsJson, variableScope);
writeToLog("b", "JSON string:");
writeToLog("b", allFieldsJson);

// In other job/transformation you'll be able to parse json object into a javascript object back:
var fields = JSON.parse(allFieldsJson);
for (var i = 0; i < fields.length; i++) {
    fieldName = fields[i].fieldName;
    fieldType = fields[i].fieldType;
    writeToLog("b", "Field number " + i + ":");
    writeToLog("b", fieldName);
    writeToLog("b", fieldType);
}

在日志中,您将看到以下输出:

2016/10/13 15:33:09 - Modified Java Script Value.0 - Optimization level set to 9.
2016/10/13 15:33:09 - Modified Java Script Value.0 - JSON string:
2016/10/13 15:33:09 - Modified Java Script Value.0 - [{"fieldName":"test","fieldType":"String"},{"fieldName":"test 2","fieldType":"Integer"},{"fieldName":"another field","fieldType":"BigNumber"}]
2016/10/13 15:33:09 - Modified Java Script Value.0 - Field number 0:
2016/10/13 15:33:09 - Modified Java Script Value.0 - test
2016/10/13 15:33:09 - Modified Java Script Value.0 - String
2016/10/13 15:33:09 - Modified Java Script Value.0 - Field number 1:
2016/10/13 15:33:09 - Modified Java Script Value.0 - test 2
2016/10/13 15:33:09 - Modified Java Script Value.0 - Integer
2016/10/13 15:33:09 - Modified Java Script Value.0 - Field number 2:
2016/10/13 15:33:09 - Modified Java Script Value.0 - another field
2016/10/13 15:33:09 - Modified Java Script Value.0 - BigNumber
2016/10/13 15:33:09 - Modified Java Script Value.0 - Finished processing (I=0, O=0, R=1, W=1, U=0, E=0)