ETL from LOCAL/XML to CIFS/EXCELX.mql
ETL script with application 'supervision' compatibility
if (not (group exist "0001_folder1_folder2_folder3")) {
group add "0001_folder1_folder2_folder3";
};
script merge "folder1.folder2.folder3.step_1_source_connect.exe" true 5
(param
)
"Connect to the source"
{
# CONFIGURATION ;
-> "[CONF_NAME_OF_THE_FLOW]" "name_of_the_flow";
# INITIALIZATION ;
-> "[FLOW_PID]" [PID];
# HANDLE ;
try {
# Flow initialization;
stack flow_init [FLOW_PID] [CONF_NAME_OF_THE_FLOW] "{}";
stack flow_step [FLOW_PID] 1 "source_connect...";
# Local connection: don't use step 1;
log write "Source: Local connection" OK null null;
#Step 1 is valid;
stack flow_step [FLOW_PID] 1 "source_connect_ok";
include "folder1.folder2.folder3.step_2_extract.exe";
} {
#Step 1 is not valid;
stack flow_step [FLOW_PID] 1 "source_connect_KO";
# Give the error to the stack and stop the process;
exception (1) ([global_err]);
} "[global_err]";
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_1_source_connect.exe" "0001_folder1_folder2_folder3")) {
group grant script "folder1.folder2.folder3.step_1_source_connect.exe" "0001_folder1_folder2_folder3";
};
script merge "folder1.folder2.folder3.step_2_extract.exe" true 1
(param
)
"Extract files from the source"
{
# CONFIGURATION ;
-> "[CONF_DIR_SOURCE]" "tmp/source";
-> "[CONF_SOURCE_FILTER]" ".*xml";
# HANDLE ;
try {
# Mark the flow as step 2;
stack flow_step [FLOW_PID] 2 "extract...";
# Create the local directory PID;
file mkdir (concat "home/" [FLOW_PID]);
# Get files into PID directory;
log write (concat "Get files from '" [CONF_DIR_SOURCE] "/" [CONF_SOURCE_FILTER] "'.") OK null null;
json load "valid_files" (file dir_list_regex [CONF_DIR_SOURCE] [CONF_SOURCE_FILTER] true false);
json parse_array "valid_files" "/" "[filename]" {
file copy_file (concat [CONF_DIR_SOURCE] "/" [filename]) (concat "home/" [FLOW_PID] "/" [filename]);
log write (concat "Get file 'home/" [FLOW_PID] "/" [filename] "'.") OK null null;
};
# Delete source file;
json parse_array "valid_files" "/" "[filename]" {
file delete (concat [CONF_DIR_SOURCE] "/" [filename]);
log write (concat "Remove source file '" [CONF_DIR_SOURCE] "/" [filename] "'.") OK null null;
};
} {
#Step 2 is not valid;
stack flow_step [FLOW_PID] 2 "extract_KO";
# Give the error to the stack and stop the process;
exception (1) ([global_err]);
} "[global_err]";
log write (concat (json count "valid_files" /) " file(s) to transform.") OK null null;
if (> (json count "valid_files" /) 0) {
#Step 2 is valid;
stack flow_step [FLOW_PID] 2 (concat "extract_ok_" (json count "valid_files" /) "_files");
json parse_array "valid_files" "/" "[filename]" {
stack (date now) "folder1.folder2.folder3.step_3_transform.exe" "[FLOW_PID]" [FLOW_PID] "[filename]" [filename];
};
} {
#Step 2 is valid;
stack flow_step [FLOW_PID] 2 "extract_ok_zero_file";
# No file into the directory;
file delete (concat "home/" [FLOW_PID]);
};
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_2_extract.exe" "0001_folder1_folder2_folder3")) {
group grant script "folder1.folder2.folder3.step_2_extract.exe" "0001_folder1_folder2_folder3";
};
script merge "folder1.folder2.folder3.step_3_transform.exe" true 1
(param
(var "[FLOW_PID]" {true} "The flow PID" is_null:true is_empty:true "1")
(var "[filename]" {true} "The filename" is_null:true is_empty:true "file.xxx")
)
"Transform the file"
{
# CONFIGURATION ;
-> "[CONF_DESTINATION_NEW_FILENAME]" (concat "new_filename_" (date systimestamp_min) ".xlsx");
-> "[CONF_DESTINATION_SHEETNAME]" "sheet1";
# HANDLE ;
try {
# Mark the flow as step 3;
stack flow_step [FLOW_PID] 3 "transform...";
excelx load empty "flow";
excelx sheet add "flow" [CONF_DESTINATION_SHEETNAME];
log write (concat "Build the file 'home/" [FLOW_PID] "/" [CONF_DESTINATION_NEW_FILENAME] "' from 'home/" [FLOW_PID] "/" [filename] "'...") OK null null;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 0 "ColNameA" STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 1 "ColNameB" STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 0 2 "ColNameC" STR;
xml load "flow_source" (file load (concat "home/" [FLOW_PID] "/" [filename]));
# BEGIN METHOD 1;
-> "[T_A]" (xml select text "flow_source" "/data/A");
-> "[T_B]" (xml select text "flow_source" "/data/B");
-> "[T_C]" (xml select text "flow_source" "/data/C");
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 0 [T_A] STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 1 [T_B] STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] 1 2 [T_C] STR;
# END METHOD 1;
# BEGIN METHOD 2;
-> "[index]" 1;
-> "[nb_rows]" (xml count "flow_source" "/data/rows/item");
for (-> "[i_xml]" 1) (<= [i_xml] [nb_rows]) (++ "[i_xml]") {
-> "[T_A]" (xml select text "flow_source" (concat "/data/rows/item[" [i_xml] "]/A"));
-> "[T_B]" (xml select text "flow_source" (concat "/data/rows/item[" [i_xml] "]/B"));
-> "[T_C]" (xml select text "flow_source" (concat "/data/rows/item[" [i_xml] "]/C"));
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 0 [T_A] STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 1 [T_B] STR;
excelx cell set "flow" [CONF_DESTINATION_SHEETNAME] [index] 2 [T_C] STR;
++ "[index]";
};
# END METHOD 2;
excelx save "flow" (concat "home/" [FLOW_PID] "/" [CONF_DESTINATION_NEW_FILENAME]);
excelx close "flow";
log write (concat "Builded.") OK null null;
#Step 3 is valid;
stack flow_step [FLOW_PID] 3 "transform_ok";
stack (date now) "folder1.folder2.folder3.step_4_destination_connect.exe" "[FLOW_PID]" [FLOW_PID] "[filename]" [CONF_DESTINATION_NEW_FILENAME];
} {
try {excelx close "flow";} {} "[err]";
#Step 3 is not valid;
stack flow_step [FLOW_PID] 3 "transform_KO";
# Give the error to the stack and stop the process;
exception (1) ([global_err]);
} "[global_err]";
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_3_transform.exe" "0001_folder1_folder2_folder3")) {
group grant script "folder1.folder2.folder3.step_3_transform.exe" "0001_folder1_folder2_folder3";
};
script merge "folder1.folder2.folder3.step_4_destination_connect.exe" true 5
(param
(var "[FLOW_PID]" {true} "The flow PID" is_null:true is_empty:true "1")
(var "[filename]" {true} "The filename" is_null:true is_empty:true "file.xxx")
)
"Connect to the destination"
{
include "folder1.folder2.folder3.step_5_load.exe";
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_4_destination_connect.exe" "0001_folder1_folder2_folder3")) {
group grant script "folder1.folder2.folder3.step_4_destination_connect.exe" "0001_folder1_folder2_folder3";
};
script merge "folder1.folder2.folder3.step_5_load.exe" true 1
(param
)
"Load file to the destination"
{
# CONFIGURATION ;
-> "[CONF_DESTINATION_CM]" "demo_cm_cifs";
-> "[CONF_DIR_DESTINATION]" "C://remote/dir";
# HANDLE ;
try {
# Mark the flow as step 5;
stack flow_step [FLOW_PID] 5 "load...";
log write (concat "Send '" (concat "home/" [FLOW_PID] "/" [filename]) "' to '" (concat [CONF_DIR_DESTINATION] "/" [filename]) "' ...") OK null null;
# Send file into the destination directory;
cifs put (concat "home/" [FLOW_PID] "/" [filename]) (concat [CONF_DIR_DESTINATION] "/" [filename]) {cm get [CONF_DESTINATION_CM];};
log write "Sended." OK null null;
#Step 5 is valid;
stack flow_step [FLOW_PID] 5 "load_ok";
} {
#Step 5 is not valid;
stack flow_step [FLOW_PID] 5 "load_KO";
# Give the error to the stack and stop the process;
exception (1) ([global_err]);
} "[global_err]";
} "Return nothing";
if (not (group is granted script "folder1.folder2.folder3.step_5_load.exe" "0001_folder1_folder2_folder3")) {
group grant script "folder1.folder2.folder3.step_5_load.exe" "0001_folder1_folder2_folder3";
};