Java Code Examples for org.pentaho.di.core.row.RowDataUtil#createResizedCopy()

The following examples show how to use org.pentaho.di.core.row.RowDataUtil#createResizedCopy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object[] createRowCopy( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
  Object FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      int newSize = (int) Math.round( (Double) ArgList[0] );

      Object scmO = actualObject.get( "row" );
      Object[] row = (Object[]) scmO; // TODO AKRETION ensure

      return RowDataUtil.createResizedCopy( row, newSize );
    } catch ( Exception e ) {
      throw new RuntimeException( "Unable to create a row copy: " + Const.CR + e.toString() );
    }
  } else {
    throw new RuntimeException(
      "The function call createRowCopy requires a single arguments : the new size of the row" );
  }
}
 
Example 2
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object[] createRowCopy( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      int newSize = (int) Math.round( Context.toNumber( ArgList[0] ) );

      Object scmO = actualObject.get( "row", actualObject );
      Object[] row = (Object[]) Context.jsToJava( scmO, ( new Object[] {} ).getClass() );

      return RowDataUtil.createResizedCopy( row, newSize );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "Unable to create a row copy: " + Const.CR + e.toString() );
    }
  } else {
    throw Context
      .reportRuntimeError( "The function call createRowCopy requires a single arguments : the new size of the row" );
  }
}
 
Example 3
Source File: JsonInput.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * allocates out row
 */
private Object[] buildBaseOutputRow() {
  Object[] outputRowData;
  if ( data.readrow != null ) {
    if ( meta.isRemoveSourceField() && data.indexSourceField > -1 ) {
      // skip the source field in the output array
      int sz = data.readrow.length;
      outputRowData = RowDataUtil.allocateRowData( data.outputRowMeta.size() );
      int ii = 0;
      for ( int i = 0; i < sz; i++ ) {
        if ( i != data.indexSourceField ) {
          outputRowData[ ii++ ] = data.readrow[ i ];
        }
      }
    } else {
      outputRowData = RowDataUtil.createResizedCopy( data.readrow, data.outputRowMeta.size() );
    }
  } else {
    outputRowData = RowDataUtil.allocateRowData( data.outputRowMeta.size() );
  }
  return outputRowData;
}
 
Example 4
Source File: TransformClassBase.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public Object[] createOutputRow( Object[] inputRow, int outputRowSize ) {
  if ( meta.isClearingResultFields() ) {
    return RowDataUtil.allocateRowData( outputRowSize );
  } else {
    return RowDataUtil.createResizedCopy( inputRow, outputRowSize );
  }
}
 
Example 5
Source File: KettleBeamUtil.java    From kettle-beam with Apache License 2.0 4 votes vote down vote up
public static final KettleRow copyKettleRow( KettleRow kettleRow, RowMetaInterface rowMeta ) throws KettleException {
  Object[] newRow = RowDataUtil.createResizedCopy(kettleRow.getRow(), rowMeta.size());
  return new KettleRow(newRow);
}
 
Example 6
Source File: Neo4JOutput.java    From knowbi-pentaho-pdi-neo4j-output with Apache License 2.0 4 votes vote down vote up
private void outputGraphValue( RowMetaInterface rowMeta, Object[] row ) throws KettleException {

    try {

      GraphData graphData = new GraphData();
      graphData.setSourceTransformationName( getTransMeta().getName() );
      graphData.setSourceStepName( getStepMeta().getName() );

      GraphNodeData sourceNodeData = null;
      GraphNodeData targetNodeData = null;
      GraphRelationshipData relationshipData;

      if ( meta.getFromNodeProps().length > 0 ) {
        sourceNodeData = createGraphNodeData( rowMeta, row, meta.getFromNodeLabels(), data.fromLabelValues, data.fromNodeLabelIndexes,
          data.fromNodePropIndexes, meta.getFromNodePropNames(), meta.getFromNodePropPrimary(), "from" );
        if ( !meta.isOnlyCreatingRelationships() ) {
          graphData.getNodes().add( sourceNodeData );
        }
      }
      if ( meta.getToNodeProps().length > 0 ) {
        targetNodeData = createGraphNodeData( rowMeta, row, meta.getToNodeLabels(), data.toLabelValues, data.toNodeLabelIndexes,
          data.toNodePropIndexes, meta.getToNodePropNames(), meta.getToNodePropPrimary(), "to" );
        if ( !meta.isOnlyCreatingRelationships() ) {
          graphData.getNodes().add( targetNodeData );
        }
      }

      String relationshipLabel = null;
      if ( data.relationshipIndex >= 0 ) {
        relationshipLabel = getInputRowMeta().getString( row, data.relationshipIndex );
      }
      if ( StringUtil.isEmpty( relationshipLabel ) && StringUtils.isNotEmpty( data.relationshipLabelValue ) ) {
        relationshipLabel = data.relationshipLabelValue;
      }
      if ( sourceNodeData != null && targetNodeData != null && StringUtils.isNotEmpty( relationshipLabel ) ) {

        relationshipData = new GraphRelationshipData();
        relationshipData.setSourceNodeId( sourceNodeData.getId() );
        relationshipData.setTargetNodeId( targetNodeData.getId() );
        relationshipData.setLabel( relationshipLabel );
        relationshipData.setId( sourceNodeData.getId() + " -> " + targetNodeData.getId() );
        relationshipData.setPropertySetId( "relationship" );

        // Add relationship properties...
        //
        // Set the properties
        //
        for ( int i = 0; i < data.relPropIndexes.length; i++ ) {

          ValueMetaInterface valueMeta = rowMeta.getValueMeta( data.relPropIndexes[ i ] );
          Object valueData = row[ data.relPropIndexes[ i ] ];

          String propertyName = meta.getRelPropNames()[ i ];
          GraphPropertyDataType propertyType = GraphPropertyDataType.getTypeFromKettle( valueMeta );
          Object propertyNeoValue = propertyType.convertFromKettle( valueMeta, valueData );
          boolean propertyPrimary = false;

          relationshipData.getProperties().add(
            new GraphPropertyData( propertyName, propertyNeoValue, propertyType, propertyPrimary )
          );
        }

        graphData.getRelationships().add( relationshipData );
      }

      // Pass it forward...
      //
      Object[] outputRowData = RowDataUtil.createResizedCopy( row, data.outputRowMeta.size() );
      int startIndex = rowMeta.size();
      outputRowData[ rowMeta.size() ] = graphData;
      putRow( data.outputRowMeta, outputRowData );

    } catch ( Exception e ) {
      throw new KettleException( "Unable to calculate graph output value", e );
    }
  }
 
