ComposantsComponents

ComposantsComponents › TraitementProcessing

tWriteDynamicFields

tWriteDynamicFields

TraitementProcessing

Regroupe des colonnes du flux dans une colonne Dynamic : chaque colonne d'entrée qui n'est pas gardée à part y devient un champ, avec son nom, son type, sa longueur, son motif et sa valeur. La suite du job peut ainsi traiter un nombre de colonnes variable d'un bloc : un tFileOutputDelimited les écrit toutes, en-tête compris. L'inverse est le tExtractDynamicFields, qui présente aussi le type Dynamic dans Talaxie et les composants qui l'acceptent. Packs columns of the flow into a Dynamic column: each input column that is not kept apart becomes a field there, with its name, its type, its length, its pattern and its value. The rest of the job can thus handle a variable number of columns as a block: a tFileOutputDelimited writes them all, header included. The reverse is the tExtractDynamicFields, which also presents the Dynamic type in Talaxie and the components accepting it.

EntréeInput

Main un flux, avec ou sans colonne Dynamicone flow, with or without a Dynamic column

SortieOutput

Main un flux : une ligne par ligne reçue, dont une colonne Dynamicone flow: one row per row received, including a Dynamic column

Déclencheurs en sortieOutput triggers

On Component Ok On Component Error Run if

UtilisationUsage

row1 (Main) row2 (Main) tFixedFlowInput_1 tWriteDynamicFields_1 tFileOutputDelimited_1

Le tWriteDynamicFields garde id à part et range les autres colonnes dans fields ; le fichier reçoit toutes les colonnes, et leurs noms en en-tête. The tWriteDynamicFields keeps id apart and puts the other columns in fields; the file gets all the columns, and their names in the header.

  1. Déposez le tWriteDynamicFields depuis la famille Traitement de la palette et reliez-lui le flux. Drop the tWriteDynamicFields from the Processing family of the palette and connect the flow to it.
  2. Dans son Schema, recopiez les colonnes à garder à part, avec le même nom et le même type qu'en entrée, puis ajoutez en dernier une colonne de type Dynamic. In its Schema, copy the columns to keep apart, with the same name and the same type as in the input, then add last a column of type Dynamic.
  3. Reliez la sortie à un composant qui accepte le type Dynamic (tFileOutputDelimited, tLogRow, tJavaRow, tExtractDynamicFields…). Connect the output to a component accepting the Dynamic type (tFileOutputDelimited, tLogRow, tJavaRow, tExtractDynamicFields…).

ParamètresSettings

Le composant n'a qu'un paramètre, son schéma ; le libellé est le même en français et en anglais. Il n'a pas de paramètres avancés. The component has only one setting, its schema; the label is the same in French and in English. It has no advanced settings.

ParamètreSetting DéfautDefault VisibleShown Description
SchemaSCHEMA videempty ToujoursAlways Les colonnes de sortie : des colonnes ordinaires, recopiées de l'entrée (même nom, même type), et une seule colonne Dynamic, la dernière. Celle-ci reçoit : The output columns: ordinary columns, copied from the input (same name, same type), and a single Dynamic column, the last one. This one gets:
  • une copie de la colonne Dynamic d'entrée, s'il y en a une ;a copy of the Dynamic input column, if there is one;
  • puis, à la suite, chaque colonne d'entrée absente du schéma de sortie, dans l'ordre de l'entrée.then, after it, each input column missing from the output schema, in the order of the input.

Ce que garde chaque champWhat each field keeps

