creating kettle variables in javascript

3,429 views
Skip to first unread message

Brassrat

unread,
Jul 27, 2007, 10:21:49 AM7/27/07
to kettle-developers
The following javascript works in 2.5.
This will no longer work in 3.0 as the kettle variable implementation
has changed, so, how does one create kettle variables from javascript
in 3.0?

example javascript:
var trans_Status= CONTINUE_TRANSFORMATION;
var zz=Packages.be.ibridge.kettle.core.KettleVariables.getInstance();
var nameStr = "run.root";
var valueStr = "/test"
zz.setVariable(nameStr,valueStr);

-jay

Sven Boden

unread,
Jul 27, 2007, 1:54:15 PM7/27/07
to kettle-developers

For the moment not, wait a day ;-)

Regards,
Sven

Sven Boden

unread,
Jul 28, 2007, 6:59:40 AM7/28/07
to kettle-developers

Since variables are moderately changed ;-) that snippet of code won't
work anymore. It's also not really possible to make it backwards
compatible, this will be the case for any use of "Package.... " when
the implementation of the called functionality changes (inside or
outside of Kettle).

What I did add are 2 new functions in the modified javascript step:

setVariable(key, value, type) and getVariable(key, default)

The type is either "s" system-wide, "r" upto the root, "p" upto the
parent, "g" up to the grandparent. The snippet in the original post
has to become something as:

var trans_Status= CONTINUE_TRANSFORMATION;
var zz=Packages.be.ibridge.kettle.core.KettleVariables.getInstance();
var nameStr = "run.root";
var valueStr = "/test"

setVariable(nameStr,valueStr, "r");

Jay Goldman

unread,
Jul 28, 2007, 11:33:37 AM7/28/07
to kettle-d...@googlegroups.com
this sounds good ... I didn't like using the Packages... method to
access kettle related state.

thanks,
jay

Brassrat

unread,
Jul 28, 2007, 1:58:58 PM7/28/07
to kettle-developers
btw - would it be acceptable if i added a similar function to 2.5.1 so
i can use the same scripts for the production work i am doing?

I am currently working on a deployment over the next month or so, so i
can't wait for 3.0 to be ready.
I have been testing my scripts under 3.0 to help catch any
inconsistencies between 2.5.1 and 3.0.

it would be nice to not have to start managing different repositories,
etc.

comments, please
-jay

Sven Boden

unread,
Jul 28, 2007, 5:08:09 PM7/28/07
to kettle-developers

Personally no problem with it... although I think that you will find
that setting the variables may give you some problems if you run using
subjobs.

Regards,
Sven

Bruce

unread,
Sep 12, 2007, 11:17:47 AM9/12/07
to kettle-developers
Should this be available in 3.0 M2? I tried the sample in this thread
from Sven, but throws an error when I click the "Test script".
The error is as follows:

