Skip to main content Skip to complementary content

Creating a temporary file and writing data into it

This scenario describes a Job that creates a temporary file in the default system temporary directory, writes data into the file, and finally displays the data in the file on the console.

Adding and linking the components

Procedure

  1. Create a new Job and add the following components by typing their names in the design workspace or dropping them from the Palette: a tCreateTemporaryFile component, a tJava component, a tRowGenerator component, a tFileOutputDelimited component, a tFileInputDelimited component, and a tLogRow component.
  2. Connect tRowGenerator to tFileOutputDelimited using a Row > Main connection.
  3. Do the same to connect tFileInputDelimited to tLogRow.
  4. Connect tCreateTemporaryFile to tJava using a Trigger > OnSubjobOk connection.
  5. Do the same to connect tJava to tRowGenerator and connect tRowGenerator to tFileInputDelimited.

Configuring the components

Creating the temporary file

Procedure

  1. Double-click tCreateTemporaryFile to open its Basic settings view.
  2. Select the Remove file when execution is over check box to delete the created temporary file after the Job execution.
  3. Select the Use default temporary system directory check box to create the file in the default system temporary directory.
  4. In the Template field, enter the temporary file name which should contain the characters XXXX. In this example, it is talend_XXXX.
  5. In the Suffix field, enter the filename extension of the temporary file. In this example, it is dat.
  6. Double-click tJava to open its Basic settings view.
  7. In the Code field, enter the following code to display the default system temporary directory and the path to the temporary file that will be created on the console:
    System.out.println("The default system temporary directory is:\r" + (String)System.getProperty("java.io.tmpdir"));
                            System.out.println("The path to the temporary file is:\r" + (String)globalMap.get("tCreateTemporaryFile_1_FILEPATH"));

Writing the data into the file

Procedure

  1. Double-click tRowGenerator to open its RowGenerator Editor.
  2. Click the [+] button to add two columns: id of Integer type and name of String type. Then in the Functions column, select the predefined function Numeric.sequence(String,int,int) for id and TalendDataGenerator.getFirstName() for name.
  3. In the Number of Rows for RowGenerator field, enter 5 to generate five rows.
  4. Click OK to validate the changes and accept the propagation prompted by the pop-up dialog box.
  5. Double-click tFileOutputDelimited to open its Basic settings view.
  6. In the File Name field, press Ctrl+Space and from the global variable list displayed select ((String)globalMap.get("tCreateTemporaryFile_1_FILEPATH")).

Reading the data from the file

Procedure

  1. Double-click tFileInputDelimited to open its Basic settings view.
  2. In the File name/Stream field, press Ctrl+Space and from the global variable list displayed select ((String)globalMap.get("tCreateTemporaryFile_1_FILEPATH")).
  3. Click the [...] button next to Edit schema and in the dialog box displayed define the schema by adding two columns: id of Integer type and name of String type.
  4. Click OK to validate the changes and accept the propagation prompted by the pop-up dialog box.
  5. Double-click tLogRow to open its Basic settings view.
  6. In the Mode area, select Table (print values in cells of a table) to display the output data in a better way.

Saving and executing the Job

Procedure

  1. Press Ctrl+S to save the Job.
  2. Press F6 or click Run on the Run tab to run the Job.
    The file talend_MHTI.dat is created under the default system temporary directory C:\Users\lena_li\AppData\Local\Temp\ during the Job execution, the five generated rows of data is written into it, then the file is deleted after the Job execution.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!