Jump to content
We've recently updated our Privacy Statement, available here ×
  • Java Reflection in Talend Data Integration Jobs


    German version (original): http://blog.itransparent.de/java-reflection-in-talend-data-integration-jobs

    A code generator (using JET) transforms Talend jobs to Java code. Schemas are represented by inner Java classes.

    Background: A webservice call used a string containing comma-separated values that should be read from an Excel file. The set of fields should be configurable at runtime.

    Solution:

    The component tJavaRow got out4 as input and read the relevant fields (tXMLMap created the SOAP document for the webservice call). The choice of relevant fields ist stored in a context variablen as comma-separated string at runtime.

    tJavaRow_3 (completely):

    String delims = "[,]";String[] attributes = context.Attributes.split(delims);String s = "";for (int i=0;i<attributes.length; i++){	// Replace empty spaces and brackets in the same way as Talend	String methodName="get"+attributes[i].replaceAll("\\s|\\(|\\)","_");	java.lang.reflect.Method method = input_row.getClass().getMethod(methodName);	String t =(String)method.invoke(input_row);	if(i>0) s+=",";	s+="\""+((t == null)?"":t)+"\"";}output_row.attributes=s;

    You can see that this component uses input_row and output_row internally instead of out4 and row7 respectively. Generally input_row has identical attributes as stream out4. It is not self-evident that Java reflection can be applied directly to input_row. The code from tJavaRow is transformed to this code fragment (manually formatted):

    ...String delims = "[,]";String[] attributes = context.Attributes.split(delims);String s = "";for (int i = 0; i < attributes.length; i++) {	// Replace empty spaces and brackets in the same way as Talend	String methodName = "get"+ attributes[i].replaceAll("\\s|\\(|\\)", "_");	java.lang.reflect.Method method = out4.getClass().getMethod(methodName);	String t = (String) method.invoke(out4);	if (i > 0)  s += ",";	s += "\"" + ((t == null) ? "" : t)+ "\"";}row7.attributes = s;...

    The methods getXXX() that are called by Java reflection are provided by class out4Struct. Here, out4 is an object of class out4Struct. These classes are generated automatically by Talend. As Talend generates public fields in this version (5.3), Java reflection could access fields instead of methods, too.

    Résumé: tJavaRow provides an easy way for adding user-defined Java code to a Talend job. input_row and output_row are replaced directly without sophisticated checks or generation rules. This facilitates interesting opportunities like using Java reflection.


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...