Hitachi Vantara Pentaho Community Forums
Results 1 to 6 of 6

Thread: Loop through columns & rows

  1. #1

    Post Loop through columns & rows

    I have a table data like below,

    id F_Name R_Name Value S_Name
    1 A <null> <null> AAA
    2 <null> AA 1 <null>
    3 <null> AAB 2 <null>
    4 B <null> <null> BAV
    5 <null> BA 3 <null>
    6 <null> BAB 4 <null>

    Now, I'm trying to loop through each column and row to fill the blank/null with the previous data and add new column to the right titled "Header" with value "yes" for first row , for this i'm writing a javascript but don't know how to loop through the columns and then rows to check the current row value, below is the code that i'm trying with,

    Desired Output:

    id F_Name R_Name Value S_Name Header
    1 A <null> <null> AAA Yes
    2 A AA 1 AAA
    3 A AAB 2 AAA
    4 B <null> <null> BAV Yes
    5 B BA 3 BAV
    6 B BAB 4 BAV

    Columns are dynamic here.

    Code:

    for (i = 1; i <= getInputRowMeta().size(); i++){ var rowMetaName = trim(replace(getInputRowMeta().getValueMeta(i), " String", ""));
    var value = rowMetaName.getString(row[i]);
    var rowMetaVal;
    if (value != null){
    rowMetaVal = value;
    }else{
    value = rowMetaVal;
    }
    }

    please give me some idea and guidance
    Last edited by saianupkumarp; 12-10-2016 at 03:49 PM. Reason: Output changes

  2. #2
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    Here comes guidance
    Attached Files Attached Files
    So long, and thanks for all the fish.

  3. #3

    Default

    Quote Originally Posted by marabu View Post
    Here comes guidance
    Thanks, Marabu. I need some help on the below please,

    What JS method can I use to loop the rows once I start looping the columns?
    How to replace the value in the same column and cell rather creating new as the column names are dynamic, it is hard to hardcode them?

    Your help is appreciated on this.

  4. #4

    Default

    Quote Originally Posted by saianupkumarp View Post
    Thanks, Marabu. I need some help on the below please,

    What JS method can I use to loop the rows once I start looping the columns?
    How to replace the value in the same column and cell rather creating new as the column names are dynamic, it is hard to hardcode them?

    Your help is appreciated on this.
    I tried something like below, and seems like looping is working but am I doing something wrong in replace function? Again my goal is to replace the value with the previous one if it's null

    for (i = 1; i <= getInputRowMeta().size(); i++){
    var rowMetaName = trim(replace(getInputRowMeta().getValueMeta(i), " String", ""));
    var value = row[i];
    var rowMetaVal;
    if (rowMetaName != "Value" && rowMetaName != "Header"){
    if (row[i] != null){
    rowMetaVal = value;
    }else{
    value = replace(value, value, rowMetaVal);
    }
    }
    }

  5. #5

    Default

    Any help on this please, I'm struggling to get the previous row of same column value.

  6. #6

    Default

    Quote Originally Posted by saianupkumarp View Post
    Any help on this please, I'm struggling to get the previous row of same column value.
    Fixed the issue, below is the code which makes all null values = previous row for all columns.

    Code:
    Object[] previousRow;RowMetaInterface outputMeta;
    
    
    public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
    {
        // First, get a row from the default input hop
        Object[] r = getRow();
    
    
        // If the row object is null, we are done processing.
        if (r == null) {
            setOutputDone();
            return false;
        }
    
    
        if (outputMeta == null) {
            outputMeta = data.outputRowMeta.clone();
        for(int i=0; i < outputMeta.size(); i++) {
            ValueMetaInterface meta = outputMeta.getValueMeta(i);
            if (meta.getType() == ValueMetaInterface.TYPE_NUMBER) {
                meta.setPrecision(4);
                meta.setConversionMask("#");
            }
        }
    }
    
    
    // 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.
    Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
    
    
    // copy all input fields to the output fields
    
    
    for (int i=0; i < getInputRowMeta().size(); i++) {
        if ((r[i] == null) && (previousRow != null)) {
            // if the current field is empty take it from the previous row
            outputRow[i] = previousRow[i];
        }
        else {
            // otherwise use the current row
            outputRow[i] = r[i];
        }     
    }
    
    
    putRow(outputMeta, outputRow);
    // store the current row as future previous row
    previousRow = outputMeta.cloneRow(outputRow);
    
    
    return true;
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.