Example 7
Source File: AvroNestedReader.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
private Object[][] setKettleFields( Object[] outputRowData, VariableSpace space ) throws KettleException {
  Object[][] result = null;

  // expand map/array in path structure to multiple rows (if necessary)
  if ( m_expansionHandler != null ) {
    m_expansionHandler.reset( space );

    if ( m_schemaToUse.getType() == Schema.Type.RECORD || m_schemaToUse.getType() == Schema.Type.UNION ) {
      // call getSchema() on the top level record here in case it has been
      // read as one of the elements from a top-level union
      result =
        m_expansionHandler
          .convertToKettleValues( m_topLevelRecord, m_topLevelRecord.getSchema(), m_defaultSchema, space,
            m_dontComplainAboutMissingFields );
    } else if ( m_schemaToUse.getType() == Schema.Type.ARRAY ) {
      result =
        m_expansionHandler.convertToKettleValues( m_topLevelArray, m_schemaToUse, m_defaultSchema, space,
          m_dontComplainAboutMissingFields );
    } else {
      result =
        m_expansionHandler.convertToKettleValues( m_topLevelMap, m_schemaToUse, m_defaultSchema, space,
          m_dontComplainAboutMissingFields );
    }
  } else {
    result = new Object[ 1 ][];
  }

  // if there are no incoming rows (i.e. we're decoding from a file rather
  // than a field
  if ( outputRowData == null ) {
    outputRowData = RowDataUtil.allocateRowData( m_outputRowMeta.size() );
  } else {
    // make sure we allocate enough space for the new fields
    outputRowData = RowDataUtil.createResizedCopy( outputRowData, m_outputRowMeta.size() );
  }

  // get the normal (non expansion-related fields)
  Object value = null;
  int incomingFieldsOffset = m_outputRowMeta.size() - m_normalFields.size();

  for ( AvroInputField f : m_normalFields ) {
    resetField( f, space );

    if ( m_schemaToUse.getType() == Schema.Type.RECORD || m_schemaToUse.getType() == Schema.Type.UNION ) {
      // call getSchema() on the top level record here in case it has been
      // read as one of the elements from a top-level union
      value =
        convertToKettleValue( f, m_topLevelRecord, m_topLevelRecord.getSchema(), m_defaultSchema,
          m_dontComplainAboutMissingFields );
    } else if ( m_schemaToUse.getType() == Schema.Type.ARRAY ) {
      value =
        convertToKettleValue( f, m_topLevelArray, m_schemaToUse, m_defaultSchema, m_dontComplainAboutMissingFields );
    } else {
      value =
        convertToKettleValue( f, m_topLevelMap, m_schemaToUse, m_defaultSchema, m_dontComplainAboutMissingFields );
    }

    outputRowData[ f.getOutputIndex() + incomingFieldsOffset ] = value;
  }

  // copy normal fields and existing incoming over to each expansion row (if
  // necessary)
  if ( m_expansionHandler == null ) {
    result[ 0 ] = outputRowData;
  } else if ( m_normalFields.size() > 0 || m_newFieldOffset > 0 ) {
    for ( int i = 0; i < result.length; i++ ) {
      Object[] row = result[ i ];

      // existing incoming fields
      for ( int j = 0; j < m_newFieldOffset; j++ ) {
        row[ j ] = outputRowData[ j ];
      }
      int rowIndex = 0;
      for ( int x = 0; x < outputRowData.length; x++ ) {
        if ( outputRowData[ x ] != null && rowIndex < row.length ) {
          row[ rowIndex++ ] = outputRowData[ x ];
        }
      }
    }
  }

  return result;
}
 
