Pentaho/PDI: Increment a value automatically by one if a load-job (within a metajob) fails - pentaho

in PDI I've got the following structure
0_Metajob
1_Load_1
1_Load_2
1_SimpleEvaluation
1_Mail
As of now
1_Load_1 and 1_Load_2 are independent of each other. The second one will run, irrespective of the success of the first one. That is okay, I want it that way!
Issue
I want to have a counter that is incremented by one every time one of the single loads fails, i.e. in my example the counter can take the values 0, 1 or 2.
What do I need it for? Customer will receive a mail at the end of the metajob. The aforementioned value determines the subject of the mail, i.e. 0=everything fine, 1=so-so, 2=load totally failed!
Why not mailing within every single the Load-Job? I do that but without attaching the log-file because it is usually non-finished. Therefore the log-file is mailed with the mail that is sent when the Metajob is finished.
Tried
"Set a variable". Thought I can simply increment it with adding a one in the value field, i.e. "${VariableName}+1". Of course, this step is implementened within a fail path of each Load-Job.
However, it didn't work.
Would anyone mind helping me? I would appreciate that!

Set Variable doesn't do calculations, you'll need a Javascript step for that.
Fortunately, variables can be also be set within the Javascript step. This bit of code should go into each of the steps you put in place of the Set Variable steps:
var i = parseInt(parent_job.getVariable("Counter"),0);
i = i + 1;
parent_job.setVariable("Counter",i);
true;
This bit of code gets the variable "Counter" from the parent job and converts it to int, since all Pentaho variables are strings. Then it increments it and sets the job variable again. The "true" at the end is to ensure that the javascript step reports success to the main job.
IMPORTANT: This works roughly as you would expect in a Job. It will NOT in a transformation!

Related

Setting up multiple schedules for a ResourcePool

I am doing a simulation of a production line, which is built through Excel.
Now there is one, which needs free setting of worker shifts for each machine, three shifts in total.
My idea is to create three different Schedules, representing each of the three shifts, and then in the ResourcePool, use the If statement to set up the use of each shift. As shown in the picture enter image description here
But it didn't work. If it is possable, Could you please tell me if there is something wrong with the Java statement or the idea is wrong.
If possible, could you please tell me how to set multiple schedules in ResourcePool?
Additions to the question:
The model is to do the evaluation of the production system and the shift of the workers is an important evaluation parameter. What I want to do is to enter the shift of the workers directly in the Excel sheet and ResourcePool recognizes and uses the corresponding Schedule.
I tried to generate the corresponding Schedule by code, but the Schedule of Resourcepool cannot be set dynamically. So I would like to try to manually input multiple Schedules into the model and then set the corresponding Schedule in ResourcePool by If statement like the image.
Thanks in advance
The first thing you need to know is that the location where you placed the code is a static parameter, thus it is only evaluated once, when the object is created and not checked continuously.
This is indicated by the little popup when you hover over the button that change the entry field from code to value
If it was dynamic it would state Dynamic Value instead of Static Value
Secondly, inside that field you must use a ternary operator, not an if statement, so that the result of the formula is a scheduled object, else you will get a "misplaced construct(s)" error
If you changed the code to
v_Shift == "Shift1" ?
s_Shift1 : v_Shift == "Shift2" ? s_Shift2 : s_Shift3
It will work BUT:
It will only be evaluated when the object is created and not again
Rather not use == on Strings, always use .equals(), on Strings it might work, and sometimes it might not. You can do some research as to why ;-)
Solution: You will have to use a function to change the schedule of the resource pool. Call this function whenever the v_Shift variable changes

Can you print variables that are dynamically generated?

I am trying to setup a program that takes user input for database ddl generation. I have it working to the point where it can ask a user for the name of the table, the number of columns and any attributes that might be needed. The problem comes when I try to print a string that includes the variables used for the column names. Due to trying to let users have as many columns as they want I used variables similar to this newvar(number that increases every time you enter a column name). This works fine and I can get the values if i do send %newvar1% but it doesn't work to do send newvar%increasing number%. I need to know if this is possible or if I'm just missing something obvious. Also I don't have the code with me but I can post it once I get back to my main computer.
I have tried quite a few things like, send %newvar%%number%, send newvar%number%, othervar = newvar%number% send %othervar%.
I'll show some once I have access to it in about 2 hours.
I expect to be able to output names for increasing variables using an ever increasing number. Class is starting I'll clarify some things later.
You can use a lone percent % beginning the first argument for the send command to achieve what you want. This will make everything after it to be evaluated (up to the next comma). Here is an example:
f1::
newvar1 := "This " , newvar2 := "is just a " , newvar3 := "test."
Loop , 3
Send , % newvar%A_Index%
Return
See: https://www.autohotkey.com/docs/Language.htm#-expression

