Thursday, June 13, 2013

Incoming Connections in Talend Open Studio Components


To work with data in your custom Talend Open Studio component, retrieve an IConnection.  IConnection provides the variable name for a row of data; "row1" is common in smaller jobs.

The XML descriptor for a Talend Open Studio component defines the incoming connections for the component, accessed using the IConnection Java interface.  Most components define a single incoming connection.  Exceptions include one-shot calls like tPreJob or tJava.  Also, tUnite uses more than one connection expressed as a special MERGE parameter.

This line from an XML descriptor defines a single input connection.




From the following Design Workspace Perspective screenshot, the input connection is "row1" setting tRules' input with a tRowGenerator output.  Had the connection been configured later in the job creation, it might have had a different identifier (say "row5").  Which ever identifier is displayed in the map, that's the variable name that is used in a main, as set in a JET section.

Incoming Connection "row1" in tRules
In a main, "row1" can be used as a variable for things like assignments.  "row2.field1=row1.field1" will log (using tLogRow)  field1's data from row1 to the equivalent field in row2.  To avoid a hardcoded value, set up a variable in a JET block like

<%
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
INode node = (INode)codeGenArgument.getArgument();
String cid = node.getUniqueName();

String incomingConnName = null;

List< ? extends IConnection> conns = node.getIncomingConnections();
if(conns!=null && conns.size()>0){
IConnection conn = conns.get(0);
incomingConnName = conn.getName();
}

%>

In this particular configuration, the number of incoming connections will be at most one.  Other requirements may need a loop on the structure 'conns'.

Later, outside of the JET block, "incomingConnName" can be used as follows


jc = new org.apache.commons.jexl2.MapContext();
jc.set("<%= incomingConnName %>", <%= incomingConnName %>);

o = filter.evaluate(jc);


The jc.set() puts a variable (evaluated as "row1") into a context using the key determined by "incomingConnName" which is also "row1", but a String value.

When building a Talend Open Studio component that needs to work with input data, retrieve an IConnection object using the getIncomingConnections() call on the node.  The returned list can be pared down to a single item in most cases ("get(0)") that can be interrogated.

No comments: