Pentaho Kettle - 从二进制类型的字段中将十六进制转换为数字



我需要使用 Kettle/PDI 社区版本来读取大型固定长度的数据文件,并对它们做一些 ETL 操作。 在开发阶段,我遇到了以下问题:

  • 水壶插件"固定文件输入"允许多种数据类型,并备注它们实际上是字符串或字节数组。

  • 我的输入包含两者:字符串和字节数组对应于长,整数和短的小端表示(英特尔特定的字节序)。要读取的记录结构示例: 列 1(字符:8), 列 2(长:8 十六进制), 列 3(字符:2),列 4(整数:4 十六进制).

我尝试使用"选择值"插件并将列的二进制类型更改为整数,但未实现此方法。 最后,我以以下解决方案结束:

  • 我使用了"用户定义的Java类",代码粘贴在下面。

如您所见,我使用了一个公式来获得多头值。

   public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{       
    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());      
    // Get the value from an input field
    byte[] buf;
    long  longValue;
    // BAN_L - 8 bytes
    buf= get(Fields.In, "BAN").getBinary(r);      
    longValue=  ((buf[0] & 0xFFL) << 0) | ((buf[1] & 0xFFL) << 8)
                | ((buf[2] & 0xFFL) << 16) | ((buf[3] & 0xFFL) << 24)
                | ((buf[4] & 0xFFL) << 32) | ((buf[5] & 0xFFL) << 40)
                | ((buf[6] & 0xFFL) << 48) | ((buf[7] & 0xFFL) << 56);      
    get(Fields.Out, "BAN_L").setValue(r, longValue);
    //DEPOSIT_PAID_AMT -4 bytes
    buf = get(Fields.In, "DEPOSIT_PAID_AMT").getBinary(r);
    longValue=  ((buf[0] & 0xFFL) << 0) | ((buf[1] & 0xFFL) << 8)
                | ((buf[2] & 0xFFL) << 16) | ((buf[3] & 0xFFL) << 24);
    get(Fields.Out, "DEPOSIT_PAID_AMT_L").setValue(r, longValue);
    //BILL_SEQ_NO_L -2 bytes
    buf = get(Fields.In, "BILL_SEQ_NO").getBinary(r);
    longValue =  ((buf[0] & 0xFFL) << 0) | ((buf[1] & 0xFFL) << 8);
    get(Fields.Out, "BILL_SEQ_NO_L").setValue(r, longValue);    

    // Send the row on to the next step.
    putRow(data.outputRowMeta, r);
    //binaryToDecimal();
    return true;
}

当我在一个数据中提取 8-20 个二进制字段时出现问题。这种方法是否有其他选择,所以我可以称之为:

getNumberFromLE(byte [] buff, buff.length);    

有没有其他插件在开发中,可以用来将byte[]转换为Pentaho Kettle"数字"数据类型?(大数和整数也很好)。

我发现了以下可能性:

1)可以向ValueMetaInterface类添加其他类型:

org.pentaho.di.core.row.ValueMetaInterface

并将转换函数添加到

org.pentaho.di.core.row.ValueMeta

2)将代码片段实现getNumberFromLE添加到"用户定义的Java类"的"Common Use"代码截图中

3)添加作为插件的新数据类型,如下面的两个链接中所述: Jira 可插拔类型 GitHub pdi-valuemeta-map 添加数据类型

最新更新