Java Code Examples for org.pentaho.di.trans.step.StepMeta#getStepMetaInterface()

The following examples show how to use org.pentaho.di.trans.step.StepMeta#getStepMetaInterface() . 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: TransFileListener.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
protected TransMeta processLinkedJobs( TransMeta transMeta ) {
  for ( StepMeta stepMeta : transMeta.getSteps() ) {
    if ( stepMeta.getStepID().equalsIgnoreCase( "JobExecutor" ) ) {
      JobExecutorMeta jem = (JobExecutorMeta) stepMeta.getStepMetaInterface();
      ObjectLocationSpecificationMethod specMethod = jem.getSpecificationMethod();
      // If the reference is by filename, change it to Repository By Name. Otherwise it's fine so leave it alone
      if ( specMethod == ObjectLocationSpecificationMethod.FILENAME ) {
        jem.setSpecificationMethod( ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME );
        String filename = jem.getFileName();
        String jobname = filename.substring( filename.lastIndexOf( "/" ) + 1, filename.lastIndexOf( '.' ) );
        String directory = filename.substring( 0, filename.lastIndexOf( "/" ) );
        jem.setJobName( jobname );
        jem.setDirectoryPath( directory );
      }
    }
  }
  return transMeta;
}
 
Example 2
Source File: TransFileListenerTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessLinkedTransWithFilename() {
  TransExecutorMeta transExecutorMeta = spy( new TransExecutorMeta() );
  transExecutorMeta.setFileName( "/path/to/Transformation2.ktr" );
  transExecutorMeta.setSpecificationMethod( ObjectLocationSpecificationMethod.FILENAME );
  StepMeta transExecutorStep = mock( StepMeta.class );
  when( transExecutorStep.getStepID() ).thenReturn( "TransExecutor" );
  when( transExecutorStep.getStepMetaInterface() ).thenReturn( transExecutorMeta );

  TransMeta parent = mock( TransMeta.class );
  when( parent.getSteps() ).thenReturn( Arrays.asList( transExecutorStep ) );

  TransMeta result = transFileListener.processLinkedTrans( parent );

  boolean found = false;
  for ( StepMeta stepMeta : result.getSteps() ) {
    if ( stepMeta.getStepID().equalsIgnoreCase( "TransExecutor" ) ) {
      found = true;
      TransExecutorMeta resultExecMeta = (TransExecutorMeta) stepMeta.getStepMetaInterface();
      assertEquals( ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME, resultExecMeta.getSpecificationMethod() );
      assertEquals( resultExecMeta.getDirectoryPath(), "/path/to" );
      assertEquals( resultExecMeta.getTransName(), "Transformation2" );
    }
  }
  assertTrue( found );
}
 
Example 3
Source File: MetaInjectMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Override
public void onStepChange( TransMeta transMeta, StepMeta oldMeta, StepMeta newMeta ) {
  for ( int i = 0; i < transMeta.nrTransHops(); i++ ) {
    TransHopMeta hopMeta = transMeta.getTransHop( i );
    if ( hopMeta.getFromStep().equals( oldMeta ) ) {
      StepMeta toStepMeta = hopMeta.getToStep();
      if ( ( toStepMeta.getStepMetaInterface() instanceof MetaInjectMeta ) && ( toStepMeta.equals( this
        .getParentStepMeta() ) ) ) {
        MetaInjectMeta toMeta = (MetaInjectMeta) toStepMeta.getStepMetaInterface();
        Map<TargetStepAttribute, SourceStepField> sourceMapping = toMeta.getTargetSourceMapping();
        for ( Entry<TargetStepAttribute, SourceStepField> entry : sourceMapping.entrySet() ) {
          SourceStepField value = entry.getValue();
          if ( value.getStepname() != null && value.getStepname().equals( oldMeta.getName() ) ) {
            value.setStepname( newMeta.getName() );
          }
        }
      }
    }
  }
}
 
