Tuesday, June 11, 2013

Outgoing Connections in Talend Open Studio Custom Components

Outgoing Connections in Talend Open Studio Custom Components

When building a custom component for Talend Open Studio, start by defining the outgoing connections in the descriptor XML file packaged with the component.  Then, retrieve a handle to one or more outgoing connection objects which will provide you with a variable name to use in your JET code.
Talend Open Studio custom components contain an XML descriptor that includes a list of the allowed outgoing connections.  An outgoing connection that is flow-based like the common "Main" connection can be accessed through a list of outgoing connections for that particular component instance.  For example, an outgoing connection may be tagged with "row2" which can be used in JET code to manipulate the row2Struct data structure.

XML Descriptor

This block of XML code is from the Talend Exchange component tScriptRules.



 
 
 


Two outgoing connections -- FILTER and REJECT -- have been defined as outgoing flow connections.  There can only be one FILTER and one REJECT connect as specified in the MAX_OUTPUT attribute.

JET is a templating language that works like Java Server Pages.  Java code is generated by combining embedded Java sections with an overall format.  The following are blocks of JET code that will make a variable referencing the FILTER outoing connection available.

Accessing the Connections


First, acquire the set of outgoing connections in a main.javajet.  Even though only one is permitted in the XML descriptor, the data structure is still a list.

<%
List< ? extends IConnection > filter_conns = node.getOutgoingConnections("FILTER");

%>

Getting Info from Connection


Next, set a variable aside for later use.   The null check is important so that you don't try to access something that isn't there (there was no FILTER connection set by the user).

<%
   String filterRowName = null;
   if( filter_conns!=null && filter_conns.size()>0) {
      IConnection filter_c = filter_conns.get(0);
      filterRowName = filter_c.getName();
   }

%>

Using Connection Info to Manipulate Objects


This block of code does something with the filterRowName.  It uses the variable as the first part of a data structure ("Struct") and maps the input to the output.

// reassign output to empty struct based on auto-gen
<%= filterRowName %>Struct r_ok = new <%= filterRowName %>Struct();

// take an input column, map to its equivalent output column

<%
   for( IMetadataColumn col : metadata.getListColumns() ) {
%>
      r_ok.<%= col %> = <%= rowName %>.<%= col %>;
<%
   }
%>


A null check prior to this code should be performed as well.

The possible connections available to a Talend Open Studio custom component is set in the XML descriptor.  The actual connections are made by the user in their Talend Open Studio development.  A list of outgoing connections can be used to get a handle to a data structure (ex, "row2Struct") that can be manipulated in the custom component code for the desired functionality.

No comments: