如何完全删除Pentaho中用户定义的Java类中的特定输入字段

问题描述 投票:0回答:1

我不理解如何使用Pentaho Data Integration中的User Defined Java Class来完全删除特定的输入字段。

假设我有输入字段ABC。假设我要连接BC中的值(用空格分隔),将结果写入C中,仅保留名称为A的字段和C没有名称为B的字段(实际问题要复杂得多)。我了解如何将结果写入字段C,但是我不知道如何完全删除字段B

private String outFieldName1;
private String outFieldName2;
private String removeFieldName;

private int outFieldIndex1;
private int outFieldIndex2;
private int removeFieldIndex;

private Object[] inputRow;

private int inputRowMetaSize;
private int outputRowMetaSize;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
    inputRow = getRow();
    if (inputRow == null) {
        setOutputDone();
        return false;
    }

    if (first) processMetadata();

    pushOutputRow( get(Fields.In, removeFieldName).getString(inputRow) + " "
                 + get(Fields.In, outFieldName2).getString(inputRow));

    return true;
}

private void processMetadata() throws KettleException {
    outFieldName1 = getParameter("OUT1");
    outFieldName2 = getParameter("OUT2");
    removeFieldName = getParameter("REMOVE");

    outFieldIndex1 = getInputRowMeta().indexOfValue(outFieldName1);
    outFieldIndex2 = getInputRowMeta().indexOfValue(outFieldName2);
    removeFieldIndex = getInputRowMeta().indexOfValue(removeFieldName);

    inputRowMetaSize = data.inputRowMeta.size();
    outputRowMetaSize = data.outputRowMeta.size();

    first=false;
}


private void pushOutputRow(String content) throws KettleException {
    Object[] outRow = RowDataUtil.allocateRowData(outputRowMetaSize);

    for (int fieldN=0; fieldN < inputRow.length; ++fieldN) {
        if(fieldN == outFieldIndex1) {
            outRow[fieldN] = inputRow[fieldN];
        } else if(fieldN == outFieldIndex2) {
            outRow[fieldN] = content;
        } else if(fieldN == removeFieldIndex) {
            outRow[fieldN] = "";
            // Unable to delete this row!
        }

    }

    putRow( data.outputRowMeta, outRow );
}
java pentaho-data-integration
1个回答
0
投票

所需的全部是:

  1. data.outputRowMeta保存在RowMetaInterface类型的变量中(在我的情况下为rowMeta);
  2. 使用要删除的字段的名称或索引为其调用rowMeta.removeValueMeta方法;
  3. 使用rowMeta而不是getInputRowMeta()来搜索输出字段的索引和输出数据量;
  4. putRow()方法中,使用rowMeta作为第一个参数。

``

private String outFieldName1;
private String outFieldName2;
private String removeFieldName;

private int outFieldIndex1;
private int outFieldIndex2;

private Object[] inputRow;

private int inputRowMetaSize;
private int outputRowMetaSize;
private RowMetaInterface rowMeta;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
    inputRow = getRow();
    if (inputRow == null) {
        setOutputDone();
        return false;
    }

    if (first) processMetadata();

    pushOutputRow( get(Fields.In, removeFieldName).getString(inputRow) + " "
                 + get(Fields.In, outFieldName2).getString(inputRow));

    return true;
}

private void processMetadata() throws KettleException {
    outFieldName1 = getParameter("OUT1");
    outFieldName2 = getParameter("OUT2");
    removeFieldName = getParameter("REMOVE");

    inputRowMetaSize = data.inputRowMeta.size();
    outputRowMetaSize = data.outputRowMeta.size();

    rowMeta = data.outputRowMeta;
    rowMeta.removeValueMeta(removeFieldName);

    outFieldIndex1 = rowMeta.indexOfValue(outFieldName1);
    outFieldIndex2 = rowMeta.indexOfValue(outFieldName2);

    outputRowMetaSize = rowMeta.size();

    first=false;
}

private void pushOutputRow(String content) throws KettleException {
    Object[] outRow = RowDataUtil.allocateRowData(outputRowMetaSize);

    for (int fieldN=0; fieldN < inputRow.length; ++fieldN) {

        if(fieldN == outFieldIndex1) {
            outRow[fieldN] = inputRow[fieldN];
        } else if(fieldN == outFieldIndex2) {
            outRow[fieldN] = content;
        }
    }

    putRow( rowMeta, outRow );
}

``

© www.soinside.com 2019 - 2024. All rights reserved.