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

EnrichVersion

6.4

6.3

6.2

6.1

6.0

5.6

EnrichProdName

Talend Big Data

Talend Open Studio for Data Integration

Talend Open Studio for MDM

Talend Open Studio for Big Data

Talend Open Studio for Data Quality

Talend Data Fabric

Talend Open Studio for ESB

Talend Data Management Platform

Talend Big Data Platform

Talend ESB

Talend Integration Cloud

Talend Data Integration

Talend Real-Time Big Data Platform

Talend Data Services Platform

Talend MDM 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

Create a new job named DetectOSTypeDemo and place a
tJava component from Palette
on the workspace.

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.

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:

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:

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:

Click the component view of FixedtFlowInput and set the
file name with the following context variable
context.filePath in the File Name field.

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.