Example 4
Source File: PentahoReportingOutputIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void runTransformation(PentahoReportingOutputMeta.ProcessorType processorType) throws Exception {
  File tmp = File.createTempFile( "PentahoReportingOutputTest", "PentahoReportingOutputTest" );
  tmp.deleteOnExit();

  StepMeta outputStep = transMeta.findStep( "Data Grid" );
  DataGridMeta metaGrid = (DataGridMeta) outputStep.getStepMetaInterface();
  metaGrid.getDataLines().clear();
  metaGrid.getDataLines()
    .add( asList( "src/it/resources/org/pentaho/di/trans/steps/pentahoreporting/pdi-13434.prpt", tmp.getAbsolutePath() ) );

  StepMeta reportingStep = transMeta.findStep( "Pentaho Reporting Output" );
  PentahoReportingOutputMeta reportingMeta = (PentahoReportingOutputMeta) reportingStep.getStepMetaInterface();
  reportingMeta.setOutputProcessorType( processorType );

  Trans trans = new Trans( transMeta );
  trans.prepareExecution( null );
  trans.startThreads();
  trans.waitUntilFinished();

  Assert.assertEquals( 0, trans.getErrors() );
  Assert.assertTrue( tmp.length() > 0 );
  tmp.delete();
}
 
Example 5
Source File: TransMetaConverter.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private static void resolveStepMetaResources( TransMeta transMeta ) {
  for ( StepMeta stepMeta : transMeta.getSteps() ) {
    StepMetaInterface smi = stepMeta.getStepMetaInterface();
    if ( smi instanceof ResolvableResource ) {
      ResolvableResource resolvableMeta = (ResolvableResource) smi;
      resolvableMeta.resolve();
    }
  }
}
 
Example 6
Source File: BeamPublisherStepHandler.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta stepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface rowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  BeamPublishMeta publishMeta = (BeamPublishMeta) stepMeta.getStepMetaInterface();

  // some validation
  //
  if ( StringUtils.isEmpty( publishMeta.getTopic() ) ) {
    throw new KettleException( "Please specify a topic to publish to in Beam Pub/Sub Publish step '" + stepMeta.getName() + "'" );
  }

  BeamPublishTransform beamOutputTransform = new BeamPublishTransform(
    stepMeta.getName(),
    transMeta.environmentSubstitute( publishMeta.getTopic() ),
    publishMeta.getMessageType(),
    publishMeta.getMessageField(),
    JsonRowMeta.toJson( rowMeta ),
    stepPluginClasses,
    xpPluginClasses
  );

  // Which step do we apply this transform to?
  // Ignore info hops until we figure that out.
  //
  if ( previousSteps.size() > 1 ) {
    throw new KettleException( "Combining data from multiple steps is not supported yet!" );
  }
  StepMeta previousStep = previousSteps.get( 0 );

  // No need to store this, it's PDone.
  //
  input.apply( beamOutputTransform );
  log.logBasic( "Handled step (PUBLISH) : " + stepMeta.getName() + ", gets data from " + previousStep.getName() );
}
 
Example 7
Source File: BeamKafkaOutputStepHandler.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta beamOutputStepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface rowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  BeamProduceMeta beamProduceMeta = (BeamProduceMeta) beamOutputStepMeta.getStepMetaInterface();

  BeamKafkaOutputTransform beamOutputTransform = new BeamKafkaOutputTransform(
    beamOutputStepMeta.getName(),
    transMeta.environmentSubstitute( beamProduceMeta.getBootstrapServers() ),
    transMeta.environmentSubstitute( beamProduceMeta.getTopic() ),
    transMeta.environmentSubstitute( beamProduceMeta.getKeyField() ),
    transMeta.environmentSubstitute( beamProduceMeta.getMessageField() ),
    JsonRowMeta.toJson( rowMeta ),
    stepPluginClasses,
    xpPluginClasses
  );

  // Which step do we apply this transform to?
  // Ignore info hops until we figure that out.
  //
  if ( previousSteps.size() > 1 ) {
    throw new KettleException( "Combining data from multiple steps is not supported yet!" );
  }
  StepMeta previousStep = previousSteps.get( 0 );

  // No need to store this, it's PDone.
  //
  input.apply( beamOutputTransform );
  log.logBasic( "Handled step (KAFKA OUTPUT) : " + beamOutputStepMeta.getName() + ", gets data from " + previousStep.getName() );
}
 
