The Command Task in Informatica is used to run Unix or Shell commands during the Workflow. You can use this Task to Move, Copy, or Remove files after the data is loaded, etc. In this article, we will show you how to use the Command Task in Informatica with an example.
For this Informatica Command Task example, we are going to use the below show text file data (Employee table). From the below screenshot, you can see that the Source table has 15 records.
And the Destination table is Employee File. As you can see, it is an Empty table
Command Task in Informatica Example
Before we start configuring the Command Task in Informatica to Load Data from Flat File to SQL Server. First, let me connect with the Informatica repository service. To do so, we have to provide the Admin Console credentials. So, Please provide the appropriate Username and Password specified while installing the Server.
Step 1: Create Source Definition for Informatica Command Task
Once you connect successfully, Please navigate to Source Analyzer to define your Sources. As we said before, we are using the Employee table present in the local File System as our source definitions. So, Please navigate to the Source menu and select the Import from File.. option.
Once you choose the Import from File.. option, a new window called Open Flat File will open. Please select the Employee.txt file from your local file system and click the Open button. I suggest you refer Flat File Source article to understand the following steps.
From the below screenshot, you can observe that you can see our newly created Flat File source.
TIP: We already explained the steps involved in importing a Text file in our previous article. So, Please refer to Flat File Source to understand the same.
Step 2: Create Target Definition for Command Task in Informatica
Please navigate to Target Designer to define the Informatica Command Task Target. We use the existing SQL table (Employee File) as our target definition in this example. You can refer to Create a Target Table to understand the process of creating a target definition.
Step 3: Create Mapping for Command Task in Informatica
To create a new mapping, Please navigate to the Mappings menu in Menu Bar, and select the Create.. option.
Once you select the Create.. option, a new Mapping Name window will open. Let me provide m_SQLEmp as the mapping name and click the OK button.
Drag and drop the Employee source definitions from the Flat File Sources folder to the mapping designer. Once you drag the source, the Power Center designer automatically creates the Source Qualifier Transformation for you.
Next, Drag and drop the target definition (Employee File) from the Targets folder to the mapping designer. Next, connect the source qualifier with the target definition using the Autolink.. option.
Before we close the Mapping, Let us Save and Validate the mapping by going to the Mapping Menu bar and selecting the Validate option.
Step 4: Create a Workflow for Command Task in Informatica
After we finish creating the Command Task Mapping, we have to create the workflow for it. PowerCenter Workflow manager provides two approaches to creating a workflow.
In this Informatica Command Task example, we will create the Workflow manually. To do so, Please navigate to Workflows Menu and select the Create option.
It will open the Create Workflow window. Please provide the unique name (wf_CommandEx) and leave the default settings.
Once we have created the workflow, our next step is to create a session task for our mapping.
Step 4(a): Create a Session for Command Task in Informatica
There are two types of sessions:
For this Informatica Command Task example, we created a Non-reusable Session. To create Non-reusable Session, Please navigate to Tasks Menu and select the Create option.
Please provide a unique name for this Session. Here, we are naming it as s_CommandEx. Once you click on the Create button, a new window called Mappings opens. Here you have to select the Mapping that you want to associate with this Session, i.e., m_CommandEx.
Double click on the Session task will open the Edit Tasks window. We have to configure the Source and Target Connections within the mappings tab. First, let us configure the Target connection by clicking on the EmployeeFile.
Within the Connections, click on the Arrow button beside the Relational type, and Select the Target Database (Target)
Now, we have to configure the Source Connection. Please click on the SQ_Employee present in the Sources folder.
- Source File directory: It will use the default directory, but you can change the directory by giving the full path.
- Source filename: It will automatically detect. If not, please specify the file name along with the extension.
Next, navigate to Workflows Menu and select the Validate option to validate the Informatica Command Task Workflow.
Step 4(b): Create a Command Task in Informatica
To create an Informatica Command Task, First, go to the Task Developer Tab. Next, navigate to Tasks Menu and select the Create option.
Once you select the Create option, a new window called Create Task will open. First, select the Command Task, provide a unique name for this Task and click on the Create button. Here, we are naming it as cmd_CopyFile.
Click on the Create button will create a Command task for you
Double click on the Informatica Command Task will open the following window. Within the General Tab, we can rename the Command Task to a more meaningful name. Please do not forget to provide a valid description of the Task.
Within the Properties Tab, there is one property called :
- Fail Parent if this task fails: Please check mark this option. If this Command Task fails, then it will fail the workflow.
Within the Commands Tab, you have to provide the command that you are going to use in this workflow. Please click on the New button to add a new command
We renamed the field the Copy Command. Next, click on the Arrow to add the command.
It opens a space to write your command. For this example, we are using the Copy command to copy the Employee File from Source Files to target files.
Next, navigate to Workflows Menu and select the Validate option to validate the Workflow.
If you observe the Workflow, First, we are starting the Workflow. Next, it will run the Session task to load data from the text file to the SQL table. Once the data is transferred, the Informatica Command Task will copy Employee File from Source to the target
Now, Let me start the Workflow. To do so, navigate to the Workflows menu and select the Start Workflow option.
Now you can see the Employee file in the default target location.
Command Task in Informatica Example 2
Instead of creating a separate command task, you can also apply the functionality within the Session Task. Let me remove the Command task from the workflow to demonstrate the same.
Double click on the Session task, and go to the Components tab
- Pre-Session Command: This command executed before the Session
- Post-Session Command: This command is executed after the Session complete
- Post-Session Failure Command: It executes if the session task fails
For this example, we are going to create a command that has to execute after the Session is complete (Post-Session). Under the Type drop-down, we have three options. Let me select the Non-reusable command
Click on the New field
Next, click the Arrow field to write a command
For this example, we are using the Move command to move the Employee File from Source Files to target files.
Let me run the Informatica Command Task Workflow and see. As you can see, there is no Employee under the Source folder.
And the destination folder has this employee file.