Hitachi Vantara Pentaho Community Forums
Results 1 to 7 of 7

Thread: Modified Java Script Step

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Modified Java Script Step

    I'm trying to convert a string in the format of "yyyymmdd" to a date using the following:


    /* ******************************************************* */
    /* Purpose= get string date DOT in yyyymmdd format
    and convert to a real date BUS_DATE */


    /* Get string input field and parse */
    var dot = DOT.getString();
    var my_year = substr(dot,1,4);
    var my_mth = substr(dot,5,6);
    var my_day = substr(dot,7,8);

    /* Put the pieces back together as BUS_DATE */

    var BUS_DATE.getDate() = str2date(my_year,my_mth,my_day);
    /* ******************************************************* */

    Getting the following error when I test the script:


    Couldn't execute this script! Error on position (13:14) :
    missing ; before statement (script#13)


    What am I doing wrong? Also, where can I learn more about using java script? Is there more documentation
    at pentaho about properly building java script steps?

    Thanks,
    David

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    this part

    Code:
    var BUS_DATE.getDate() = str2date(my_year,my_mth,my_day);
    does not fly of course... your left hand is not something assignable (javascript expects ; after the getDate().

    Regards,
    Sven

  3. #3
    Join Date
    May 2006
    Posts
    4,882

    Default

    Attached an easier example ... you could also have select values for it probably.

    For the rest javascript is general javascript (rhino):

    - When writing new transformations, use compatibility OFF
    - make new columns by putting them in the table at the bottom of the dialog
    - you can't change existing fields in versions <=3.1. As of 3.2 it should work.
    - there's a difference between java string and javascript string which bites a lot of people in the a**.

    Regards,
    Sven
    Attached Files Attached Files

  4. #4

    Default

    Ah, good thing not everybody is as lazy as me : )

  5. #5

    Smile

    Many thanks to sboden and colorfool. I tried both the java script method and the select method and got both to work. Opted for the select values method because already had one in the transformation and this made one less step.

    I'd still like to learn more about using the Modified Java Script step as I see much flexibility. Any tutorials on pentaho regarding the use of Modified Java Script?

    Thanks again to both of you. This really is a great community!

    David

  6. #6
    Join Date
    May 2006
    Posts
    4,882

    Default

    You have small pieces in the wiki on modified javascript (http://wiki.pentaho.com/display/EAI/...a+Script+Value), there are some sample scripts in the samples directory using javascript.

    But I don't think we want to make a complete "Pentaho" javascript tutorial, nor do we have the time I think... it's just regular (embedded) javascript and once you get to know the few quirks it's not that bad.

    If you feel like upgrading the wiki information on javascript, you can do that if you want to

    Regards,
    Sven

  7. #7

    Default

    This line obviously is wrong:

    var BUS_DATE.getDate() = str2date(my_year,my_mth,my_day);

    You are not trying to get a date but to set a date... I am lazy to test right now but it would be something along the lines of either

    var BUS_DATE = str2date(my_year,my_mth,my_day);

    or

    var BUS_DATE.setDate(str2date(my_year,my_mth,my_day));

    depending on if you are using compatibility mode or not.

    Also I think you can reach the same goal using the Select Values step. Just say you want to change the meta data of the DOT column to type date and provide the format string. (I also didn't test it just now but I think it would work.)

    I like to use JavaScript, but as long as another step can give me the same result I usually try going with the ready made stuff. I always hope it might be faster.

    Cheers,

    Axel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.