Le nom de la colonne, son nom en base (celui d'origine, sinon le nom), son type Talend (id_Integer…), son type en base, sa longueur, sa précision, nullable, clé, son motif, et sa valeur dans son type Java (Integer, Date…), sans conversion en texte. The column name, its database name (the original one, otherwise the name), its Talend type (id_Integer…), its database type, its length, its precision, nullable, key, its pattern, and its value in its Java type (Integer, Date…), without conversion to text.

Variable globaleGlobal variable

NB_LINE (Integer, disponible après l'exécution du composant) : nombre de lignes traitées. NB_LINE (Integer, available after the component has run): number of rows processed.

((Integer) globalMap.get("tWriteDynamicFields_1_NB_LINE"))

ExemplesExamples

Un tFixedFlowInput envoie deux lignes au tWriteDynamicFields_1, dont le schéma de sortie est id (Integer), fields (Dynamic). Les sorties sont copiées depuis des exécutions des jobs dans Talaxie (Java 21). A tFixedFlowInput sends two rows to tWriteDynamicFields_1, whose output schema is id (Integer), fields (Dynamic). The outputs are copied from runs of the jobs in Talaxie (Java 21).

idInteger nameString cityString ageInteger sinceDate "yyyy-MM-dd"
1AdaLondon362020-01-15
2Alannull41null

Les champs du DynamicThe fields of the Dynamic

row1 (Main) row2 (Main) row3 (Main) OnSubjobOk tFixedFlowInput_1 tWriteDynamicFields_1 tJavaRow_1 tLogRow_1 tJava_1

Le tJavaRow liste chaque champ : nom, type, classe Java de la valeur, valeur. Le tLogRow est en tableau. Le tJava affiche NB_LINE. The tJavaRow lists each field: name, type, Java class of the value, value. The tLogRow is a table. The tJava prints NB_LINE.

routines.system.Dynamic dyn = input_row.fields;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dyn.getColumnCount(); i++) {
    routines.system.DynamicMetadata m = dyn.getColumnMetadata(i);
    Object v = dyn.getColumnValue(i);
    sb.append(i == 0 ? "" : ", ").append(m.getName()).append(" (").append(m.getType()).append(", ").append(v == null ? "null" : v.getClass().getSimpleName()).append(")=").append(v);
}
System.out.println("fields: " + sb);
output_row.id = input_row.id;
output_row.fields = input_row.fields;
System.out.println("tWriteDynamicFields_1_NB_LINE: " + globalMap.get("tWriteDynamicFields_1_NB_LINE"));
fields: name (id_String, String)=Ada, city (id_String, String)=London, age (id_Integer, Integer)=36, since (id_Date, Date)=Wed Jan 15 00:00:00 CET 2020
fields: name (id_String, String)=Alan, city (id_String, null)=null, age (id_Integer, Integer)=41, since (id_Date, null)=null
tLogRow_1
+----+------+--------+-----+------------------------------+
| id | name | city   | age | since                        |
+----+------+--------+-----+------------------------------+
| 1  | Ada  | London | 36  | Wed Jan 15 00:00:00 CET 2020 |
| 2  | Alan | null   | 41  | null                         |
+----+------+--------+-----+------------------------------+
tWriteDynamicFields_1_NB_LINE: 2

Les quatre colonnes absentes du schéma de sortie sont dans fields, avec leur type et leur valeur Java. Le tLogRow déplie le Dynamic : ses colonnes s'affichent comme les autres. La date, elle, s'affiche par Date.toString(), sans le motif de sa colonne d'origine (voir les limites). The four columns missing from the output schema are in fields, with their type and Java value. The tLogRow unfolds the Dynamic: its columns are displayed as the others. The date, however, is displayed by Date.toString(), without the pattern of its original column (see the limits).

Écrire un fichierWriting a file

Le tWriteDynamicFields_1 vers un tFileOutputDelimited (C:/data/packed.csv, séparateur ;, Inclure l'en-tête) ; un tJava affiche ensuite le fichier : The tWriteDynamicFields_1 to a tFileOutputDelimited (C:/data/packed.csv, ; separator, Include Header); a tJava then prints the file:

System.out.print(new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("C:/data/packed.csv")), "UTF-8"));
id;name;city;age;since
1;Ada;London;36;Wed Jan 15 00:00:00 CET 2020
2;Alan;;41;

L'en-tête reprend les noms des champs ; une valeur nulle donne une case vide. The header takes the names of the fields; a null value gives an empty cell.

Entrée déjà dynamiqueInput already dynamic

Un tFileInputDelimited lit C:\data\people.csv (en-tête id;name;city;age;since) en id, fields (Dynamic) ; un tExtractDynamicFields en sort name et garde le reste dans fields ; le tWriteDynamicFields (id, fields) y remet name : A tFileInputDelimited reads C:\data\people.csv (header id;name;city;age;since) as id, fields (Dynamic); a tExtractDynamicFields takes name out and keeps the rest in fields; the tWriteDynamicFields (id, fields) puts name back:

tLogRow_1
+----+----------+-----+------------+-------+
| id | city     | age | since      | name  |
+----+----------+-----+------------+-------+
| 1  | London   | 36  | 2020-01-15 | Ada   |
| 2  |          | 41  |            | Alan  |
| 3  | New York |     | 2019-07-01 | Grace |
+----+----------+-----+------------+-------+

Le contenu du Dynamic d'entrée vient d'abord, les colonnes ajoutées ensuite : name passe en dernier. The content of the input Dynamic comes first, the added columns after: name moves to the end.

Erreurs et limitesErrors and limits

Colonne gardée à part qui ne correspond pasColumn kept apart that does not match

Schéma de sortie id (String, alors qu'il est Integer en entrée), town (absente de l'entrée), fields (Dynamic) : la génération s'arrête, le job ne démarre pas. Output schema id (String, while it is Integer in the input), town (missing from the input), fields (Dynamic): the generation stops, the job does not start.

tWriteDynamicFields_1 : tWriteDynamicFields: static output must match an input field: id
tWriteDynamicFields_1 : tWriteDynamicFields: static output must match an input field: town

Schéma de sortie sans colonne Dynamic : Output schema without a Dynamic column:

tWriteDynamicFields_1 : tWriteDynamicFields requires a Dynamic output column.

Ces messages n'existent qu'en anglais. Pour renommer ou convertir une colonne, faites-le avant, dans un tMap. These messages only exist in English. To rename or convert a column, do it before, in a tMap.

Les dates perdent leur motifDates lose their pattern

Le motif d'une colonne Date est gardé dans le champ, mais ni le tLogRow ni le tFileOutputDelimited ne s'en servent : la date s'écrit Wed Jan 15 00:00:00 CET 2020, dans le fuseau de la machine (voir les exemples). Pour écrire 2020-01-15, convertissez la date en texte avant le tWriteDynamicFields (TalendDate.formatDate("yyyy-MM-dd", row.since) dans un tMap). Relue par un tExtractDynamicFields dans une colonne Date, elle garde sa valeur. The pattern of a Date column is kept in the field, but neither the tLogRow nor the tFileOutputDelimited use it: the date is written Wed Jan 15 00:00:00 CET 2020, in the time zone of the machine (see the examples). To write 2020-01-15, convert the date to text before the tWriteDynamicFields (TalendDate.formatDate("yyyy-MM-dd", row.since) in a tMap). Read back by a tExtractDynamicFields into a Date column, it keeps its value.

Le composant suivant doit accepter le type DynamicThe next component must accept the Dynamic type

Un composant qui ne prend pas en charge le type Dynamic, un tSortRow par exemple, arrête la génération s'il reçoit la colonne Dynamic (Ce composant ne supporte pas le type Dynamique). Voir la liste des composants qui l'acceptent sur la page du tExtractDynamicFields. A component that does not support the Dynamic type, a tSortRow for example, stops the generation if it gets the Dynamic column (This component doesn't support Dynamic Type). See the list of the components accepting it on the page of the tExtractDynamicFields.

Relire les champsReading the fields back

Pour reprendre un champ comme colonne ordinaire, un tExtractDynamicFields le sort par son nom ; dans du code Java, parcourez getColumnCount(), getColumnMetadata(i) et getColumnValue(i), comme dans le tJavaRow des exemples. To take a field back as an ordinary column, a tExtractDynamicFields takes it out by its name; in Java code, go through getColumnCount(), getColumnMetadata(i) and getColumnValue(i), as in the tJavaRow of the examples.

Voir aussiSee also