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.
- Always make the first call to
KettleEnvironment.init()
whenever you are working with the PDI APIs. - 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. - Populate the
TransMeta
object with steps. The data flow of a transformation is defined by steps that are connected by hops.- Create the step by instantiating its class directly and configure it using its
get
andset
methods. Transformation steps reside in sub-packages oforg.pentaho.di.trans.steps
. For example, to use the Get File Names step , create an instance oforg.pentaho.di.trans.steps.getfilenames.GetFileNamesMeta
and use itsget
andset
methods to configure it. - 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)
. - 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 ofStepMeta
encapsulates the step properties, as well as controls the placement of the step on the Spoon canvas and connections to hops. Once theStepMeta
object has been created, call setDrawn(true) andsetLocation(x,y)
to make sure the step appears correctly on the Spoon canvas. Finally, add the step to the transformation, by callingaddStep()
on the transformation definition object. - 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 callingaddTransHop()
.
- Create the step by instantiating its class directly and configure it using its
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.