Java Code Examples for org.pentaho.di.core.RowMetaAndData#getRowMeta()

The following examples show how to use org.pentaho.di.core.RowMetaAndData#getRowMeta() . 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: KettleDatabaseRepositoryConnectionDelegate.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public synchronized ObjectId getIDWithValue( String tablename, String idfield, String lookupfield, String value,
                                             String lookupkey, ObjectId key ) throws KettleException {
  RowMetaAndData par = new RowMetaAndData();
  par.addValue( new ValueMetaString( "value" ), value );
  par.addValue( new ValueMetaInteger( "key" ), new LongObjectId( key ) );
  RowMetaAndData result =
    getOneRow( "SELECT "
      + idfield + " FROM " + tablename + " WHERE " + lookupfield + " = ? AND " + lookupkey + " = ?", par
      .getRowMeta(), par.getData() );

  if ( result != null && result.getRowMeta() != null && result.getData() != null && result.isNumeric( 0 ) ) {
    return new LongObjectId( result.getInteger( 0, 0 ) );
  }

  return null;
}
 
Example 2
Source File: KettleDatabaseRepositoryConnectionDelegate.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public synchronized ObjectId getIDWithValue( String tablename, String idfield, String[] lookupkey, ObjectId[] key )
  throws KettleException {
  RowMetaAndData par = new RowMetaAndData();
  String sql = "SELECT " + idfield + " FROM " + tablename + " ";

  for ( int i = 0; i < lookupkey.length; i++ ) {
    if ( i == 0 ) {
      sql += "WHERE ";
    } else {
      sql += "AND   ";
    }
    par.addValue( new ValueMetaInteger( lookupkey[ i ] ), new LongObjectId( key[ i ] ) );
    sql += lookupkey[ i ] + " = ? ";
  }
  RowMetaAndData result = getOneRow( sql, par.getRowMeta(), par.getData() );
  if ( result != null && result.getRowMeta() != null && result.getData() != null && result.isNumeric( 0 ) ) {
    return new LongObjectId( result.getInteger( 0, 0 ) );
  }
  return null;
}
 
Example 3
Source File: KettleDatabaseRepositoryConnectionDelegate.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public synchronized LongObjectId getIDWithValue( String tablename, String idfield, String lookupfield,
                                                 String value, String[] lookupkey, ObjectId[] key )
  throws KettleException {
  RowMetaAndData par = new RowMetaAndData();
  par.addValue( new ValueMetaString( lookupfield ), value );

  String sql = "SELECT " + idfield + " FROM " + tablename + " WHERE " + lookupfield + " = ? ";

  for ( int i = 0; i < lookupkey.length; i++ ) {
    par.addValue( new ValueMetaInteger( lookupkey[ i ] ), new LongObjectId( key[ i ] ) );
    sql += "AND " + lookupkey[ i ] + " = ? ";
  }

  RowMetaAndData result = getOneRow( sql, par.getRowMeta(), par.getData() );
  if ( result != null && result.getRowMeta() != null && result.getData() != null && result.isNumeric( 0 ) ) {
    return new LongObjectId( result.getInteger( 0, 0 ) );
  }
  return null;
}
 
Example 4
Source File: NormaliserTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void checkResults( List<RowMetaAndData> expectedOutput, List<RowMetaAndData> outputList ) {
  assertEquals( expectedOutput.size(), outputList.size() );
  for ( int i = 0; i < outputList.size(); i++ ) {
    RowMetaAndData aRowMetaAndData = outputList.get( i );
    RowMetaAndData expectedRowMetaAndData = expectedOutput.get( i );
    RowMetaInterface rowMeta = aRowMetaAndData.getRowMeta();
    RowMetaInterface expectedRowMeta = expectedRowMetaAndData.getRowMeta();
    String[] fields = rowMeta.getFieldNames();
    String[] expectedFields = expectedRowMeta.getFieldNames();
    assertEquals( expectedFields.length, fields.length );
    assertArrayEquals( expectedFields, fields );
    Object[] aRow = aRowMetaAndData.getData();
    Object[] expectedRow = expectedRowMetaAndData.getData();
    assertEquals( expectedRow.length, aRow.length );
    assertArrayEquals( expectedRow, aRow );
  }
}
 
Example 5
Source File: SortRowsIT.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Check the list, the list has to be sorted.
 */
