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 |
<%
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.
Tnx for the great post!
ReplyDeleteone question: what's the purpose of those jexl expressions and how to use them in generated java code?
Hi Gabriele,
ReplyDeleteI'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
Tnx allot, Carl! It's definitively clear now. I can see JEXL expression can be really powerful, in TOS jobs.
Delete