Example 8
Source File: SplitFieldToRows.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private boolean splitField( RowMetaInterface rowMeta, Object[] rowData ) throws KettleException {
  if ( first ) {
    first = false;

    data.outputRowMeta = getInputRowMeta().clone();
    meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore );

    String realSplitFieldName = environmentSubstitute( meta.getSplitField() );
    data.fieldnr = rowMeta.indexOfValue( realSplitFieldName );

    int numErrors = 0;
    if ( Utils.isEmpty( meta.getNewFieldname() ) ) {
      logError( BaseMessages.getString( PKG, "SplitFieldToRows.Log.NewFieldNameIsNull" ) );
      numErrors++;
    }

    if ( data.fieldnr < 0 ) {
      logError( BaseMessages
        .getString( PKG, "SplitFieldToRows.Log.CouldNotFindFieldToSplit", realSplitFieldName ) );
      numErrors++;
    }

    if ( !rowMeta.getValueMeta( data.fieldnr ).isString() ) {
      logError( BaseMessages.getString( PKG, "SplitFieldToRows.Log.SplitFieldNotValid", realSplitFieldName ) );
      numErrors++;
    }

    if ( meta.includeRowNumber() ) {
      String realRowNumberField = environmentSubstitute( meta.getRowNumberField() );
      if ( Utils.isEmpty( realRowNumberField ) ) {
        logError( BaseMessages.getString( PKG, "SplitFieldToRows.Exception.RownrFieldMissing" ) );
        numErrors++;
      }
    }

    if ( numErrors > 0 ) {
      setErrors( numErrors );
      stopAll();
      return false;
    }

    data.splitMeta = rowMeta.getValueMeta( data.fieldnr );
  }

  String originalString = data.splitMeta.getString( rowData[data.fieldnr] );
  if ( originalString == null ) {
    originalString = "";
  }

  if ( meta.includeRowNumber() && meta.resetRowNumber() ) {
    data.rownr = 1L;
  }
  // use -1 for include all strings. see http://jira.pentaho.com/browse/PDI-11477
  String[] splitStrings = data.delimiterPattern.split( originalString, -1 );
  for ( String string : splitStrings ) {
    Object[] outputRow = RowDataUtil.createResizedCopy( rowData, data.outputRowMeta.size() );
    outputRow[rowMeta.size()] = string;
    // Include row number in output?
    if ( meta.includeRowNumber() ) {
      outputRow[rowMeta.size() + 1] = data.rownr;
    }
    putRow( data.outputRowMeta, outputRow );
    data.rownr++;
  }

  return true;
}
 
Example 9
Source File: WebService.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private Object[] createNewRow( Object[] inputRowData ) {
  return inputRowData == null ? RowDataUtil.allocateRowData( data.outputRowMeta.size() ) : RowDataUtil
    .createResizedCopy( inputRowData, data.outputRowMeta.size() );
}
 
Example 10
Source File: HL7Input.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (HL7InputMeta) smi;
  data = (HL7InputData) sdi;

  Object[] r = getRow(); // get row, set busy!
  if ( r == null ) { // no more input to be expected...
    setOutputDone();
    return false;
  }

  if ( first ) {
    data.messageFieldIndex = getInputRowMeta().indexOfValue( meta.getMessageField() );
    if ( data.messageFieldIndex < 0 ) {
      throw new KettleException( "Unable to find field [" + meta.getMessageField() + "] in the input fields." );
    }

    data.outputRowMeta = getInputRowMeta().clone();
    meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore );

    data.parser = new GenericParser();
    data.parser.setValidationContext( new NoValidation() );
  }

  String messageString = getInputRowMeta().getString( r, data.messageFieldIndex );

  try {
    Message message = data.parser.parse( messageString );
    List<HL7Value> values = HL7KettleParser.extractValues( message );

    for ( HL7Value value : values ) {
      Object[] output = RowDataUtil.createResizedCopy( r, data.outputRowMeta.size() );
      int outputIndex = getInputRowMeta().size();

      output[outputIndex++] = value.getParentGroup();
      output[outputIndex++] = value.getGroupName();
      output[outputIndex++] = value.getVersion();
      output[outputIndex++] = value.getStructureName();
      output[outputIndex++] = value.getStructureNumber();
      output[outputIndex++] = value.getFieldName();
      output[outputIndex++] = value.getCoordinate();
      output[outputIndex++] = value.getDataType();
      output[outputIndex++] = value.getDescription();
      output[outputIndex++] = value.getValue();

      putRow( data.outputRowMeta, output );
    }
  } catch ( Exception e ) {
    throw new KettleException( "Error parsing message", e );
  }

  if ( checkFeedback( getLinesWritten() ) ) {
    if ( log.isBasic() ) {
      logBasic( BaseMessages.getString( PKG, "HL7Input.Log.LineNumber" ) + getLinesWritten() );
    }
  }

  return true;
}