Groovy is a custom transformation plug-in that allows you to write Groovy code to transform the fields in each record to meet specific requirements.

For example, you can use Groovy to transform uppercase letters in values of a specific field to lowercase letters, add a prefix or suffix to all field values, or add attributes parsed from JavaScript Object Notation (JSON) data to other fields.

Implementation

The Groovy plug-in compiles and runs the custom Groovy code through the Groovy compiler. The code needs to inherit the com.alibaba.di.plugin.center.transformer.Transformer class and implement the Record evaluate(Record record) method.

Create a Groovy node

  1. Log on to the DataWorks console. In the left-side navigation pane, click Workspaces. On the Workspaces page, find the target workspace and click Data Analytics in the Actions column.
  2. On the Data Analytics tab, move the pointer over the Create a sync node icon and choose Data Integration > Real-Time Sync.

    You can also find the target workflow, right-click Data Integration, and choose Create > Real-Time Sync.

  3. In the Create Node dialog box that appears, set Node Name and Location, and then click Commit.
  4. On the configuration tab of the created real-time sync node, drag Groovy under Transform to the editing panel. Connect it to the desired reader in the panel.
  5. Click the Groovy node and set parameters in the Node Settings section.
    • Node Settings
      Groovy Code: the Groovy code to run. For example, to read data from a MySQL database, add the GROOVY suffix to the first field in the data, and write the transformed data to Datahub, enter the following code:
      import com.alibaba.di.plugin.center.element.Column;
      import com.alibaba.di.plugin.center.element.StringColumn;
      import com.alibaba.di.plugin.center.record.Record;
      import com.alibaba.di.plugin.center.transformer.Transformer;
      
      class MyTransformer extends Transformer {
          @Override
          public Record evaluate(Record record) {
              Column column = record.getColumn(0);
              String newValue = column.asString() + "GROOVY";
              record.setColumn(0, new StringColumn(newValue));
              return record;
          }
      }
    • Output Fields

      The output fields of the parent node that are used as the input fields of the current node.

    • Custom Field

      The output fields of the Groovy node after transformation.

  6. Click Save the settings in the toolbar.