Calling a Talend Job from an external Java application

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

Calling a Talend Job from an external Java application

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.

Create a demo Job

This example begins by creating an example Job called CallJobInEclipseDemo.

In this Job, you will:

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

Procedure

  1. Create and define the context variables in the Contexts tab:
    • name: type String
    • nb_line: type Integer
  2. Click the Value field of each variable and set their default values, talend and 1 respectively.
  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. Select the Append check box so that the data gets added to an existing file, and not overwritten every time the Job is executed.
  6. Execute the Job to ensure it works well in the Talend Studio. Open the file D:file/out.txt to check that the data is properly appended: Default name 'talend' appended with the current system timestamp.
    talend;2014-08-04 09:34:13
  7. Export the Job by following the instructions in Exporting a Job script and executing it outside of Talend Studio.

Integrate the Talend Job into Eclipse

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

Procedure

  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 the Add external Jars button to import the Job jar and all the dependent jars.

    For example, this demo Job contains the following jars (Located in <Job script unzip dir>\CallJobInEclipseDemo\):

    • calljobineclipsedemo_0_1.jar
    • axis.jar
    • dom4j-1.6.1.jar
    • jaxrpc.jar
    • systemRoutines.jar
    • talend_file_enhanced_20070724.jar
    • talendcsv.jar
    • userRoutines.jar
  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[]{});
    	}
    }
  8. Execute the TalendExample.java class
  9. Open the file D:file/out.txt and see whether a new line of data is appended.

    If so, it means the Job has been executed successfully in the Java program.

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.

Procedure

  1. Edit the TalendExample.java class.

    In this example, add two lines to the output.

    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);
    	}
    }
  2. Execute the TalendExample.java class.
  3. Open the file D:file/out.txt and check 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