Example 8
Source File: BeamInputStepHandler.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta stepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface rowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  // Input handling
  //
  BeamInputMeta beamInputMeta = (BeamInputMeta) stepMeta.getStepMetaInterface();
  FileDefinition inputFileDefinition = beamInputMeta.loadFileDefinition( metaStore );
  RowMetaInterface fileRowMeta = inputFileDefinition.getRowMeta();

  // Apply the PBegin to KettleRow transform:
  //
  if ( inputFileDefinition == null ) {
    throw new KettleException( "We couldn't find or load the Beam Input step file definition" );
  }
  String fileInputLocation = transMeta.environmentSubstitute( beamInputMeta.getInputLocation() );

  BeamInputTransform beamInputTransform = new BeamInputTransform(
    stepMeta.getName(),
    stepMeta.getName(),
    fileInputLocation,
    transMeta.environmentSubstitute( inputFileDefinition.getSeparator() ),
    JsonRowMeta.toJson( fileRowMeta ),
    stepPluginClasses,
    xpPluginClasses
  );
  PCollection<KettleRow> afterInput = pipeline.apply( beamInputTransform );
  stepCollectionMap.put( stepMeta.getName(), afterInput );
  log.logBasic( "Handled step (INPUT) : " + stepMeta.getName() );

}
 
Example 9
Source File: TransFileListenerTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessLinkedTransWithNoFilename() {
  TransExecutorMeta transExecutorMeta = spy( new TransExecutorMeta() );
  transExecutorMeta.setFileName( null );
  transExecutorMeta.setDirectoryPath( "/path/to" );
  transExecutorMeta.setTransName( "Transformation2" );
  transExecutorMeta.setSpecificationMethod( ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME );
  StepMeta transExecutorStep = mock( StepMeta.class );
  when( transExecutorStep.getStepID() ).thenReturn( "TransExecutor" );
  when( transExecutorStep.getStepMetaInterface() ).thenReturn( transExecutorMeta );

  TransMeta parent = mock( TransMeta.class );
  when( parent.getSteps() ).thenReturn( Arrays.asList( transExecutorStep ) );

  TransMeta result = transFileListener.processLinkedTrans( parent );

  boolean found = false;
  for ( StepMeta stepMeta : result.getSteps() ) {
    if ( stepMeta.getStepID().equalsIgnoreCase( "TransExecutor" ) ) {
      found = true;
      TransExecutorMeta resultExecMeta = (TransExecutorMeta) stepMeta.getStepMetaInterface();
      assertEquals( ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME, resultExecMeta.getSpecificationMethod() );
      assertEquals( resultExecMeta.getDirectoryPath(), "/path/to" );
      assertEquals( resultExecMeta.getTransName(), "Transformation2" );
    }
  }
  assertTrue( found );
}
 
Example 10
Source File: BeamSubscriberStepHandler.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta stepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface rowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  // A Beam subscriber step
  //
  BeamSubscribeMeta inputMeta = (BeamSubscribeMeta) stepMeta.getStepMetaInterface();

  RowMetaInterface outputRowMeta = transMeta.getStepFields( stepMeta );
  String rowMetaJson = JsonRowMeta.toJson( outputRowMeta );

  // Verify some things:
  //
  if ( StringUtils.isEmpty( inputMeta.getTopic() ) ) {
    throw new KettleException( "Please specify a topic to read from in Beam Pub/Sub Subscribe step '" + stepMeta.getName() + "'" );
  }

  BeamSubscribeTransform subscribeTransform = new BeamSubscribeTransform(
    stepMeta.getName(),
    stepMeta.getName(),
    transMeta.environmentSubstitute( inputMeta.getSubscription() ),
    transMeta.environmentSubstitute( inputMeta.getTopic() ),
    inputMeta.getMessageType(),
    rowMetaJson,
    stepPluginClasses,
    xpPluginClasses
  );

  PCollection<KettleRow> afterInput = pipeline.apply( subscribeTransform );
  stepCollectionMap.put( stepMeta.getName(), afterInput );

  log.logBasic( "Handled step (SUBSCRIBE) : " + stepMeta.getName() );
}
 
Example 11
Source File: ModPartitionerDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public ModPartitionerDialog( Shell parent, StepMeta stepMeta, StepPartitioningMeta partitioningMeta,
                             TransMeta transMeta ) {
  super( parent, (BaseStepMeta) stepMeta.getStepMetaInterface(), transMeta, partitioningMeta
    .getPartitioner().getDescription() );
  this.stepMeta = stepMeta;
  this.partitioningMeta = partitioningMeta;
  partitioner = (ModPartitioner) partitioningMeta.getPartitioner();
  fieldName = partitioner.getFieldName();
}
 
