This article describes an example of creating and executing an ETL task with data transformation.
Executing the example requires that the repository contains two tables: T_Source and T_Destination. Tables must be identical in structure. The repository must also contain an ETL task with the ETLTASKS identifier. After executing the example three objects are created in the ETL task: repository provider, the Transform transformer and repository consumer. The required properties and links are set for all objects:
After objects are created and saved, the ETL task is executed. The similar code applied to different objects is placed into separate procedures or functions.
Add links to the Andy, Db, Drawing, Dt, Etl, Metabase system assemblies.
Sub UserProc;
Var
MB: IMetabase;
ETLTask: IEtlTask;
EtlProvider: IEtlPlainDataProvider;
MetabaseProvider: IDtMetabaseProvider;
EtlConsumer: IEtlPlainDataConsumer;
MetabaseConsumer: IDtMetabaseConsumer;
Mapper: IEtlPlainDataMapper;
Link, Link1: IEtlPlainLink;
Shapes: IWxShapes;
ProvFields, MapperInFields, MapperOutFields, ConsFields: IEtlPlainFields;
OutField: IEtlPlainField;
Begin
//Open ETL task
MB := MetabaseClass.Active;
ETLTask := MB.ItemById("ETLTASKS").Edit As IEtlTask;
//Create a provider
EtlProvider := ETLTask.Create(EtlObjectType.PlainDataMetabaseProvider) As IEtlPlainDataProvider;
EtlProvider := EtlProvider.Edit;
EtlProvider.Id := "Metabase_Provider";
EtlProvider.Name := "Import from table";
MetabaseProvider := EtlProvider.Provider As IDtMetabaseProvider;
MetabaseProvider.Dataset := MB.ItemById("T_Source").Bind As IDatasetModel;
EtlProvider.FillDefault;
//Create a visual object of provider
CreateWX(ETLTask, EtlProvider, -60, 0);
//Create a consumer
EtlConsumer := ETLTask.Create(EtlObjectType.PlainDataMetabaseConsumer) As IEtlPlainDataConsumer;
EtlConsumer := EtlConsumer.Edit;
EtlConsumer.Id := "Metabase_Consumer";
EtlConsumer.Name := "Export to table";
MetabaseConsumer := EtlConsumer.Consumer As IDtMetabaseConsumer;
MetabaseConsumer.Dataset := MB.ItemById("T_Destination").Bind As IDatasetModel;
//Create a visual object of consumer
CreateWX(ETLTask, EtlConsumer, 60, 0);
//Create the "Transform" transformer
Mapper := ETLTask.Create(EtlObjectType.PlainDataMapper) As IEtlPlainDataMapper;
Mapper := Mapper.Edit;
Mapper.Id := "Mapper_Transform";
Mapper.Name := "Transform";
//Create a visual object of transformer
CreateWX(ETLTask, Mapper, 0, 0);
//Fill lists of fields of all objects based on the list of provider fields
//Get list of field
ProvFields := EtlProvider.PlainOutput.Fields; //Provider output
MapperInFields := Mapper.PlainInput.Fields; //Transformer input
MapperOutFields := Mapper.PlainOutput.Fields; //Transformer output
ConsFields := EtlConsumer.PlainInput.Fields; //Consumer input
//Fill lists of fields
FillFields(MapperInFields, ProvFields);
FillFields(MapperOutFields, MapperInFields);
FillFields(ConsFields, MapperOutFields);
//Link of fields with setting transform formula
For Each OutField In MapperOutFields Do
Mapper.Mapper.Map(OutField).AsString := Mapper.PlainInput.Id + "." + MapperInFields.FindById(OutField.Id).Id + "*100"; // I1.<FieldId>*100
End For;
//Save all objects
EtlProvider.Save;
Mapper.Save;
EtlConsumer.Save;
//Link of transformer with all objects
//Create links
Link := CreateLink(ETLTask, EtlProvider.PlainOutput, Mapper.PlainInput);
Link1 := CreateLink(ETLTask, Mapper.PlainOutput, EtlConsumer.PlainInput);
//Create visual links
Shapes := ETLTask.Workspace.Shapes;
CreateWXLink(ETLTask, Shapes.FindById(EtlProvider.Id), Shapes.FindById(Mapper.Id), Link);
CreateWXLink(ETLTask, Shapes.FindById(Mapper.Id), Shapes.FindById(EtlConsumer.Id), Link1);
//Save task
(ETLTask As IMetabaseObject).Save;
//Execute task
ETLTask.Execute(Null);
End Sub UserProc;
//Create visual objects
Sub CreateWX(ETLTask: IEtlTask; ETLObject: IEtlObject; XPosition: Integer; YPosition: Integer);
Var
WxRect: IWxRectangle;
WxETLObj: IWxEtlObject;
Begin
WxRect := ETLTask.Workspace.CreateRectangle;
WxRect.Id := ETLObject.Id;
WxETLObj := New WxEtlObject.Create;
WxETLObj.EtlObject := ETLObject;
WxRect.Style.TextPosition := WxTextPosition.Bottom;
WxRect.Style.PictureMarginTop := -10;
WxRect.PinPosition := New GxPointF.Create(XPosition, YPosition);
WxRect.Extension := WxETLObj As IWxShapeExtension;
End Sub CreateWX;
//Fill lists of input/output fields
Sub FillFields(Fields, FieldsSource: IEtlPlainFields);
Begin
Fields := Fields.Edit;
Fields.Fill(FieldsSource);
Fields.Save;
End Sub FillFields;
//Create a link between objects
Function CreateLink(ETLTask: IEtlTask; Output: IEtlPlainOutput; Input: IEtlPlainInput): IEtlPlainLink;
Var
Link: IEtlPlainLink;
Begin
Link := ETLTask.CreatePlainLink;
Link.SourceObjectOutput := Output;
Link.DestinationObjectInput := Input;
Link.FillDefault;
Return Link;
End Function CreateLink;
//Create visual links
Sub CreateWXLink(ETLTask: IEtlTask; Shape1: IWxShape; Shape2: IWxShape; Link: IEtlPlainLink);
Var
WLink: IWxLink;
WxETLLink: IWxEtlObject;
Begin
//Create visual link objects
WLink := ETLTask.Workspace.AutoLinkShapes(Shape1, Shape2);
WLink.Style.LinePenBeginWxCap := WxLineCap.Flat;
WLink.Style.LinePenEndWxCap := WxLineCap.Arrow30DegreeFilled;
WxETLLink := New WxEtlObject.Create;
WxETLLink.EtlObject := Link;
WLink.Extension := WxETLLink As IWxShapeExtension;
End Sub CreateWXLink;
See also:
Examples | IEtlPlainDataMapper