Creating a Java archive for the user-defined indicator - 7.1

Talend Data Management Platform Studio User Guide

author
Talend Documentation Team
EnrichVersion
7.1
EnrichProdName
Talend Data Management Platform
task
Design and Development
EnrichPlatform
Talend Studio
Before creating a Java archive for the user defined indicator, you must define, in Eclipse, the target platform against which the workspace plug-ins will be compiled and tested.

Before you begin

You have selected the Profiling perspective of Talend Studio.

Procedure

  1. Define the target platform:
    1. In Eclipse, select Preferences to display the Preferenceswindow.
    2. Expand Plug-in Development and select Target Platform then click Add... to open a view where you can create the target definition.
    3. Select the Nothing: Start with an empty target definition option and then click Next to proceed to the next step.
    4. In the Name field, enter a name for the new target definition and then click the Add... button to proceed to the next step.
    5. Select Installation from the Add Content list and then click Next to proceed to the next step.
    6. Use the Browse... button to set the path of the installation directory and then click Next to proceed to the next step.

      The new target definition is displayed in the location list.

    7. Click Finish to close the dialog box.
  2. Create a Java archive for the user defined indicator
    1. In Eclipse, check out the project from GIT at https://github.com/Talend/tdq-studio-se/tree/master/sample/test.myudi.

      In this Java project, you can find four Java classes that correspond to the four indicator categories listed in the Indicator Category view in the indicator editor.

      Each one of these Java classes extends the UserDefIndicatorImpl indicator. The figure below illustrates an example using the MyAvgLength Java class.

      package test.udi;
      
      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;
      
      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {
      
          private double length = 0;
      
          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }
      
          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }
      
          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }
      
      }
    2. Modify the code of the methods that follow each @Override according to your needs.
    3. Optional: If required, use the following methods in your code to retrieve the indicator parameters:
      Method Description
      Indicator.getParameter() Returns an IndicatorParameters object
      IndicatorParameters.getIndicatorValidDomain() Returns a Domain object
      Domain.getJavaUDIIndicatorParameter() Returns a list of JavaUDIIndicatorParameter that stores each key/value pair that defines the parameter
    4. Save your modifications.
    5. Using Eclipse, export this new Java archive.

Results

The Java archive is now ready to be attached to any Java indicator you want to create in from Profiling perspective of Talend Studio.