Example 12
Source File: OpenMappingExtension.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override public void callExtensionPoint( LogChannelInterface log, Object object ) throws KettleException {
  StepMeta stepMeta = (StepMeta) ( (Object[]) object )[ 0 ];
  TransMeta transMeta = (TransMeta) ( (Object[]) object )[ 1 ];

  if ( stepMeta.getStepMetaInterface() instanceof MetaInjectMeta ) {
    // Make sure we don't accidently overwrite this transformation so we'll remove the filename and objectId
    // Modify the name so the users sees it's a result
    transMeta.setFilename( null );
    transMeta.setObjectId( null );
    String appendName = " (" + BaseMessages.getString( PKG, "TransGraph.AfterInjection" ) + ")";
    if ( !transMeta.getName().endsWith( appendName ) ) {
      transMeta.setName( transMeta.getName() + appendName );
    }
  }
}
 
Example 13
Source File: GPLoadIT.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns a GPLoadMeta.
 * 
 * @param gpLoadStepname
 * @param action
 * @param targetTableName
 * @param errorTableName
 * @param dataFilename
 * @param delimiter
 * @param logFilename
 * @param[] tableColumns
 * @param matchColumns
 * @param updateColumns
 */
private GPLoadMeta createGPLoadMeta( String gpLoadStepname, String action, String schemaTableName,
  String targetTableName, String errorTableName, String dataFilename, String delimiter, String pathToGPLoad,
  String pathToControlfile, String logFilename, String[] tableColumn, boolean[] matchColumn,
  boolean[] updateColumn, String localhostPort, String[] localHosts, String updateCondition, String encoding ) throws Exception {

  // create the trans meta
  TransMeta transMeta = createTransformationMeta( gpLoadStepname );

  // get a reference to the GPLoad step meta and then it's
  StepMeta gpLoadStepMeta = transMeta.getStep( 0 );
  GPLoadMeta gpLoadMeta = (GPLoadMeta) gpLoadStepMeta.getStepMetaInterface();

  // setDefault is called from Spoon.newStep if we were creating a new step using the interface.
  gpLoadMeta.setDefault();

  // set properties
  gpLoadMeta.setSchemaName( schemaTableName );
  gpLoadMeta.setTableName( targetTableName );
  gpLoadMeta.setErrorTableName( GPLoadIT.GPLOAD_ERROR_TABLE );
  gpLoadMeta.setLoadAction( action );
  gpLoadMeta.setDataFile( dataFilename );
  gpLoadMeta.setDelimiter( delimiter );
  gpLoadMeta.setFieldTable( tableColumn );
  gpLoadMeta.setMatchColumns( matchColumn );
  gpLoadMeta.setUpdateColumn( updateColumn );
  gpLoadMeta.setControlFile( pathToControlFile );
  gpLoadMeta.setLogFile( logFilename );
  gpLoadMeta.setGploadPath( pathToGPLoad );
  gpLoadMeta.setDatabaseMeta( transMeta.getDatabase( 0 ) );
  gpLoadMeta.setLocalhostPort( localhostPort );
  gpLoadMeta.setLocalHosts( localHosts );
  gpLoadMeta.setUpdateCondition( updateCondition );
  gpLoadMeta.setEncoding( encoding );

  return gpLoadMeta;
}
 
Example 14
Source File: BeamOutputStepHandler.java    From kettle-beam with Apache License 2.0 4 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta beamOutputStepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface rowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  BeamOutputMeta beamOutputMeta = (BeamOutputMeta) beamOutputStepMeta.getStepMetaInterface();
  FileDefinition outputFileDefinition;
  if ( StringUtils.isEmpty( beamOutputMeta.getFileDescriptionName() ) ) {
    // Create a default file definition using standard output and sane defaults...
    //
    outputFileDefinition = getDefaultFileDefition( beamOutputStepMeta );
  } else {
    outputFileDefinition = beamOutputMeta.loadFileDefinition( metaStore );
  }

  // Empty file definition? Add all fields in the output
  //
  addAllFieldsToEmptyFileDefinition( rowMeta, outputFileDefinition );

  // Apply the output transform from KettleRow to PDone
  //
  if ( outputFileDefinition == null ) {
    throw new KettleException( "We couldn't find or load the Beam Output step file definition" );
  }
  if ( rowMeta == null || rowMeta.isEmpty() ) {
    throw new KettleException( "No output fields found in the file definition or from previous steps" );
  }

  BeamOutputTransform beamOutputTransform = new BeamOutputTransform(
    beamOutputStepMeta.getName(),
    transMeta.environmentSubstitute( beamOutputMeta.getOutputLocation() ),
    transMeta.environmentSubstitute( beamOutputMeta.getFilePrefix() ),
    transMeta.environmentSubstitute( beamOutputMeta.getFileSuffix() ),
    transMeta.environmentSubstitute( outputFileDefinition.getSeparator() ),
    transMeta.environmentSubstitute( outputFileDefinition.getEnclosure() ),
    beamOutputMeta.isWindowed(),
    JsonRowMeta.toJson( rowMeta ),
    stepPluginClasses,
    xpPluginClasses
  );

  // Which step do we apply this transform to?
  // Ignore info hops until we figure that out.
  //
  if ( previousSteps.size() > 1 ) {
    throw new KettleException( "Combining data from multiple steps is not supported yet!" );
  }
  StepMeta previousStep = previousSteps.get( 0 );

  // No need to store this, it's PDone.
  //
  input.apply( beamOutputTransform );
  log.logBasic( "Handled step (OUTPUT) : " + beamOutputStepMeta.getName() + ", gets data from " + previousStep.getName() );
}
 