void checkStringRows( List<RowMetaAndData> rows, boolean ascending ) throws Exception {
  String prev_key1 = null, prev_key2 = null;
  int idx = 0;

  for ( RowMetaAndData rm : rows ) {
    Object[] r1 = rm.getData();
    RowMetaInterface rmi = rm.getRowMeta();

    String key1 = rmi.getString( r1, "KEY1", "" );
    String key2 = rmi.getString( r1, "KEY2", "" );

    if ( prev_key1 != null && prev_key2 != null ) {
      if ( ascending ) {
        if ( prev_key1.compareTo( key1 ) == 0 ) {
          if ( prev_key2.compareTo( key2 ) > 0 ) {
            fail( "error in sort" );
          }
        } else if ( prev_key1.compareTo( key1 ) > 0 ) {
          fail( "error in sort" );
        }
      } else {
        if ( prev_key1.compareTo( key1 ) == 0 ) {
          if ( prev_key2.compareTo( key2 ) < 0 ) {
            fail( "error in sort" );
          }
        } else if ( prev_key1.compareTo( key1 ) < 0 ) {
          fail( "error in sort" );
        }
      }
    }
    prev_key1 = key1;
    prev_key2 = key2;
    idx++;
  }
  Assert.assertEquals( "less rows returned than expected", MAX_COUNT, idx );
}
 
Example 6
Source File: RestInputIT.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testRESTInput() throws Exception {
  //
  // Create a new transformation...
  //
  TransMeta transMeta = new TransMeta();
  transMeta.setName( "restinput" );

  PluginRegistry registry = PluginRegistry.getInstance();

  StepMeta inputStep = createRestInputStep( transMeta, registry );

  RowStepCollector rowStepCollector = new RowStepCollector();
  final Trans trans = createAndTestTrans( registry, transMeta, inputStep, rowStepCollector, "limit", 5 );
  trans.startThreads();

  trans.waitUntilFinished();

  // Compare the results
  List<RowMetaAndData> resultRows = rowStepCollector.getRowsWritten();

  assertTrue( rowStepCollector.getRowsError().isEmpty() );
  assertEquals( 1, rowStepCollector.getRowsWritten().size() );

  final RowMetaAndData rowMetaAndData = resultRows.get( 0 );
  final RowMetaInterface rowMeta = rowMetaAndData.getRowMeta();

  final String[] fieldNames = rowMeta.getFieldNames();
  final Object[] data = rowMetaAndData.getData();

  assertEquals( "pageSize", fieldNames[0] );
  assertEquals( "name", fieldNames[1] );
  assertEquals( "result", fieldNames[2] );

  assertEquals( Integer.valueOf( 5 ), data[0] );
  assertEquals( "limit", data[1] );
  assertEquals( "limit:5", data[2] );
}
 
Example 7
Source File: KettleDatabaseRepositoryConnectionDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public synchronized LongObjectId getIDWithValue( String tablename, String idfield, String lookupfield,
                                                 String value ) throws KettleException {
  RowMetaAndData par = new RowMetaAndData();
  par.addValue( new ValueMetaString( "value" ), value );
  RowMetaAndData result = getOneRow(
    "SELECT " + idfield + " FROM " + tablename + " WHERE " + lookupfield + " = ?", par.getRowMeta(), par
      .getData() );

  if ( result != null && result.getRowMeta() != null && result.getData() != null && result.isNumeric( 0 ) ) {
    return new LongObjectId( result.getInteger( 0, 0 ) );
  }
  return null;
}
 
Example 8
Source File: RowsFromResult.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  Result previousResult = getTrans().getPreviousResult();
  if ( previousResult == null || getLinesRead() >= previousResult.getRows().size() ) {
    setOutputDone();
    return false;
  }
  RowMetaAndData row = previousResult.getRows().get( (int) getLinesRead() );
  incrementLinesRead();

  data = (RowsFromResultData) sdi;

  // We don't get the meta-data from the previous steps (there aren't any) but from the previous transformation or job
  //
  data.outputRowMeta = row.getRowMeta();

  // copy row to possible alternate rowset(s).
  //
  putRow( data.outputRowMeta, row.getData() );

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

  return true;
}
 
Example 9
Source File: RowGenerator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
  try {
    meta = (RowGeneratorMeta) smi;
    data = (RowGeneratorData) sdi;

    if ( super.init( smi, sdi ) ) {
      // Determine the number of rows to generate...
      data.rowLimit = Const.toLong( environmentSubstitute( meta.getRowLimit() ), -1L );
      data.rowsWritten = 0L;
      data.delay = Const.toLong( environmentSubstitute( meta.getIntervalInMs() ), -1L );

      if ( data.rowLimit < 0L ) { // Unable to parse
        logError( BaseMessages.getString( PKG, "RowGenerator.Wrong.RowLimit.Number" ) );
        return false; // fail
      }

      // Create a row (constants) with all the values in it...
      List<CheckResultInterface> remarks = new ArrayList<CheckResultInterface>(); // stores the errors...
      RowMetaAndData outputRow = buildRow( meta, remarks, getStepname() );
      if ( !remarks.isEmpty() ) {
        for ( int i = 0; i < remarks.size(); i++ ) {
          CheckResult cr = (CheckResult) remarks.get( i );
          logError( cr.getText() );
        }
        return false;
      }

      data.outputRowData = outputRow.getData();
      data.outputRowMeta = outputRow.getRowMeta();
      return true;
    }
    return false;
  } catch ( Exception e ) {
    setErrors( 1L );
    logError( "Error initializing step", e );
    return false;
  }
}
 
