Skip to main content
Pentaho Documentation

Building Transformations Dynamically

To enable your application to respond quickly to changing conditions, you can build transformations dynamically. The example class, org.pentaho.di.sdk.samples.embedding.GeneratingTransformations, shows you how. It generates a transformation definition and saves it to a .ktr file.

  1. Always make the first call to KettleEnvironment.init() whenever you are working with the PDI APIs.
  2. Create and configure a transformation definition object. A transformation definition is represented by a TransMeta object. Create this object using the default constructor. The transformation definition includes the name, the declared parameters, and the required database connections
  3. Populate the TransMeta object with steps. The data flow of a transformation is defined by steps that are connected by hops.
    1. Create the step by instantiating its class directly and configure it using its get and set methods. Transformation steps reside in sub-packages of org.pentaho.di.trans.steps. For example, to use the Get File Names step , create an instance of org.pentaho.di.trans.steps.getfilenames.GetFileNamesMeta and use its get and set methods to configure it.
    2. Obtain the step id string. Each PDI step has an id that can be retrieved from the PDI plugin registry. A simple way to retrieve the step id is to call PluginRegistry.getInstance().getPluginId(StepPluginType.class, theStepMetaObject)
    3. Create an instance of org.pentaho.di.trans.step.StepMeta, passing the step id string, the name, and the configured step object to the constructor. An instance of StepMeta encapsulates the step properties, as well as controls the placement of the step on the Spoon canvas and connections to hops. Once the StepMeta object has been created, call setDrawn(true) and setLocation(x,y) to make sure the step appears correctly on the Spoon canvas. Finally, add the step to the transformation, by calling addStep() on the transformation definition object.
    4. Once steps have been added to the transformation definition, they need to be connected by hops. To create a hop, create an instance of org.pentaho.di.trans.TransHopMeta, passing in the From and To steps as arguments to the constructor. Add the hop to the transformation definition by calling addTransHop().
After all steps have been added and connected by hops, the transformation definition object can be serialized to a .ktr file by calling getXML() and opening it in Spoon for inspection. The sample class org.pentaho.di.sdk.samples.embedding.GeneratingTransformations generates this transformation.