Example 15
Source File: TransGraph.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Open the transformation mentioned in the mapping...
 */
public void openMapping( StepMeta stepMeta, int index ) {
  try {
    Object referencedMeta = null;
    Trans subTrans = getActiveSubtransformation( this, stepMeta );
    if ( subTrans != null
      && ( stepMeta.getStepMetaInterface().getActiveReferencedObjectDescription() == null || index < 0 ) ) {
      TransMeta subTransMeta = subTrans.getTransMeta();
      referencedMeta = subTransMeta;
      Object[] objectArray = new Object[ 4 ];
      objectArray[ 0 ] = stepMeta;
      objectArray[ 1 ] = subTransMeta;
      ExtensionPointHandler.callExtensionPoint( log, KettleExtensionPoint.OpenMapping.id, objectArray );
    } else {
      StepMetaInterface meta = stepMeta.getStepMetaInterface();
      if ( !Utils.isEmpty( meta.getReferencedObjectDescriptions() ) ) {
        referencedMeta = meta.loadReferencedObject( index, spoon.rep, spoon.metaStore, transMeta );
      }
    }
    if ( referencedMeta == null ) {
      return;
    }

    if ( referencedMeta instanceof TransMeta ) {
      TransMeta mappingMeta = (TransMeta) referencedMeta;
      mappingMeta.clearChanged();
      spoon.addTransGraph( mappingMeta );
      TransGraph subTransGraph = spoon.getActiveTransGraph();
      attachActiveTrans( subTransGraph, this.currentStep );
    }

    if ( referencedMeta instanceof JobMeta ) {
      JobMeta jobMeta = (JobMeta) referencedMeta;
      jobMeta.clearChanged();
      spoon.addJobGraph( jobMeta );
      JobGraph jobGraph = spoon.getActiveJobGraph();
      attachActiveJob( jobGraph, this.currentStep );
    }

  } catch ( Exception e ) {
    new ErrorDialog( shell, BaseMessages.getString( PKG, "TransGraph.Exception.UnableToLoadMapping.Title" ),
      BaseMessages.getString( PKG, "TransGraph.Exception.UnableToLoadMapping.Message" ), e );
  }
}
 
Example 16
Source File: TransExecutorIT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private static TransExecutorMeta getExecutorMeta( StepMeta stepMeta ) {
  return (TransExecutorMeta) stepMeta.getStepMetaInterface();
}
 
Example 17
Source File: MappingIT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that info steps are correctly identified via StepMetaInterface.getStepIOMeta()
 */