Example 10
Source File: SelectRowDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Copy information from the input buffer to the dialog fields.
 */
private void getData() {
  for ( int i = 0; i < buffer.size(); i++ ) {
    RowMetaAndData rowMetaAndData = buffer.get( i );
    RowMetaInterface rowMeta = rowMetaAndData.getRowMeta();
    Object[] rowData = rowMetaAndData.getData();

    for ( int c = 0; c < rowMeta.size(); c++ ) {
      ValueMetaInterface v = rowMeta.getValueMeta( c );
      String show;

      try {
        if ( v.isNumeric() ) {
          show = v.getString( rowData[c] );
        } else {
          show = v.getString( rowData[c] );
        }
      } catch ( KettleValueException e ) {
        show = "<conversion error>";
      }
      if ( show != null ) {
        wFields.table.getItem( i ).setText( c + 1, show );
      }
    }
  }
  wFields.optWidth( true );
}
 
Example 11
Source File: AccessOutputTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private static AccessInputField[] getInputFields( List<RowMetaAndData> in ) {
  RowMetaAndData rmd = in.get( 0 );
  RowMetaInterface rm = rmd.getRowMeta();
  List<AccessInputField> retval = new ArrayList<AccessInputField>();
  for ( ValueMetaInterface value : rm.getValueMetaList() ) {
    AccessInputField field = new AccessInputField( value.getName() );
    field.setColumn( value.getName() );
    field.setType( value.getType() );
    retval.add( field );
  }
  return retval.toArray( new AccessInputField[retval.size()] );
}
 
Example 12
Source File: MetaInject.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * package-local visibility for testing purposes
 */
void setEntryValueIfFieldExists( StepInjectionMetaEntry entry, RowMetaAndData row, SourceStepField source )
  throws KettleValueException {
  RowMetaInterface rowMeta = row.getRowMeta();
  if ( rowMeta.indexOfValue( source.getField() ) < 0 ) {
    return;
  }
  setEntryValue( entry, row, source );
}
 
Example 13
Source File: TestUtilities.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Check the 2 lists comparing the rows in order. If they are not the same fail the test.
 *
 * @param rows1          set 1 of rows to compare
 * @param rows2          set 2 of rows to compare
 * @param fileNameColumn Number of the column containing the filename. This is only checked for being non-null (some
 *                       systems maybe canonize names differently than we input).
 */
public static void checkRows( List<RowMetaAndData> rows1, List<RowMetaAndData> rows2, int fileNameColumn )
  throws TestFailedException {

  int idx = 1;
  if ( rows1.size() != rows2.size() ) {
    throw new TestFailedException( "Number of rows is not the same: " + rows1.size() + " and " + rows2.size() );
  }
  Iterator<RowMetaAndData> itrRows1 = rows1.iterator();
  Iterator<RowMetaAndData> itrRows2 = rows2.iterator();

  while ( itrRows1.hasNext() && itrRows2.hasNext() ) {
    RowMetaAndData rowMetaAndData1 = itrRows1.next();
    RowMetaAndData rowMetaAndData2 = itrRows2.next();

    RowMetaInterface rowMetaInterface1 = rowMetaAndData1.getRowMeta();

    Object[] rowObject1 = rowMetaAndData1.getData();
    Object[] rowObject2 = rowMetaAndData2.getData();

    if ( rowMetaAndData1.size() != rowMetaAndData2.size() ) {
      throw new TestFailedException( "row number " + idx + " is not equal" );
    }

    int[] fields = new int[ rowMetaInterface1.size() ];
    for ( int ydx = 0; ydx < rowMetaInterface1.size(); ydx++ ) {
      fields[ ydx ] = ydx;
    }

    try {
      if ( fileNameColumn >= 0 ) {
        rowObject1[ fileNameColumn ] = rowObject2[ fileNameColumn ];
      }
      if ( rowMetaAndData1.getRowMeta().compare( rowObject1, rowObject2, fields ) != 0 ) {
        throw new ComparisonFailure( "row nr " + idx + " is not equal",
        rowMetaInterface1.getString( rowObject1 ),
        rowMetaInterface1.getString( rowObject2 ) ); 
      }
    } catch ( KettleValueException e ) {
      throw new TestFailedException( "row nr " + idx + " is not equal" );
    }
    idx++;
  }
}
 