Use Pentaho Variable to Dynamically name EXCEL file

I am trying to dynamically name an excel file after processing it for archiving purposes.
If I process Logistics.xlsx I want to save it as U:\Archive\${varDP}.xlsx
Resulting file name U:\Archive\20190709.xlsx
I have tried Get system variable to get the date, This works fine. I have created the field (DateProcessed). However, I am unable to Set variables varDP to DateProcessed.
Thank you
You cannot set and use a variable in the same transformation. If you want to use a variable you should have a job with two transformations: first transformation gets the date and sets the variable; second transformation can then use the variable.
The main reason for that is that all steps initialise at the same time. Therefore, when the variable is read by the step that is using it, it's probably not set yet.
For these cases of Variables usage and passing parameters, i've been forwarding this previous answer, it has a link to another answer of mine where i go step by step of how to pass parameters to another Transformation without 'Set Variables', and in the linked answerm i have sent a downloadable example.

SSIS Logging of OnVariableValueChanged with variable value

I am trying to log all changes in variable values in a SSIS generated with BIML.
I managed to create an event handler that writes everytime a variable changes its value.
When I log I use a parameter whose value I set to "System.VariableValue". I pass this parameter (togheter with variableName and PackageName) to a StoredProc and i write in a log table.
My problem is that often (but NOT always) it seems the parameter does not has any value. I see a new line in the DB Log table so this means the evnt is correctly raised and handled BUT it seems the parameter is empty.
The strangest thing is that sometimes values are logged correctly but not always, not for the same variables, not for the same packages, rather, in a quite random fashion.
Could it be a problem the fact that several variables could change value almost at the same time (some contention on the DB) ? I doubt it, because the row itself gets written on the DB. I even tried to write, as a value, something like 'New value = ' + ? that is, appending the parameter value to a fixed string. The fixed part gets written correctly but.. no value.
The name of the variable that changed value is always written correctly.
Any idea what this could be due to?
As a workaround I tried to use the ready-made logging facility of SSIS but in this case in the message column of the SYSSSISLOG table i can just read the name of the variable that changed, not its new value.
thankx
You could use Event Handler to do that. Go to the Variables page, go to Variable Grid Options, check Raise event when variable value changes, and there should be one more option appear for those variables, which is Raise Change Event, default to False, change to True for those variables that you need to track the changes (log). And put a logging task into Event handler
UPDATE
the new line could be the value of parameter has been reset, and that value, most likely equal to blank or whitespace, but still, that is recognized as value change.
if you are not very sure when that happened, you could set a Breakpoint to certain task and add watch window to see how the value change or whether the value will hit blank in the middle of your process

Getting KeyError when trying to create multiple variables in for loop ("post0", "post1", etc.)

Very new to python - just started actually using it yesterday. I'm running a for loop that scans a text file and copies specific parts of it into variables that will then be put into a class "post". I want to create a new post at the bottom of the loop, named "post0", "post1", and so on, corresponding with the number of times the for loop has been run. This is what I'm trying to use:
postname = globals()['post%s' % s]
And I currently am trying to have it print the name of the post every time it creates one with a simple print(postname). 's' is the variable the for loop runs off of, if that makes sense. It starts at 0 and runs up to the number of lines in the text file, currently 424.
When I run the code, it returns "KeyError: post0". What am I doing wrong?
Also, reading around here it seems that creating variables this way is bad practice. If there is a more efficient way to do it I'd be happy to try that instead, but I'd also like to know how to make this method work just so I understand the concept. Thanks.
Edit: Problem solved! See my answer below.
I created a list postlist = [] outside of the loop, then inside the loop I append the list with a "post_" where "_" is the 's' variable. Looks like this:
postlist.append('post%s' % s)
Python is cool!