public void testInfoStreams_single() throws Exception {
  KettleEnvironment.init();
  PluginRegistry registry = PluginRegistry.getInstance();

  //
  // Create a new transformation with a row generator that feeds a Mapping (Sub-Transformation) Step
  //
  TransMeta transMeta = new TransMeta();
  transMeta.setName( "Mapping Info Test" );
  StepMeta rowGenerator = buildRowGeneratorStep( registry, "Generate Rows" );
  transMeta.addStep( rowGenerator );

  String mappingName = "mapping";
  MappingMeta mappingMeta = new MappingMeta();
  mappingMeta.setSpecificationMethod( ObjectLocationSpecificationMethod.FILENAME );
  mappingMeta.setFileName( "test/org/pentaho/di/trans/steps/mapping/subtrans.ktr" );
  String mappingInputStepName = "input";
  mappingMeta.setInputMappings( Collections.singletonList( createMappingDef(
    rowGenerator.getName(), mappingInputStepName, "string", "a" ) ) );
  String mappingPid = registry.getPluginId( StepPluginType.class, mappingMeta );
  StepMeta mapping = new StepMeta( mappingPid, mappingName, mappingMeta );
  transMeta.addStep( mapping );

  TransHopMeta hopGeneratorToMapping = new TransHopMeta( rowGenerator, mapping );
  transMeta.addTransHop( hopGeneratorToMapping );

  Trans trans = new Trans( transMeta );
  trans.prepareExecution( null );

  // Mimic how a transformation is loaded and initialized from TransMeta.loadXML() or
  // KettleDatabaseRepositoryTransDelegate.loadTransformation()
  // so the StepMeta references are wired up in the MappingMeta properly
  // (Copied from TransMeta.loadXML())
  for ( int i = 0; i < transMeta.nrSteps(); i++ ) {
    StepMeta stepMeta = transMeta.getStep( i );
    StepMetaInterface sii = stepMeta.getStepMetaInterface();
    if ( sii != null ) {
      sii.searchInfoAndTargetSteps( transMeta.getSteps() );
    }
  }

  // Verify the transformation was configured properly
  assertEquals( "Transformation not initialized properly", 2, transMeta.nrSteps() );

  StepMeta meta = transMeta.getStep( 1 );
  assertTrue( "Transformation not initialized properly", meta.getStepMetaInterface() instanceof MappingMeta );

  MappingMeta loadedMappingMeta = (MappingMeta) meta.getStepMetaInterface();
  assertEquals( "Expected a single input mapping definition", 1, loadedMappingMeta.getInputMappings().size() );

  StepIOMetaInterface ioMeta = loadedMappingMeta.getStepIOMeta();
  assertEquals( "Expected a single Info Stream", 1, ioMeta.getInfoStreams().size() );
  assertEquals( "Expected a single Info Step", 1, loadedMappingMeta.getInfoSteps().length );

  // Verify the transformation can be executed
  StepInterface si = trans.getStepInterface( mappingName, 0 );
  RowStepCollector rc = new RowStepCollector();
  si.addRowListener( rc );

  trans.startThreads();
  trans.waitUntilFinished();

  assertEquals( 1, rc.getRowsRead().size() );
  assertEquals( 1, rc.getRowsWritten().size() );
}
 