Example 14
Source File: Database.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public void writeLogRecord( LogTableCoreInterface logTable, LogStatus status, Object subject, Object parent )
  throws KettleDatabaseException {
  try {
    RowMetaAndData logRecord = logTable.getLogRecord( status, subject, parent );
    if ( logRecord == null ) {
      return;
    }

    boolean update = ( logTable.getKeyField() != null ) && !status.equals( LogStatus.START );
    String schemaTable =
      databaseMeta.getQuotedSchemaTableCombination(
        environmentSubstitute( logTable.getActualSchemaName() ), environmentSubstitute( logTable
          .getActualTableName() ) );
    RowMetaInterface rowMetaInterface = logRecord.getRowMeta();
    Object[] rowData = logRecord.getData();

    if ( update ) {
      RowMetaInterface updateRowMeta = new RowMeta();
      Object[] updateRowData = new Object[ rowMetaInterface.size() ];
      ValueMetaInterface keyValueMeta = rowMetaInterface.getValueMeta( 0 );
      StringBuilder sqlBuff = new StringBuilder( 250 );
      sqlBuff.append( "UPDATE " ).append( schemaTable ).append( " SET " );

      for ( int i = 1; i < rowMetaInterface.size(); i++ ) { // Without ID_JOB or ID_BATCH
        ValueMetaInterface valueMeta = rowMetaInterface.getValueMeta( i );
        if ( i > 1 ) {
          sqlBuff.append( ", " );
        }
        sqlBuff.append( databaseMeta.quoteField( valueMeta.getName() ) ).append( "=? " );

        updateRowMeta.addValueMeta( valueMeta );
        updateRowData[ i - 1 ] = rowData[ i ];
      }
      sqlBuff.append( "WHERE " ).append( databaseMeta.quoteField( keyValueMeta.getName() ) ).append( "=? " );

      updateRowMeta.addValueMeta( keyValueMeta );
      updateRowData[ rowMetaInterface.size() - 1 ] = rowData[ 0 ];

      String sql = sqlBuff.toString();
      execStatement( sql, updateRowMeta, updateRowData );

    } else {

      insertRow( environmentSubstitute( logTable.getActualSchemaName() ), environmentSubstitute( logTable
        .getActualTableName() ), logRecord.getRowMeta(), logRecord.getData() );

    }
  } catch ( Exception e ) {
    DatabaseLogExceptionFactory.getExceptionStrategy( logTable, e )
      .registerException( log, e, PKG, "Database.Error.WriteLogTable",
        environmentSubstitute( logTable.getActualTableName() ) );
  }
}
 
Example 15
Source File: TestUtilities.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Check the 2 lists comparing the rows in order. If they are not the same fail the test.
 * 
 * @param rows1
 *          set 1 of rows to compare
 * @param rows2
 *          set 2 of rows to compare
 * @param fileNameColumn
 *          Number of the column containing the filename. This is only checked for being non-null (some systems maybe
 *          canonize names differently than we input).
 */
public static void checkRows( List<RowMetaAndData> rows1, List<RowMetaAndData> rows2, int fileNameColumn )
  throws TestFailedException {

  int idx = 1;
  if ( rows1.size() != rows2.size() ) {
    throw new TestFailedException( "Number of rows is not the same: " + rows1.size() + " and " + rows2.size() );
  }
  Iterator<RowMetaAndData> itrRows1 = rows1.iterator();
  Iterator<RowMetaAndData> itrRows2 = rows2.iterator();

  while ( itrRows1.hasNext() && itrRows2.hasNext() ) {
    RowMetaAndData rowMetaAndData1 = itrRows1.next();
    RowMetaAndData rowMetaAndData2 = itrRows2.next();

    RowMetaInterface rowMetaInterface1 = rowMetaAndData1.getRowMeta();

    Object[] rowObject1 = rowMetaAndData1.getData();
    Object[] rowObject2 = rowMetaAndData2.getData();

    if ( rowMetaAndData1.size() != rowMetaAndData2.size() ) {
      throw new TestFailedException( "row number " + idx + " is not equal" );
    }

    int[] fields = new int[rowMetaInterface1.size()];
    for ( int ydx = 0; ydx < rowMetaInterface1.size(); ydx++ ) {
      fields[ydx] = ydx;
    }

    try {
      if ( fileNameColumn >= 0 ) {
        rowObject1[fileNameColumn] = rowObject2[fileNameColumn];
      }
      if ( rowMetaAndData1.getRowMeta().compare( rowObject1, rowObject2, fields ) != 0 ) {
        throw new ComparisonFailure( "row nr " + idx + " is not equal", rowMetaInterface1.getString( rowObject1 ),
            rowMetaInterface1.getString( rowObject2 ) );
      }
    } catch ( KettleValueException e ) {
      throw new TestFailedException( "row nr " + idx + " is not equal" );
    }
    idx++;
  }
}