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.
- Create a new job in Talend Studio;
- Add 3 components:
- tRowGenerator;
- tFileOutputExcel;
- tJava;
- Connect them as shown in the image below;
- Create a schema in the tRowGenerator component;
- Add some values to Columns using the RowGenerator Editor within the tRowGenerator component;
- 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) |
- 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();}
- Add the following imports to the Advanced Settings of the tJava component:
import java.awt.Desktop;
import java.io.File;
- Make sure that the tFileOutputExcel schema is synced;
- Add “context.OutputFile” as the File Name in the tFileOutputExcel component.
- 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.
- Create a new job in Talend;
- Add 4 components:
- tRowGenerator;
- tFileOutputDelimited;
- tMsgBox
- tJava;
- Connect them as shown in the image below;
- Create a schema in the tRowGenerator component;
- Add some values to Columns using the RowGenerator Editor within the tRowGenerator component;
- 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();}
- Add the following imports to the Advanced Settings of the tJava component:
import java.awt.Desktop;
import java.io.File;
- 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;
- In the If trigger between the tMsgBox and tJava add the following code:
((String)globalMap.get("tMsgBox_1_RESULT")).equals("0")
- Make sure that the tFileOutputExcel schema is synced;
- Specify a desired output path in the File Name section of the tFileOutputDelimited component;
- 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:
- Click Yes and the output file will be opened in it’s system default application;
- 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.
0 Comments