How to Use Java Classes In Talend ? : Step-By-Step Process with REAL-TIME Examples
How-to Use-Java-Classes-In-Talend-ACTE

How to Use Java Classes In Talend ? : Step-By-Step Process with REAL-TIME Examples

Last updated on 25th Dec 2021, Blog, General

About author

Bharathi Suresh (Talend ETL Developer )

Bharathi Suresh has extensive experience with SQL, Teradata, Data warehousing, Talend open studio,MDM & MDM, ETL, mySQL. His articles assist in sharing information and abilities in core fields and provide students with informative knowledge.

(5.0) | 19587 Ratings 1534

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.

    • Using Java In Talend
    • Introduction to How to Use Java In Talend
    • Performing one-off pieces of logic using tJava
    • Setting the context and globalMap variables using tJava
    • Adding complex logic into a flow using tJavaRow
    • Importing JAR files to allow the use of external Java classes
    • Importing Java Classes
    • Conclusion

    Subscribe For Free Demo

    [custom_views_post_title]

      Using Java In Talend:

      Java is an extremely popular and incredibly rich programming language. Talend is a Java code generator that uses several open-source Java libraries, so this means that Talend functionality can be easily extended by integrating Java code into Talend jobs.


      The Java representation allows you to convert Java object instances. You can import Java classes individually, from a folder, or in a JAR file, and the Java importer will generate structure definitions from each class. At runtime, you can provide Java objects as the source of the conversion or accept them as the result. This section contains recipes that show some of the techniques for using Java within Talend jobs.


      Introduction to How to Use Java In Talend:

    • For many data integration needs, the standard Talend component other than tMap provides a means of end-to-end processing of data without using Java code. For more complex requirements, it is often necessary to add additional Java logic to the job, and in other cases, it may be that adding custom Java code provides a simpler or more elegant or more efficient code than using standard components. To be.

      Performing one-off pieces of logic using tJava:

      The function tJava enables you to enter a personal code to be integrated into the Talend program. You can execute this code only once.

      Objective tJava makes it possible to extend the functionality of Talend Job using Java commands. The tJava component allows one-time logic to be added to the task. Common uses of tJava include setting global or context variables and printing logging messages before key data processing steps.


      • To get ready
      • Open job jo_cook_ch05_0000_tJava.
      • How to achieve it…
      • tJava. open
      • Type in the following code:
      • System.out.println(“Executing job “+jobName+”+TalendDate.getDate(“CCYY-MM-dd HH:mm:ss”));
      • run the job You will see that a message is printed showing the job name and the date and time of execution.

      How it works.

      If you examine the code, you will see that the Java code is simply appended to the generated code. This is why you must remember to add; to the end of the line to avoid compilation errors.


      Setting the context and globalMap variables using tJava:

      Although this recipe focuses on the use of tJava, it also serves as a convenient means of demonstrating how context and global map variables can be directly referenced from within the majority of Talend components.


      • To get ready
      • Open jo_cook_ch05_0010_tJavaContextGlobalMap, then open the context panel, and you should see a variable called testValue.
      • How to achieve it…
      • Open tMap_1 and type the following code:
      • System.out.println(“tJava_1”);
      • context.testValue = “TestValue is now initialised”;
      • GlobalMap.put(“gmTestValue”, “gmTestValue is now initialised”);
      • Open tMap_2 and type the following code:
      • System.out.println(“tJava_2”);
      • System.out.println(“context.testValue is:”+context.testValue);
      • System.out.println(“gmTestValue is: ” + (string)
      • GlobalMap.get(“gmTestValue”));

    • Run the job You will see that the variables initialised in the first tJava are printed correctly in the second.
    • The context and globalMap variables are stored as globally available Java HashMaps, which means they are key values. This enables these values ​​to be referenced in another component, such as tMap, tFixedFlowInput, and tFileInputDelimited.
    • This recipe sets the variables in a one-time fashion using tJava. It’s worth noting that the same principles apply to tJavaRow. Since tJavaRow is called for each row processed, it is possible to create a global variable for a row that can be referenced by all components in the flow. This can be useful when pre and post field values ​​are needed for comparison purposes later in the flow. Storing it in a GlobalMap variable avoids the need to create additional schema columns.

      Adding complex logic into a flow using tJavaRow:

      The function tJavaRow allows you to enter a customised code that you can integrate into the Talend program. With tJavaRow, you can enter Java code to be applied to each line of the flow.

      But tJavaRow allows you to extend the functionality of Talend Jobs, using the Java language.

      • The tJavaRow component allows Java logic to be executed for each record within the flow.
      • To get ready
      • Open job jo_cook_ch05_0020_tJavaRow.
      • How to achieve it…
      • Add tJavaRow and tLogRow
      • Link the flows as shown in the following screenshot:
      • tjavarow

      Open the schema and you will see that there are no fields in the output. Highlight Name, Date of Birth and Age and click on the single arrow.

    • Use the + button to add new columns for the cleaned up name (string) and row count (integer), so that the schema looks like the following:
    • Close the schema by pressing OK and then pressing the Generate Code button in the main tJavaRow screen. The generated code will be as follows:

      • tJavaRow_1<
      • // generate code according to input schema and output schema
      • output_row.name = input_row.name;
      • output_row.dateOfBirth = input_row.dateOfBirth;
      • output_row.age = input_row.timestamp;
      • output_row.cleanedName = input_row.age;
      • output_row.rowCount = input_row.age;
      • Change row age = input_row.timestamp from code to read output_row.age = input_row.age .
      • Remove the rows for cleanName and output_row.rowCount, and replace with the following code:
      • if (input_row.name.startsWith(“J”)) {
      • output_row.cleanedName =
      • StringHandling.EREPLACE(input_row.name, “J”, “James”);
      • ,
      • if (input_row.name.startsWith(“Joe”)) {
      • output_row.cleanedName =
      • StringHandling.EREPLACE(input_row.name, “Joe”, “Joan”);
      • ,
      • output_row.rowCount=Numeric.sequence(“s1”,1,1);
      • output_row.rowCount=Numeric.sequence(“s1”,1,1);

      Importing JAR files to allow the use of external Java classes:

      Talend has a rich set of functions and libraries available within its suite. However, you may want to use libraries provided by the data vendor or API vendor e.g. If you want to get data from Google Adwords you may want to include or import the library/jar files provided by Google into Talend.


      Sometimes, during development, it is necessary (or simpler) to use Java classes that are not already included in Talend. These can be pre-existing Java code such as financial calculations or open source libraries provided by the Apache Software Foundation.


      In this example, we will use a simple Java class ExternalValidations and its ExternalValidateCustomerName method. This class performs the following simple validation:


      Course Curriculum

      Develop Your Skills with Java Certification Training

      Weekday / Weekend BatchesSee Batch Details
      • if (customerName.startsWith(“J”)) {
      • Return customer name. replace(“J”, “James”);
      • } Otherwise {
      • if (customerName.startsWith(“Jo”)) {
      • Return customer name. replace(“Joe”, “Joan”); } Otherwise {
      • return customer name;
      • To get ready
      • open job jo_cook_ch05_0050_externalClasses.
      • how to do this…
      • Create a code routine called external validation.
      • Right-click and select Edit Routine Library.
      • Click on New in the next dialog.
      • Select the option Browse Library File, and browse to the CookbookData folder which contains a sub-folder called External Jar. Click the jar, then click OK to confirm. The import dialog should now take note of the following:
      • import external library
      • Return to Tasks and open tJavaRow, and click on the Advanced Settings tab.
      • Add the following code:
      • import talentExternalClass.ExternalValidations;
      • Return to the Basic tab and add the following code:
      • output_row.validatedName =ExternalValidations.ExternalValidateCustomerName(input_row.name);
      • run the job You will see that the verification is done, and the customer names have been changed.

      Importing Java Classes:

      The Java representation allows you to convert Java object instances. You can import Java classes individually, from a folder, or in a JAR file, and the Java importer will generate structure definitions from each class. At runtime you can provide Java objects as the source of the conversion or accept them as the result.

      Select – Here you select the classes to import. Each class is read and any other classes that depend on this class are also processed (for example a reference class or superclass). These dependent classes are resolved using the classpath (specified below Properties). Initially, the classpath is set up to be the same as the entries selected for import, but there are some cases where you might not want this. For example, if you’re selecting folders containing classes to import, you’ll want only a few folders, but your classpath needs to have an attached top-level folder (such as bin) for any classes to resolve correctly. folder) needs to be specified.


      Create Element From – You can specify fields, which creates struct elements from fields of your classes, or specify bean properties to create struct elements from JavaBean properties.


      Include Excluded Fields/Properties – Includes excluded fields or properties by default. If you don’t want them to happen, turn it off.


      Create structures as – You can control how you name structures based on the class of the objects. Note that for small numbers of objects, using simple class names can make things more manageable.


      Handling references to classes containing subclasses – see Java Subclass Generation below.


      Classpath – When processing Java classes for import, dependent classes must be resolved against the classpath. This is where you can add JAR files or folders (which can be external or resources) to the classpath. If you don’t specify anything, the selected file/resources/folders you are importing become the classpath.


    Java Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Conclusion:

      Talend Studio produces Java environments. You can execute any Java code using Java components such as tJava or tJavaRow. You can customise Java code in routines. In addition, Talend Studio allows you to export job scripts and call them from external Java applications such as Eclipse or NetBeans.


      Talend is a Java code generator that uses several open-source Java libraries, so this means that Talend functionality can be easily extended by integrating Java code into Talend jobs. The Java representation allows you to convert Java object instances.


      You do not need to know Java to use Talend. You will only need to know the Java language if you intend to enhance or add custom features to Talend Community Edition. Talend is an ETL tool for data integration. It provides software solutions for data preparation, data quality, data integration, application integration, data management and big data. Data integration and big data products are widely used.


    Are you looking training with Right Jobs?

    Contact Us

    Popular Courses

    Get Training Quote for Free