Example 18
Source File: BeamWindowStepHandler.java    From kettle-beam with Apache License 2.0 4 votes vote down vote up
@Override public void handleStep( LogChannelInterface log, StepMeta stepMeta, Map<String, PCollection<KettleRow>> stepCollectionMap,
                                  Pipeline pipeline, RowMetaInterface inputRowMeta, List<StepMeta> previousSteps,
                                  PCollection<KettleRow> input ) throws KettleException {

  BeamWindowMeta beamWindowMeta = (BeamWindowMeta) stepMeta.getStepMetaInterface();

  if ( StringUtils.isEmpty( beamWindowMeta.getWindowType() ) ) {
    throw new KettleException( "Please specify a window type in Beam Window step '" + stepMeta.getName() + "'" );
  }

  String duration = transMeta.environmentSubstitute( beamWindowMeta.getDuration() );
  long durationSeconds = Const.toLong( duration, -1L );

  PCollection<KettleRow> stepPCollection;

  if ( BeamDefaults.WINDOW_TYPE_FIXED.equals( beamWindowMeta.getWindowType() ) ) {

    if ( durationSeconds <= 0 ) {
      throw new KettleException( "Please specify a valid positive window size (duration) for Beam window step '" + stepMeta.getName() + "'" );
    }

    FixedWindows fixedWindows = FixedWindows
      .of( Duration.standardSeconds( durationSeconds ) );
    stepPCollection = input.apply( Window.into( fixedWindows ) );

  } else if ( BeamDefaults.WINDOW_TYPE_SLIDING.equals( beamWindowMeta.getWindowType() ) ) {

    if ( durationSeconds <= 0 ) {
      throw new KettleException( "Please specify a valid positive window size (duration) for Beam window step '" + stepMeta.getName() + "'" );
    }

    String every = transMeta.environmentSubstitute( beamWindowMeta.getEvery() );
    long everySeconds = Const.toLong( every, -1L );

    SlidingWindows slidingWindows = SlidingWindows
      .of( Duration.standardSeconds( durationSeconds ) )
      .every( Duration.standardSeconds( everySeconds ) );
    stepPCollection = input.apply( Window.into( slidingWindows ) );

  } else if ( BeamDefaults.WINDOW_TYPE_SESSION.equals( beamWindowMeta.getWindowType() ) ) {

    if ( durationSeconds < 600 ) {
      throw new KettleException(
        "Please specify a window size (duration) of at least 600 (10 minutes) for Beam window step '" + stepMeta.getName() + "'.  This is the minimum gap between session windows." );
    }

    Sessions sessionWindows = Sessions
      .withGapDuration( Duration.standardSeconds( durationSeconds ) );
    stepPCollection = input.apply( Window.into( sessionWindows ) );

  } else if ( BeamDefaults.WINDOW_TYPE_GLOBAL.equals( beamWindowMeta.getWindowType() ) ) {

    stepPCollection = input.apply( Window.into( new GlobalWindows() ) );

  } else {
    throw new KettleException( "Beam Window type '" + beamWindowMeta.getWindowType() + " is not supported in step '" + stepMeta.getName() + "'" );
  }

  // Now get window information about the window if we asked about it...
  //
  if ( StringUtils.isNotEmpty( beamWindowMeta.getStartWindowField() ) ||
    StringUtils.isNotEmpty( beamWindowMeta.getEndWindowField() ) ||
    StringUtils.isNotEmpty( beamWindowMeta.getMaxWindowField() ) ) {

    WindowInfoFn windowInfoFn = new WindowInfoFn(
      stepMeta.getName(),
      transMeta.environmentSubstitute( beamWindowMeta.getMaxWindowField() ),
      transMeta.environmentSubstitute( beamWindowMeta.getStartWindowField() ),
      transMeta.environmentSubstitute( beamWindowMeta.getMaxWindowField() ),
      JsonRowMeta.toJson( inputRowMeta ),
      stepPluginClasses,
      xpPluginClasses
    );

    stepPCollection = stepPCollection.apply( ParDo.of( windowInfoFn ) );
  }

  // Save this in the map
  //
  stepCollectionMap.put( stepMeta.getName(), stepPCollection );
  log.logBasic( "Handled step (WINDOW) : " + stepMeta.getName() + ", gets data from " + previousSteps.size() + " previous step(s)" );
}
 
Example 19
Source File: TransMetaTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testTransListeners() {
  TransMeta TransMeta = new TransMeta();

  StepMeta oldFormStep = new StepMeta();
  oldFormStep.setName( "Generate_1" );

  StepMeta newFormStep = new StepMeta();
  newFormStep.setName( "Generate_2" );

  StepMeta toStep = new StepMeta();
  toStep.setStepMetaInterface( new MetaInjectMeta() );
  toStep.setName( "ETL Inject Metadata" );

  StepMeta deletedStep = new StepMeta();
  deletedStep.setStepMetaInterface( new MetaInjectMeta() );
  deletedStep.setName( "ETL Inject Metadata for delete" );

  // Verify add & remove listeners

  TransMeta.addStep( oldFormStep );
  TransMeta.addStep( toStep );
  TransMeta.addStep( deletedStep );

  assertEquals( TransMeta.nrStepChangeListeners(), 2 );
  TransMeta.removeStepChangeListener( (StepMetaChangeListenerInterface) deletedStep.getStepMetaInterface() );
  assertEquals( TransMeta.nrStepChangeListeners(), 1 );
  TransMeta.removeStep( 2 );

  TransHopMeta hi = new TransHopMeta( oldFormStep, toStep );
  TransMeta.addTransHop( hi );

  // Verify MetaInjectMeta.onStepChange()

  // add new TargetStepAttribute
  MetaInjectMeta toMeta = (MetaInjectMeta) toStep.getStepMetaInterface();

  Map<TargetStepAttribute, SourceStepField> sourceMapping = new HashMap<TargetStepAttribute, SourceStepField>();
  TargetStepAttribute keyTest = new TargetStepAttribute( "File", "key", true );
  SourceStepField valueTest = new SourceStepField( oldFormStep.getName(), oldFormStep.getName() );
  sourceMapping.put( keyTest, valueTest );

  toMeta.setTargetSourceMapping( sourceMapping );

  // Run all listeners
  TransMeta.notifyAllListeners( oldFormStep, newFormStep );

  // Verify changes, which listeners makes
  sourceMapping = toMeta.getTargetSourceMapping();
  for ( Entry<TargetStepAttribute, SourceStepField> entry : sourceMapping.entrySet() ) {
    SourceStepField value = entry.getValue();
    if ( !value.getStepname().equals( newFormStep.getName() ) ) {
      fail();
    }
  }

  // verify another functions
  TransMeta.addStep( deletedStep );
  assertEquals( TransMeta.nrSteps(), 3 );
  assertEquals( TransMeta.nrStepChangeListeners(), 2 );

  TransMeta.removeStep( 0 );
  assertEquals( TransMeta.nrSteps(), 2 );

}
 
