ComposantsComponents

ComposantsComponents › Code personnaliséCustom Code

tJavaRow

tJavaRow

Code personnaliséCustom Code

Exécute votre code Java pour chaque ligne d'un flux : il lit la ligne reçue (input_row) et remplit la ligne envoyée au composant suivant (output_row). C'est le composant pour un calcul ou une transformation qu'un tMap n'exprime pas simplement. Runs your Java code for each row of a flow: it reads the incoming row (input_row) and fills the row sent to the next component (output_row). It is the component for a computation or a transformation that a tMap does not express simply.

EntréeInput

Main un flux, obligatoireone flow, required

SortieOutput

Main un flux, avec le schéma du composantone flow, with the component schema

Déclencheurs en sortieOutput triggers

On Component Ok On Component Error Run if

UtilisationUsage

row1 (Main) row2 (Main) tFileInputDelimited_1 tJavaRow_1 tDBOutput_1

Dans le code, input_row désigne la ligne qui arrive par row1, output_row celle qui part par row2. Le composant ne recopie rien de lui-même : chaque colonne de sortie reçoit ce que le code lui donne, sinon elle reste vide. In the code, input_row is the row arriving through row1, output_row the one leaving through row2. The component copies nothing by itself: each output column gets what the code gives it, otherwise it stays empty.

  1. Reliez la sortie d'un composant au tJavaRow, puis son entrée au composant suivant. Connect the output of a component to the tJavaRow, then its output to the next component.
  2. Donnez-lui son schéma de sortie : il ne le reprend pas tout seul du lien entrant. Synchroniser les colonnes copie celui de l'entrée ; Modifier le schéma ajoute ou retire des colonnes. Give it its output schema: it does not take it by itself from the incoming link. Sync columns copies the input one; Edit schema adds or removes columns.
  3. Écrivez le code, ou cliquez sur Générer le code : le Studio écrit une ligne output_row.colonne = input_row.colonne; par colonne, dans l'ordre des deux schémas, à la place de tout le code déjà saisi (il demande confirmation). Write the code, or click Generate code: the Studio writes one output_row.column = input_row.column; line per column, in the order of the two schemas, replacing all the code already typed (it asks for confirmation).

ParamètresSettings

Le nom technique, en gris, est celui du paramètre dans le fichier du job. The technical name, in grey, is the parameter name in the job file.

ParamètreSetting DéfautDefault OngletTab Description
SchémaSchemaSCHEMA videempty Paramètres simplesBasic settings Les colonnes de output_row, envoyées au composant suivant. Elles ne suivent pas le schéma d'entrée : Synchroniser les colonnes le recopie quand vous le voulez. The columns of output_row, sent to the next component. They do not follow the input schema: Sync columns copies it when you want.
CodeCODE un exemple en commentairea sample, commented out Paramètres simplesBasic settings Les instructions exécutées pour chaque ligne. input_row et output_row y sont remplacés par le nom des liens (row1, row2…). The statements run for each row. input_row and output_row are replaced in them with the link names (row1, row2…).
ImportIMPORT //import java.util.List; Paramètres avancésAdvanced settings Lignes import complètes, placées en tête de la classe du job : elles valent pour tout son code. Complete import lines, placed at the top of the job class: they apply to all its code.

Variable globaleGlobal variable

NB_LINE (Integer, disponible après le composant) : nombre de lignes traitées, ((Integer) globalMap.get("tJavaRow_1_NB_LINE")). Le code peut aussi utiliser context, globalMap et les routines, comme un tJava. NB_LINE (Integer, available after the component): number of rows processed, ((Integer) globalMap.get("tJavaRow_1_NB_LINE")). The code can also use context, globalMap and the routines, like a tJava.

ExemplesExamples

Un tFixedFlowInput envoie trois lignes (id Integer, name String : 1 Ada, 2 Grace, 3 Alan) au tJavaRow, suivi d'un tLogRow. Sorties copiées depuis une exécution du job. A tFixedFlowInput sends three rows (id Integer, name String: 1 Ada, 2 Grace, 3 Alan) to the tJavaRow, followed by a tLogRow. Outputs copied from a run of the job.

row1 (Main) row2 (Main) tFixedFlowInput_1 tJavaRow_1 tLogRow_1

Transformer chaque ligneTransform each row

Schéma de sortie : id, name, greeting. Un tJava relié au tFixedFlowInput par On Subjob Ok affiche NB_LINE. Output schema: id, name, greeting. A tJava connected to the tFixedFlowInput with On Subjob Ok prints NB_LINE.

output_row.id = input_row.id * 10;
output_row.name = input_row.name.toUpperCase();
output_row.greeting = "Hello " + input_row.name;
10|ADA|Hello Ada
20|GRACE|Hello Grace
30|ALAN|Hello Alan
Rows: 3

Une colonne oubliée reste videA forgotten column stays empty

Schéma de sortie : id, name. Le code ne remplit que name : id ne vient pas de l'entrée, il reste nul. Output schema: id, name. The code only fills name: id does not come from the input, it stays null.

output_row.name = input_row.name.toUpperCase();
|ADA
|GRACE
|ALAN

PiègesPitfalls

input_row remplacé partout, même dans un texteinput_row replaced everywhere, even in a text

Le remplacement se fait sur le texte du code, sans le comprendre : dans une chaîne, dans un commentaire, ou au milieu d'un nom (my_input_row). The replacement is done on the code text, without understanding it: in a string, in a comment, or in the middle of a name (my_input_row).

System.out.println("input_row.name = " + input_row.name);
output_row.id = input_row.id;
output_row.name = input_row.name;
row1.name = Ada
1|Ada
row1.name = Grace
2|Grace
row1.name = Alan
3|Alan

Évitez ces deux mots ailleurs que pour désigner les lignes. Le tJavaFlex ne remplace que les mots entiers. Avoid these two words anywhere else than to refer to the rows. The tJavaFlex only replaces whole words.

Sans lien de sortie, output_row n'existe pasWithout an output link, output_row does not exist

Un tJavaRow au bout d'un flux n'a pas de ligne de sortie : le mot reste tel quel et la compilation échoue (extrait du message). A tJavaRow at the end of a flow has no output row: the word stays as is and compilation fails (message excerpt).

output_row.id = input_row.id;
error: cannot find symbol
    output_row.id = row1.id;
    ^
  symbol:   variable output_row

Au bout d'un flux, n'utilisez que input_row, ou reliez la sortie à un composant. At the end of a flow, only use input_row, or connect the output to a component.

Valeurs nullesNull values

Une colonne nullable peut arriver vide : input_row.name.toUpperCase() lève alors une NullPointerException qui arrête le job. Testez-la d'abord : input_row.name == null ? null : input_row.name.toUpperCase(). A nullable column can arrive empty: input_row.name.toUpperCase() then throws a NullPointerException that stops the job. Test it first: input_row.name == null ? null : input_row.name.toUpperCase().

Une ligne entre, une ligne sortOne row in, one row out

Le tJavaRow ne peut ni filtrer ni multiplier les lignes, et n'a pas de code avant la première ou après la dernière. Pour filtrer, un tFilterRow ; pour un compteur ou un total, un tJavaFlex. The tJavaRow can neither filter nor multiply rows, and has no code before the first or after the last one. To filter, a tFilterRow; for a counter or a total, a tJavaFlex.

Voir aussiSee also