Detecting the operating system at run time and dynamically setting the file path

author
Shicong Hong
EnrichVersion
6.4
6.3
6.2
6.1
6.0
EnrichProdName
Talend Open Studio for Big Data
Talend Open Studio for Data Quality
Talend Data Fabric
Talend Real-Time Big Data Platform
Talend Data Services Platform
Talend Open Studio for ESB
Talend Big Data Platform
Talend Big Data
Talend ESB
Talend MDM Platform
Talend Open Studio for MDM
Talend Cloud
Talend Open Studio for Data Integration
Talend Data Integration
Talend Data Management Platform
task
Design and Development > Designing Jobs
EnrichPlatform
Talend Studio

Why detecting the operating system at run time?

As Talend Studio supports cross-platform Job execution and development on one or several machines, you often need to know the current operating system when running a Talend Job.

Some components such as File components always require a local file path. Operating systems do not all have the same format for paths in the file system. In order to make the Job run on different operating systems, the component needs to detect the current operating system type and set a dynamic file path at runtime.

Detecting the operating system at run time and dynamically setting the file path

To allow the Job to run on different operating systems, you can set the file path using a context variable as in the following example Job.

This Job is composed of:

  • a tJava component to initialize the context variable and detect the operating system,
  • a FixedtFlowInput component to generate data,
  • a tFileOutputDelimited component to generate a .txt file.

Procedure

  1. Create a new job named DetectOSTypeDemo and place a tJava component from Palette on the workspace.
  2. Place a FixedtFlowInput and a tFileOutputDelimited on the workspace and connect them with a Row Main link. Connect the tJava to the tFixeFlowInput component with a Trigger > On Subjob Ok link.
  3. Click the tJava component to open its Basic setttings view, add the following code to the Code area to initialize the context variable and to detect the operating system:
    String os = System.getProperty("os.name").toLowerCase();  
    if(os.indexOf("win") >= 0){
    context.filePath="C:/temp/out.txt";
    }else { //Unix / Linux file path
    context.filePath="/var/temp/out.txt";
    }
  4. Click the FixedtFlowInput component to open its Basic setttings view.
  5. Click the Edit schema button and add a new column named name. In the Values box, add the value "Hello world" to generate output data as below:
  6. Open its contexts view to define a context variable used to the set the path of output file in Contexts views named filePath of String type using the plus button as below:
  7. Click the component view of FixedtFlowInput and set the file name with the following context variable context.filePath in the File Name field.
  8. Execute the Job. If the Job is executed on a Windows system, the data is written to the "C:/temp/out.txt" file. If the Job is executed on a Unix or Linux system, the data is written to the "/var/temp/out.txt" file.