In questo Percorso didattico collegato, creeremo una catena che illustra come convertire un file XML in CSV. Inoltre, esploreremo come confrontare i set di dati per isolare le differenze.
| Obiettivo di apprendimento primario | Il connettore XML |
| Obiettivi didattici secondari | Filtri intelligenti di trasformazione tabulare, modifica delimitatore e comandi di query avanzati |
| Prerequisiti | Configurare la connessione al connettore XML |
| Modello di supporto | CLP | Accesso ai dati XML |
Passo 1: Creare una catena
- Add a new Chain, and name the Chain: CLP | Accessing XML Data.
- Crea due variabili di catena come segue:
- Under Chain Variables, click the plus sign (+) twice.
- Usa i seguenti nomi e valori per le variabili:
- Variabile XML
- Name: cv-XML-Employee
- Value: https://cs-sftp-training-bucket.s3.amazonaws.com/cs-training/transformation-qs/employees.xml
- Variabile CSV
- Name: cv-CSV-Employee
- Value: https://cs-sftp-training-bucket.s3.amazonaws.com/cs-training/transformation-qs/employee_detail.csv
- Variabile XML
- Salva la catena.
Passo 2: Recuperare i dati in formato CSV
Usa il connettore HTTP per recuperare i dati relativi all'organico in formato CSV. Nelle fasi successive, recupereremo informazioni simili sull'organico in formato XML e poi confronteremo i set di dati per individuare le differenze.
- Add a GET Command from the HTTP Connector to the Start node.
-
Configura il comando utilizzando i seguenti valori:
Nome GET - Organico in formato CSV Nome utente <leave blank> Password <leave blank> Certificato CA <leave blank> Certificato <leave blank> Chiave privata del certificato <leave blank> Mostra risposta Controllato URL cv-CSV-Employee Chain Variable Stringa di query <leave blank> Tipo di contenuto testo/csv Risposta <leave blank> - Salva il comando.
Passo 3: Recuperare i dati in formato XML
Utilizza il connettore HTTP per recuperare i dati relativi agli effettivi in formato XML. Come indicato al punto 2, in una fase successiva confronteremo il set di dati XML con il set di dati CSV per individuare le differenze.
- Add a GET Command from the HTTP Connector to the Chain.
- Connect the Start Node (GET - Headcount in CSV Format) to the GET Command.
-
Configura il comando utilizzando i seguenti valori:
Nome GET - Organico in formato XML Nome utente <leave blank> Password <leave blank> Certificato CA <leave blank> Certificato <leave blank> Chiave privata del certificato <leave blank> Mostra risposta Controllato URL cv-XML-Employee Chain Variable Stringa di query <leave blank> Tipo di contenuto applicazione/xml Risposta <leave blank> - Salva il comando.
Passo 4: Convertire il file XML in formato delimitato
Usa il Connettore XML per convertire i dati XML in un formato CSV che ci permetta di sfruttare i comandi successivi della Catena che prevedono dati in formato delimitato.
- Add an Element List to CSV Command from the XML Connector to the Chain.
- Connect the GET - Headcount in XML Format Command to the Element List to CSV Command.
- Configura il comando utilizzando i seguenti valori:
- For the Input File parameter, select the Response Output from the GET - Headcount in XML Format Command.
- Leave the Input Text parameter blank.
- For the Path to root parameter, enter the following envelope information:
/employee_data/employee - Mark the Preview Result parameter checkbox.
-
For the Columns section, click the Add button 12 times.
For each of the added columns, enter the name of the field from the data record previewed in the previous step in both the Column Name and XPath fields.
Example: emp_id⚠️ Il campo XPath è sensibile alle maiuscole e alle minuscole e deve corrispondere al carattere della busta.
- Per il parametro Delimitatore di più valori, inserisci una virgola (|).
- Salva il comando.
- Pubblica, esegui ed esegui la catena.
-
Review the Log tab of the Element List to CSV - All Employees Command node and confirm a list of employees is displayed.
⚠️ Non saltare l'esecuzione e la revisione dei risultati di questo passaggio perché una copia di questo comando verrà creata nella catena e gli eventuali errori dovranno essere corretti in più istanze del comando.
Passo 5: Cambiare l'XML convertito da Tab-delimitato a Comma-delimitato
Usa il Connettore di Trasformazione Tabulare per convertire l'output generato dalla conversione XML da un formato delimitato da tabelle a un formato delimitato da virgole.
- Add a Change Delimiter Command from the Tabular Transformation Connector to the Chain.
- Connect the Element List to CSV - All Employees Command to the Change Delimiter Command.
- Configura il comando utilizzando i seguenti valori:
- Name: Change Delimiter - All Employees
- Input File: Converted File Output from the Element List to CSV - All Employees Command
- Input Delimiter: \t
- Output Delimiter: , (comma)
- Salva il comando.
Passo 6: Confronto dei set di dati CSV e XML per identificare le differenze
Use Advanced Query to systematically identify differences between the XML and native CSV data sets. In questo esempio, identifica tutti i record del dataset XML che sono diversi o mancanti dal dataset CSV. Tutti i record aggiuntivi trovati nel dataset CSV che non sono presenti nel dataset XML non saranno identificati da questo esercizio.
- Add an Advanced Query Command from the Tabular Transformation Connector to the Chain.
- Connect the Change Delimiter - All Employees Command to the Advanced Query Command.
- Name the Command: Advanced Query - All Employees.
- In the Tables section, click the Add button to add a second tab.
- Per la prima tabella:
- Specify the CSV Result Output from the Change Delimiter - All Employees Command.
- Specify XML as the Table Name.
- Per la seconda tabella:
- Specify the Response Output from the GET - Headcount in CSV Format Command.
Specify CSV as the Table Name. - In the Query parameter, enter the following:
Select * from XML EXCEPT Select * from CSV - In the Input Delimiter and Output Delimiter fields, specify Comma.
- Check the Preview Results option.
- Specify the Response Output from the GET - Headcount in CSV Format Command.
- Per la prima tabella:
- Salva il comando.
- Publish, Execute, and Run the Chain.
-
Review the Log tab of the Advanced Query - All Employees Command node and confirm the employee IDs match the below for the records returned by the query.
ID dipendente 01-6875791 10-4199621 38-3932553 63-0417180 78-3600453 85-6818050 97-2828467
Passo 7: Aggiungere un gruppo di comandi
Aggiungi un gruppo di comandi alla catena per organizzare meglio i comandi. Sebbene questo passo sia necessario ai fini dell'esercizio, non è un requisito tecnico per la Catena. Questo passo serve solo a illustrare la capacità di organizzare i nodi di una Catena.
- Add a Command Group to the Chain.
- Connect the GET - Headcount in XML Format Command to the In section of the Command Group.
- Click the Command Group and then select the dropper icon to change the Command color to red.
- Name the Group: Active Employees.
Passo 8: Filtrare i dati CSV solo per i dipendenti attivi
Utilizza un filtro intelligente per conservare solo i record dei dipendenti attivi. Quando definiamo il filtro, assumiamo che il campo terminato (termed) vuoto indichi che un dipendente è attivo.
- Add a Smart Filter Rows Command from the Tabular Transformation Connector to the Chain.
- Connect the Group Start from the Command Group to the Smart Filter Command.
- Configura il comando utilizzando i seguenti valori:
- Name the Command: Smart Filter Rows - CSV Data - Active Employees.
- For the Input file parameter, select the Response Output from the GET - Headcount in CSV Format Command.
- Leave the Output file parameter blank.
- Specify Comma as the Delimiter.
- In the Filters section complete the following steps:
- Click the Add button on the Text filters.
- In the Column name parameter, enter termed.
- Check the Case Insensitive option.
- From the Condition dropdown, select Equals.
- In the Compare Text parameter, enter true.
- Check the Not option.
- Leave the Trim checkbox unchecked.
This filter will now identify any record where the field "termed" does not contain a value of true (that is, it contains "false" or is blank).
- Leave the Preview Result option marked.
- The Inverse option allows the Smart Filter to either keep (checked) or remove (unchecked) all rows that match the filter criteria. In questo esercizio, vogliamo mantenere tutte le righe in cui il campo "termed" ha un valore vero o vuoto.
To achieve this, mark (check) the Inverse checkbox. - Salva il comando.
Passo 8: Filtrare i dati CSV solo per i dipendenti attivi
Utilizza un filtro intelligente per conservare solo i record dei dipendenti attivi. Quando definiamo il filtro, assumiamo che il campo terminato ("termed") sia vuoto e che un dipendente sia attivo.
- Add a Smart Filter Rows Command from the Tabular Transformation Connector to the Chain.
- Connect the Group Start from the Command Group to the Smart Filter Command.
- Configura il comando utilizzando i seguenti valori:
- Name the Command: Smart Filter Rows - CSV Data - Active Employees.
- For Input file, select the Response Output from the GET - Headcount in CSV Format Command.
- Leave the Output file parameter blank.
- Specify Comma as the Delimiter.
- In the Filters section,
- Click the Add button on the Text filters.
- In the Column name parameter, enter termed.
- Check the Case Insensitive option.
- From the Condition dropdown, select Equals.
- In the Compare Text parameter, enter true.
- Check the Not option.
- Leave the Trim checkbox unchecked.
This filter will now identify any record where the field "termed" does not contain a value of true (that is, it contains "false" or is blank).
- Leave the Preview Result checkbox marked (checked).
- The Inverse option allows the Smart Filter to either keep (checked) or remove (unchecked) all rows that match the filter criteria. In questo esercizio, vogliamo mantenere tutte le righe in cui il campo con il termine ha un valore vero o vuoto. To achieve this, mark (check) the Inverse checkbox.
- Salva il comando.
Passo 9: Filtrare i dati XML durante la conversione in formato delimitato
In questo passaggio, filtriamo i dati XML durante la conversione in formato delimitato per identificare solo i dipendenti attivi. Questo set di dati sarà successivamente confrontato con il set di dati CSV filtrati per identificare le differenze.
- Copy the Element List to CSV - All Employees Command.
- Connect the Group Start from the Command Group to the Element List to CSV - All Employees (Copy) Command.
- Edit the Command, naming it: Element List to CSV - Active Employees.
- In the "Path to root" parameter, enter the following.
/employee_data/employee[termed!='true']
This will now identify any record where the field "termed" does not contain a value of true (that is, it contains "false" or is blank). - Salva il comando.
Passo 10: Cambiare i dati XML filtrati in delimitati da virgole
Come per l'elenco completo dei dipendenti, l'elenco dei dipendenti attivi filtrati che è stato convertito da XML a tabellare deve essere convertito in un elenco di dipendenti delimitato da virgole.
- Copy the Change Delimiter - All Employees Command.
- Connect the Element List to CSV - Active Employees Command to the Element Change Delimiter - All Employees (Copy) Command.
- Configura il comando utilizzando i seguenti valori.
- Name: Change Delimiter - Active Employees.
- Modify the Input file parameter to use the Converted File Output from the Element List to CSV - Active Employees Command.
- Salva il comando.
Passo 11: Verifica le differenze con i dipendenti attivi
Come per l'elenco completo dei dipendenti, l'elenco dei dipendenti attivi filtrati che è stato convertito da XML a tabellare deve essere convertito in un elenco di dipendenti delimitato da virgole.
- Copy the Advanced Query - All Employees Command.
- Connect the Out of the Command Group to the Advanced Query - All Employees (Copy) Command. Collegare il comando Query avanzata - Tutti i dipendenti al Gruppo in uscita assicura che tutto ciò che è collegato al Gruppo in partenza finisca prima che la catena passi ai comandi del Gruppo in uscita.
- Configura il comando utilizzando i seguenti valori:
- Name: Advanced Query - Active Employees.
-
In the Tables section, modify the File parameter for both tables using the following values:
Tabella File XML CSV Result Output from the Change Delimiter - Active Employees Command CSV Smart Filter Row Output from the Smart Filter Rows - Active Only, CSV Data Command
- Salva il comando.
Passo 12: Prova l'esercizio
Ora che la Catena è completa, verifica il risultato.
- Publish the Chain.
- Click Execute and then select Run Chain.
- Once the Chain has completed its run, click the Advanced Query - Active Employees node.
- On the Outputs tab, confirm that 4 records were output.
-
Clicca sulla scheda Logs e conferma che gli ID dei dipendenti corrispondono a quelli riportati di seguito per i record restituiti dalla query.
ID dipendente 01-6875791 63-0417180 78-3600453 85-6818050
To learn more about data transformation using Chains, check out the Connected Learning Paths - Transformation Introduction!