Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface#convertDataFromString()

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#convertDataFromString() . 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: EditRowsDialog.java    From pentaho-pdi-dataset with Apache License 2.0 6 votes vote down vote up
private Object[] getRowForData( TableItem item, int rowNr ) throws KettleException {
  try {
    Object[] row = RowDataUtil.allocateRowData( rowMeta.size() );
    for ( int i = 0; i < rowMeta.size(); i++ ) {
      ValueMetaInterface valueMeta = rowMeta.getValueMeta( i );
      ValueMetaInterface stringValueMeta = stringRowMeta.getValueMeta( i );

      int colnr = i + 1;
      if ( GUIResource.getInstance().getColorBlue().equals( item.getForeground( colnr ) ) ) {
        row[ i ] = null; // <null> value
      } else {
        String string = item.getText( colnr );
        row[ i ] = valueMeta.convertDataFromString( string, stringValueMeta,
          null, null, ValueMetaInterface.TRIM_TYPE_NONE );
      }
    }
    return row;
  } catch ( KettleException e ) {
    throw new KettleException( BaseMessages.getString( PKG, "EditRowsDialog.Error.ErrorGettingRowForData",
      Integer.toString( rowNr ) ), e );
  }
}
 
Example 2
Source File: EditRowsDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Object[] getRowForData( TableItem item, int rowNr ) throws KettleException {
  try {
    Object[] row = RowDataUtil.allocateRowData( rowMeta.size() );
    for ( int i = 0; i < rowMeta.size(); i++ ) {
      ValueMetaInterface valueMeta = rowMeta.getValueMeta( i );
      ValueMetaInterface stringValueMeta = stringRowMeta.getValueMeta( i );

      int colnr = i + 1;
      if ( isDisplayingNullValue( item, colnr ) ) {
        row[i] = null; // <null> value
      } else {
        String string = item.getText( colnr );
        if ( stringValueMeta.isNull( string ) ) {
          string = null;
        }
        row[i] = valueMeta.convertDataFromString( string, stringValueMeta,
          null, null, ValueMetaInterface.TRIM_TYPE_NONE );
      }
    }
    return row;
  } catch ( KettleException e ) {
    throw new KettleException( BaseMessages.getString( PKG, "EditRowsDialog.Error.ErrorGettingRowForData",
      Integer.toString( rowNr ) ), e );
  }
}
 
Example 3
Source File: RowOutputConverter.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private Object getValue( ValueMetaInterface targetMeta, ValueMetaInterface strConvertMeta, Object value )
    throws KettleValueException {
  if ( targetMeta.isNumeric() ) {
    try {
      // attempt direct conversion
      return targetMeta.getNativeDataType( value );
    } catch ( KettleValueException e ) {
      if ( log.isDebug() ) {
        log.logDebug( e.getLocalizedMessage(), e );
      }
    }
  }
  // convert from string
  String strValue = getStringValue( value );
  return targetMeta.convertDataFromString( strValue, strConvertMeta, null, null, targetMeta.getTrimType() );
}
 
Example 4
Source File: StringToKettleFn.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement( ProcessContext processContext ) {

  try {

    String inputString = processContext.element();
    inputCounter.inc();

    String[] components = inputString.split( separator, -1 );

    // TODO: implement enclosure in FileDefinition
    //

    Object[] row = RowDataUtil.allocateRowData( rowMeta.size() );
    int index = 0;
    while ( index < rowMeta.size() && index < components.length ) {
      String sourceString = components[ index ];
      ValueMetaInterface valueMeta = rowMeta.getValueMeta( index );
      ValueMetaInterface stringMeta = new ValueMetaString( "SourceString" );
      stringMeta.setConversionMask( valueMeta.getConversionMask() );
      try {
        row[ index ] = valueMeta.convertDataFromString( sourceString, stringMeta, null, null, ValueMetaInterface.TRIM_TYPE_NONE );
      } catch ( KettleValueException ve ) {
        throw new KettleException( "Unable to convert value '" + sourceString + "' to value : " + valueMeta.toStringMeta(), ve );
      }
      index++;
    }

    // Pass the row to the process context
    //
    processContext.output( new KettleRow( row ) );
    writtenCounter.inc();

  } catch ( Exception e ) {
    Metrics.counter( "error", stepname ).inc();
    LOG.error( "Error converting input data into Kettle rows " + processContext.element() + ", " + e.getMessage() );
    throw new RuntimeException( "Error converting input data into Kettle rows", e );

  }
}