General error executing script:
org.mozilla.javascript.EvaluatorException:
org.mozilla.javascript.EvaluatorException: Cannot convert
org.pentaho.di.ui.trans.steps.scriptvalues_mod.ScriptValuesModDialog
$ScriptValuesModDummy@1b8b3bb to
org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod (script
#5) (script #5)

Code:


var nameStr = "run.root";
var valueStr = "/test"
setVariable(nameStr,valueStr, "r");

Sven Boden

unread,
Sep 12, 2007, 6:17:03 PM9/12/07
to kettle-developers

Running works, test doesn't. So run, don't test for now. Test is
always a bit tricky as some fields will be defaulted.

Regards,
Sven

Matt Casters

unread,
Sep 13, 2007, 1:40:41 AM9/13/07
to kettle-d...@googlegroups.com

How about presenting the user with a dialog to enter the values for the used
fields in the script(s)? You can use the current defaults.
That way the test would be more accurate.

Matt


--
Matt
____________________________________________
Matt Casters
Pentaho, Open Source Business Intelligence
http://www.pentaho.org -- mcas...@pentaho.org
Tel. +32 (0) 486 97 29 37

Bruce Linn

unread,
Sep 13, 2007, 9:54:15 AM9/13/07
to kettle-d...@googlegroups.com
Thanks!

Is there also a replacement function to handle this example?

var value = Packages.be.ibridge.kettle.core.value.Value.getInstance();
value.setName("fielda");
value.setValue("valuea");
row.addValue(value);

-Bruce

Matt Casters

unread,
Sep 13, 2007, 11:18:09 AM9/13/07
to kettle-d...@googlegroups.com

There is no replacement code possible.
That is because it is highly inefficient and even in verison 2.x undocumented.

this code:

var value = "valuea";

Will do the trick just as well.
The difference being that the change in metadata only has to be calculated
once, not for every row.

Because of the changes we made in 3.0, the Value object no longer exists.

HTH,

Matt

Bruce Linn

unread,
Sep 13, 2007, 12:43:30 PM9/13/07
to kettle-d...@googlegroups.com
Just to clarify, are you saying there is no way to add a field to the data stream from within the java script node (row.addValue(value)) in 3.0?  That is the functionality that I am attempting to recreate in 3.0.

If it is not available, what other options would you recommend?  Java script fit well into our application of PDI, as we could dynamically generate the java script to create a dynamic set of fields on our data stream.

Bruce
Decision Intelligence, Inc.

Matt Casters

unread,
Sep 13, 2007, 1:58:03 PM9/13/07
to kettle-d...@googlegroups.com

Hi Bruce,

Both in version 2 or 3 you can add values to the rows simply by specifying the
variables you created in your scripts in the Fields section at the bottom of
the "Script values" dialog.

For example, you can create a variable as such:

var value = "valueA";

Adding that value to the row is simply a case of specifying that field as in
the attached screenshot.

BTW, the decision to not add values one by one to the rows is one of the
things that makes version 3 fly compared to version 2. If you are processing
rows, you should not be adding the same metadata to the values in every row.
By definition, all the rows are the same. That de-coupling of data and
metadata is why you can't trick the JS step anymore :-)

Now, all that being said, it might very well be possible that you need
functionality on top of the defaults. I appreciate that DII might be doing
some very complex things. In that case, I would much rather see those things
wrapped in a JS function somehow. Let me know your requirements and I'll
help out our data mining friends as much as possible.

All the best,

Matt

js-adding-values.png

Bruce Linn

unread,
Sep 13, 2007, 2:24:09 PM9/13/07
to kettle-d...@googlegroups.com

I think this should work just fine (actually better) in our case.  I didn't understand that I could create a new variable in the lower section and then reference it in the script.  I thought this section was exclusively for modifying meta data of existing fields.

Thanks again for your help,

Bruce

unread,
Sep 14, 2007, 2:44:15 PM9/14/07
to kettle-developers
While refactoring the create, I realized that there are 2 other pieces
of java script I need your assistance on:

Renaming a field via javascript:
The code I had still "tests" ok in the node, but does not actually
rename the column.
<old field name>.setName("<new field name>");

Removing a field via javascript:
The code I had still "tests" ok in the node, but does not actually
remove the column.
row.removeValue("<field name>");

Is it possible to still implement this functionality through the java
script node? I tried using the bottom section of the screen to do the
rename, but I couldn't get that to work (am I missing the obvious?).

Thanks again,
Bruce
Decision Intelligence, Inc.

On Sep 13, 1:24 pm, "Bruce Linn" <bwlst...@gmail.com> wrote:
> I think this should work just fine (actually better) in our case. I didn't
> understand that I could create a new variable in the lower section and then
> reference it in the script. I thought this section was exclusively for
> modifying meta data of existing fields.
>
> Thanks again for your help,
> Bruce
> Decision Intelligence, Inc.
>

> > ? That


> > > is the functionality that I am attempting to recreate in 3.0.
>
> > > If it is not available, what other options would you recommend? Java
> > > script fit well into our application of PDI, as we could dynamically
> > > generate the java script to create a dynamic set of fields on our data
> > > stream.
>
> > > Bruce
> > > Decision Intelligence, Inc.
>

> > > On 9/13/07, Matt Casters <mcast...@pentaho.org> wrote:
> > > > There is no replacement code possible.
> > > > That is because it is highly inefficient and even in verison
> > > > 2.xundocumented.
>
> > > > this code:
>
> > > > var value = "valuea";
>
> > > > Will do the trick just as well.
> > > > The difference being that the change in metadata only has to be
> > > > calculated once, not for every row.
>
> > > > Because of the changes we made in 3.0, the Value object no longer
> > exists.
>
> > > > HTH,
>
> > > > Matt
>
> > > > On Thursday 13 September 2007 15:54:15 Bruce Linn wrote:
> > > > > Thanks!
>
> > > > > Is there also a replacement function to handle this example?
>
> > > > > var value = Packages.be.ibridge.kettle.core.value.Value.getInstance
> > ();
> > > > > value.setName("fielda");
> > > > > value.setValue("valuea");
> > > > > row.addValue(value);
>
> > > > > -Bruce
>

> > > > > On 9/12/07, Sven Boden <list...@pandora.be> wrote:
> > > > > > Running works, test doesn't. So run, don't test for now. Test is
> > > > > > always a bit tricky as some fields will be defaulted.
>
> > > > > > Regards,
> > > > > > Sven
>
> > > > > > On Jul 28, 12:59 pm, Sven Boden <list...@pandora.be> wrote:
> > > > > > > Since variables are moderately changed ;-) that snippet of code
>
> > > > won't
>
> > > > > > > work anymore. It's also not really possible to make it backwards
> > > > > > > compatible, this will be the case for any use of "Package.... "
> > > > > > > when the implementation of the called functionality changes
> > (inside
> > > > > > > or outside of Kettle).
>
> > > > > > > What I did add are 2 new functions in the modified javascript
> > step:
>
> > > > > > > setVariable(key, value, type) and getVariable(key, default)
>
> > > > > > > The type is either "s" system-wide, "r" upto the root, "p" upto
> > the
> > > > > > > parent, "g" up to the grandparent. The snippet in the original
> > post
> > > > > > > has to become something as:
>
> > > > > > > var trans_Status= CONTINUE_TRANSFORMATION;

> > > >http://www.pentaho.org -- mcast...@pentaho.org


> > > > Tel. +32 (0) 486 97 29 37
>
> > --
> > Matt
> > ____________________________________________
> > Matt Casters
> > Pentaho, Open Source Business Intelligence

> >http://www.pentaho.org -- mcast...@pentaho.org

Matt Casters

unread,
Sep 14, 2007, 2:50:24 PM9/14/07
to kettle-d...@googlegroups.com

Hi Bruce,

That doesn't work anymore either for the same reasons as before.

But... we have a dedicated step called "Select Values" that performs the trick
faster and better. The "Metadata" tab allows you to rename fields in a clean
and fast way.

IMHO, the isolation of functionality is good thing. Let's take for example a
situation where you would rename a fieldname in some rows and not in others.
That sort of thing is mayhem waiting to happen.

All the best,

Matt

http://www.pentaho.org -- mcas...@pentaho.org

Reply all
Reply to author
Forward
0 new messages