Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Tuesday, April 12, 2011

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.

<CONNECTOR CTYPE="FLOW" MAX_INPUT="1" MAX_OUTPUT="0" />


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.

3 comments:

  1. Tnx for the great post!

    one question: what's the purpose of those jexl expressions and how to use them in generated java code?

    ReplyDelete
  2. Hi Gabriele,

    I'm using JEXL for two things in tScriptRules, a Talend component I wrote a year ago and put on the Exchange.

    1. A more concise syntax: 'row1.name = "Carl"' versus 'row1 != null && row1.name.equals("Carl")'

    2. Transport. A JEXL expression is a string that can be stored in a file, a String variable, or a data structure

    This link explains more about using JEXL

    http://bekwam.blogspot.com/2011/04/embedding-expressions-with-jexl-in.html

    Good luck

    ReplyDelete
    Replies
    1. Tnx allot, Carl! It's definitively clear now. I can see JEXL expression can be really powerful, in TOS jobs.

      Delete