ComposantsComponents › Code personnaliséCustom Code
Trois parties de code Java : une au début, une pour chaque ligne, une à la fin. De quoi tenir un compteur ou un total sur tout un flux, ouvrir une ressource avant les lignes et la fermer après, ou produire soi-même des lignes avec une boucle. Three parts of Java code: one at the start, one for each row, one at the end. Enough to keep a counter or a total over a whole flow, open a resource before the rows and close it after, or produce rows yourself with a loop.
EntréeInput
Aucune : il peut commencer un sous-job. Sinon un lien Main ou Iterate. None: it can start a subjob. Otherwise one Main or Iterate link.
SortieOutput
Un lien Main, avec le schéma du composant, ou Iterate. One Main link, with the component schema, or Iterate.
Déclencheurs en sortieOutput triggers
On Subjob Ok On Subjob Error On Component Ok On Component Error Run if
On Subjob : seulement quand il commence son sous-job. On Subjob: only when it starts its subjob.
Une fois, avant la première ligne : déclarer un compteur, ouvrir un fichier, une connexion. Once, before the first row: declare a counter, open a file, a connection.
Pour chaque ligne, avec input_row (la ligne reçue) et output_row (la ligne envoyée).
For each row, with input_row (the incoming row) and output_row (the outgoing row).
Une fois, après la dernière ligne : afficher un total, fermer ce qui a été ouvert. Once, after the last row: print a total, close what was opened.
Les trois parties sont dans la même méthode Java : une variable déclarée dans le code initial se lit et se modifie dans les deux autres, et garde sa valeur d'une ligne à la suivante. The three parts are in the same Java method: a variable declared in the start code can be read and changed in the other two, and keeps its value from one row to the next.
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. Comme pour le tJavaRow, elles ne suivent pas le schéma d'entrée : Synchroniser les colonnes le recopie.
The columns of output_row. As for the tJavaRow, they do not follow the input schema: Sync columns copies it.
|
| Auto propagation des donnéesData Auto PropagateDATA_AUTO_PROPAGATE | OuiYes | Paramètres simplesBasic settings |
Avant le code principal, recopie dans output_row chaque colonne d'entrée qui a le même nom en sortie. Le code n'a plus qu'à remplir ou changer le reste. Décochée, rien n'est recopié.
Before the main code, copies into output_row each input column that has the same name in the output. The code only has to fill or change the rest. Unchecked, nothing is copied.
|
| Code initialStart codeCODE_START | un commentairea comment | Paramètres simplesBasic settings | Exécuté une fois, avant les lignes. Run once, before the rows. |
| Code principalMain codeCODE_MAIN | un commentairea comment | Paramètres simplesBasic settings |
Exécuté pour chaque ligne. input_row et output_row y sont remplacés par le nom des liens, seulement quand ce sont des mots entiers.
Run for each row. input_row and output_row are replaced in it with the link names, only when they are whole words.
|
| Code de finEnd codeCODE_END | un commentairea comment | Paramètres simplesBasic settings | Exécuté une fois, après les lignes. Run once, after the rows. |
| 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.
|
Le composant n'a pas de variable globale. Dans un tJavaFlex créé par une ancienne version (Talend Open Studio 3.2), la recopie automatique a lieu après le code principal et non avant. The component has no global variable. In a tJavaFlex created by an old version (Talend Open Studio 3.2), the automatic copy happens after the main code, not before.
Sorties copiées depuis une exécution du job. Outputs copied from a run of the job.
Entrée : id, name (1 Ada, 2 Grace, 3 Alan). Schéma du tJavaFlex : id, name, length. Auto propagation cochée : id et name sont recopiés, le code ne remplit que length.
Input: id, name (1 Ada, 2 Grace, 3 Alan). tJavaFlex schema: id, name, length. Auto propagate checked: id and name are copied, the code only fills length.
Code initialStart code
int total = 0;
Code principalMain code
output_row.length = input_row.name.length();
total += output_row.length;
Code de finEnd code
System.out.println("Total length: " + total);
1|Ada|3 2|Grace|5 3|Alan|4 Total length: 12
Le même job avec l'auto propagation décochée : seule la colonne remplie par le code a une valeur. The same job with auto propagate unchecked: only the column filled by the code has a value.
||3 ||5 ||4
En début de sous-job, sans entrée. Le code initial ouvre une boucle et le code de fin la ferme : tout ce qui est entre les deux, y compris les composants qui suivent, s'exécute à chaque tour. Schéma : i, square (Integer).
At the start of a subjob, without input. The start code opens a loop and the end code closes it: everything in between, including the next components, runs at each turn. Schema: i, square (Integer).
Code initialStart code
System.out.println("Start");
for (int i = 1; i <= 3; i++) {
Code principalMain code
output_row.i = i;
output_row.square = i * i;
Code de finEnd code
}
System.out.println("End");
Start 1|1 2|4 3|9 End
Une accolade ouverte dans le code initial doit être fermée dans le code de fin, et réciproquement. Sinon c'est le code de tout le job qui ne compile plus, et l'erreur est signalée loin du tJavaFlex. A brace opened in the start code must be closed in the end code, and vice versa. Otherwise the code of the whole job no longer compiles, and the error is reported far from the tJavaFlex.
Une ressource ouverte dans le code initial (fichier, connexion) se ferme dans le code de fin. Si une ligne provoque une erreur, le code de fin n'est pas exécuté et la ressource reste ouverte. Pour la fermer quand même, rangez-la aussi dans globalMap depuis le code initial, et fermez-la dans un tJava relié par On Component Error.
A resource opened in the start code (file, connection) is closed in the end code. If a row causes an error, the end code is not run and the resource stays open. To close it anyway, also store it in globalMap from the start code, and close it in a tJava connected with On Component Error.
Pour un simple calcul ligne à ligne, le tJavaRow suffit. Le tJavaFlex s'impose dès qu'il faut un état sur tout le flux, recopier des colonnes sans les écrire une à une, ou produire des lignes. Il remplace aussi les mots input_row et output_row plus prudemment.
For a simple row-by-row computation, the tJavaRow is enough. The tJavaFlex is the one as soon as you need a state over the whole flow, to copy columns without writing them one by one, or to produce rows. It also replaces the words input_row and output_row more carefully.
Les variables du code initial n'existent que dans le sous-job. Pour garder un total après lui, rangez-le dans le code de fin : globalMap.put("total", total);.
The start code variables only exist in the subjob. To keep a total after it, store it in the end code: globalMap.put("total", total);.
tJava · code exécuté une foiscode run oncetJavaRow · code exécuté pour chaque lignecode run for each rowtLibraryLoad · une bibliothèque Java pour le code du joba Java library for the job codetLogRow · afficher les lignes d'un fluxdisplay the rows of a flow