Example 20
Source File: SimpleMappingMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public void getFields( RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep,
  VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {
  // First load some interesting data...

  // Then see which fields get added to the row.
  //
  TransMeta mappingTransMeta = null;
  try {
    mappingTransMeta =
      loadMappingMeta( this, repository, metaStore, space, mappingParameters.isInheritingAllVariables() );
  } catch ( KettleException e ) {
    throw new KettleStepException( BaseMessages.getString(
      PKG, "SimpleMappingMeta.Exception.UnableToLoadMappingTransformation" ), e );
  }

  // The field structure may depend on the input parameters as well (think of parameter replacements in MDX queries
  // for instance)
  if ( mappingParameters != null && mappingTransMeta != null ) {

    // Just set the variables in the transformation statically.
    // This just means: set a number of variables or parameter values:
    //
    StepWithMappingMeta.activateParams( mappingTransMeta, mappingTransMeta, space, mappingTransMeta.listParameters(),
      mappingParameters.getVariable(), mappingParameters.getInputField(), mappingParameters.isInheritingAllVariables() );
  }

  // Keep track of all the fields that need renaming...
  //
  List<MappingValueRename> inputRenameList = new ArrayList<MappingValueRename>();

  //
  // Before we ask the mapping outputs anything, we should teach the mapping
  // input steps in the sub-transformation about the data coming in...
  //

  RowMetaInterface inputRowMeta;

  // The row metadata, what we pass to the mapping input step
  // definition.getOutputStep(), is "row"
  // However, we do need to re-map some fields...
  //
  inputRowMeta = row.clone();
  if ( !inputRowMeta.isEmpty() ) {
    for ( MappingValueRename valueRename : inputMapping.getValueRenames() ) {
      ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta( valueRename.getSourceValueName() );
      if ( valueMeta == null ) {
        throw new KettleStepException( BaseMessages.getString(
          PKG, "SimpleMappingMeta.Exception.UnableToFindField", valueRename.getSourceValueName() ) );
      }
      valueMeta.setName( valueRename.getTargetValueName() );
    }
  }

  // What is this mapping input step?
  //
  StepMeta mappingInputStep = mappingTransMeta.findMappingInputStep( null );

  // We're certain it's a MappingInput step...
  //
  MappingInputMeta mappingInputMeta = (MappingInputMeta) mappingInputStep.getStepMetaInterface();

  // Inform the mapping input step about what it's going to receive...
  //
  mappingInputMeta.setInputRowMeta( inputRowMeta );

  // What values are we changing names for: already done!
  //
  mappingInputMeta.setValueRenames( null );

  // Keep a list of the input rename values that need to be changed back at
  // the output
  //
  if ( inputMapping.isRenamingOnOutput() ) {
    SimpleMapping.addInputRenames( inputRenameList, inputMapping.getValueRenames() );
  }

  StepMeta mappingOutputStep = mappingTransMeta.findMappingOutputStep( null );

  // We know it's a mapping output step...
  MappingOutputMeta mappingOutputMeta = (MappingOutputMeta) mappingOutputStep.getStepMetaInterface();

  // Change a few columns.
  mappingOutputMeta.setOutputValueRenames( outputMapping.getValueRenames() );

  // Perhaps we need to change a few input columns back to the original?
  //
  mappingOutputMeta.setInputValueRenames( inputRenameList );

  // Now we know wat's going to come out of there...
  // This is going to be the full row, including all the remapping, etc.
  //
  RowMetaInterface mappingOutputRowMeta = mappingTransMeta.getStepFields( mappingOutputStep );

  row.clear();
  row.addRowMeta( mappingOutputRowMeta );
}