Calling a Talend Job from an external Java application

 Overview

Talend Studio constructs a Java environment. You can execute any Java code using Java components like tJava or tJavaRow. You can customize Java code in a routine. In addition, Talend Studio allows you to export the Job script and call it from external Java applications like Eclipse or Netbean. This procedure explains how to call a Talend Job from a Java program in Eclipse.

Environment

This procedure was written with:
  • Talend Open Studio for Data Integration 5.5.0 r117820
  • Sun JDK build 1.7.0_02 64-bit
  • Windows 7 Professional
  • Eclipse Standard/SDK: Luna release (4.4.0)

Talend verified this procedure to be compatible with all versions of Talend Studio.

 

Icon

Starting from version 6.0, Talend Studio requires a JDK installation to build jobs completely, for more information, please refer to this article.

 

Procedure

Create a demo Job

This example begins by creating an example Job called 'CallJobInEclipseDemo'.

In this Job, you will:

  • Define two context variables: context.nb_line and context.name.
  • Use a tFixedFlowInput component to create rows of data and append them to a file (such as: "D:/file/out.txt").

The detailed Job settings are as follows:

  1. Create and define the context variables in the Contexts tab:



  2. Click the Values as table tab and set their default value:



  3. Configure the tFixedFlowInput component in order to generate an input data:



  4. Configure the tFileOutputDelimited component in order to output the data into a file ("D:/file/out.txt" in this example),
  5. Take care to select the Append checkbox so that the data gets added to an existing file, and not overwritten every time the Job is executed.

Icon

This Job is also available in the attachments list. In order to import the demo job into your studio, you have to use the Studio version 5.5.0 r117820 or higher.

Execute your demo Job in the Talend Studio

After designing the Job, execute the Job to ensure it works well in the Talend Studio. Open the "D:file/out.txt" file to check that the data is properly appended: Default name 'talend' appended with the current system timestamp.

talend;2014-08-04 09:34:13

Export the Job script

Refer to the export job script section in this article for instructions on how to export a Job script.

 

Integrate the Talend Job into Eclipse

Follow the steps to import the Talend Job into a Java project in Eclipse:

  1. Launch Eclipse and create a new Java project. For this example, call it P1.



  2. Create a new Java class called TalendExample that will call the Talend Job.





  3. Right-click the root folder of the P1 project, then select Build path.
  4. Then select the Configure Build Path option.



  5. In the next window, open the Libraries tab and click Add external Jars button to import the Job jar and all the dependent jars. (For example, this demo Job contains the following jars)

    calljobineclipsedemo_0_1.jar (Located in <Job script unzip dir>\CallJobInEclipseDemo\)
    axis.jar (Located in <Job script unzip dir>\lib\)
    dom4j-1.6.1.jar (Located in <Job script unzip dir>\lib\)
    jaxrpc.jar (Located in <Job script unzip dir>\lib\)
    systemRoutines.jar (Located in <Job script unzip dir>\lib\)
    talend_file_enhanced_20070724.jar (Located in <Job script unzip dir>\lib\)
    talendcsv.jar (Located in <Job script unzip dir>\lib\)
    userRoutines.jar (Located in <Job script unzip dir>\lib\)

  6. Import the context configuration file Default.properties
    1. Open the <Job script unzip dir>\CallJobInEclipseDemo folder.
    2. Copy the di_demo folder (di_demo is the project name in this example).



    3. In Eclipse, paste it to the src folder.

  7. Open the TalendExample.java class and edit its code as follows:

    package org.talend;
    import di_demo.calljobineclipsedemo_0_1.CallJobInEclipseDemo;
    public class TalendExample {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            CallJobInEclipseDemo talendJob=new CallJobInEclipseDemo();
            talendJob.runJob(new String[]{});
        }
    }

Execute the Java program in Eclipse

Execute the TalendExample.java class. Open the "D:file/out.txt" file and see whether a new line of data is appended, if so, it means the Job has been executed successfully in the Java program.

talend;2014-08-04 09:34:13
talend;2014-08-04 10:57:37

Pass new values to context variables

You can also pass new values to the context variables when calling a Talend Job in a Java program. This functionality is useful in real-world cases, because the values may be provided by other code snippets.

For example, edit the TalendExample.java class as follows:

package org.talend;
import di_demo.calljobineclipsedemo_0_1.CallJobInEclipseDemo;
public class TalendExample {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CallJobInEclipseDemo talendJob=new CallJobInEclipseDemo();
        int nb_line=2;
        String name="Ross";
        String [] context=new String[] {"--context_param nb_line="+nb_line,"--context_param name="+name};
        talendJob.runJob(context);
    }
}

Execute the TalendExample.java class. Open the "D:file/out.txt" file and see that the two lines of data are appended:

talend;2014-08-04 09:34:13
talend;2014-08-04 10:57:37
Ross;2014-08-04 11:12:04
Ross;2014-08-04 11:12:04
Version History
Revision #:
1 of 1
Last update:
‎04-17-2017 05:25 PM
Updated by:
 
Labels (1)
Contributors