User Case

What if you are asked to black-box a Talend job? Would it not be great if you could tell Talend Studio to open the output file when it is produced automaticaly? Or to prompt the user if he wants to open the rejects file and view any rejects that were produced after the job execution.

The guide below will show you how to do exactly that.

Opening a file

Unfortunately Talend Studio output components do not have an option to open the file automatically in an operating system’s default application. To achieve this follow the steps below.

  1. Create a new job in Talend Studio;
  2. Add 3 components:
    1. tRowGenerator;
    2. tFileOutputExcel;
    3. tJava;
  3. Connect them as shown in the image below;How to open a file with a system default application in Talend Studio
  4. Create a schema in the tRowGenerator component;
  5. Add some values to Columns using the RowGenerator Editor within the tRowGenerator component;
  6. Create a context value in the Context tab. Please see table below for more details;
Context Details
Name Type Value
OutputFile File Full path to the file (The file does not have to exist. The tFileOutputExcel will create it)
  1. Add the following code to the tJava component (In the Basic Settings):

try {
Desktop.getDesktop().open(new File(context.OutputFile));
} catch (IOException e) {e.printStackTrace();}

  1. Add the following imports to the Advanced Settings of the tJava component:

import java.awt.Desktop;
import java.io.File;

  1. Make sure that the tFileOutputExcel schema is synced;
  2. Add “context.OutputFile” as the File Name in the tFileOutputExcel component.
  3. Run the job;

As you can see after the job is done running, the output file will be opened automatically with the default application associated with the output file extension. Since we have used the tFileOutputExcel the default file would be either .xls or .xlsx, depending on how you configured the component.

Note: You may ask why a context value was used in this job. The reason is that you need to specify which file should be open in the tJava component. Unfortunately the tFileOutputExcel file does not allow to call a global file path for that component. So you have two options either put tCreateTemporaryFile and use the file path of that in both the tFileOutputExcel and tJava component code, or use the context value.

Taking it one step further

If you want to make it a bit more presentable and you don’t always want to open the output file upon job completion, but rather decide during each job execution. The steps below will show you how to achieve that.

  1. Create a new job in Talend;
  2. Add 4 components:
    1. tRowGenerator;
    2. tFileOutputDelimited;
    3. tMsgBox
    4. tJava;
  3. Connect them as shown in the image below;sdfsdfsdfsdfsd
  4. Create a schema in the tRowGenerator component;
  5. Add some values to Columns using the RowGenerator Editor within the tRowGenerator component;
  6. Add the following code to the tJava component (In the Basic Settings):

try {
Desktop.getDesktop().open(new File(((String)globalMap.get("tFileOutputDelimited_1_FILE_NAME"))));
} catch (IOException e) {e.printStackTrace();}

  1. Add the following imports to the Advanced Settings of the tJava component:

import java.awt.Desktop;
import java.io.File;

  1. In the tMsgBox change the Buttons layout to “Yes and No”. Optionally change the Icon to “Icon Question” and change both the Title and the Message to whatever you wish;
  2. In the If trigger between the tMsgBox and tJava add the following code:

((String)globalMap.get("tMsgBox_1_RESULT")).equals("0")

  1. Make sure that the tFileOutputExcel schema is synced;
  2. Specify a desired output path in the File Name section of the tFileOutputDelimited component;
  3. Run the Job. Once the output file has been generated the MsgBox will be activated and you should be able to see a message box similar to the one below:sdfsdfsdfs45623432
To find out how to make Msg Boxes look system native as shown in the image above – please refer to this guide;Add a Tooltip Text

 

  1. Click Yes and the output file will be opened in it’s system default application;
  2. In my case the file opens up in Sublime Text editor (you should really give Sublime a try), as both .csv and .txt file types are associated with it.

OpenFile

Send this to a friend