Hitachi Vantara Pentaho Community Forums
Results 1 to 8 of 8

Thread: Java expression for multivalue case

  1. #1

    Default Java expression for multivalue case

    The following works within a Java expression:

    var result2 = null;
    if (PROID == 1006) {result2 = 'DUIZENDZES';}
    else if (PROID == 1009) {result2 = 'DUIZENDNEGEN';}
    else if (PROID == 1011) {result2 = 'DUIZENDELF';}
    else if (PROID == 1012) {result2 = 'DUIZENDTWAALF';}
    else if (PROID == 1015) {result2 = 'DUIZENDVIJFTIEN';}
    else {result2 = 'unknown';}

    whereas:

    var result = null;
    switch (PROID) {
    case 1006: result = 'DUIZENDZES';
    case 1009: result = 'DUIZENDNEGEN';
    case 1011: result = 'DUIZENDELF';
    case 1012: result = 'DUIZENDTWAALF';
    case 1015: result = 'DUIZENDVIJFTIEN';
    default: result = 'unknown';
    }

    simply returns only the default.

    I was looking for the most compact way to use CASE WHEN expression, cause the Formula step gets nasty with a lot of values to evaluate

    Using switch is a more compact way, but somehow does not work..

    Anyone an idea why this does not work out?

  2. #2
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    Just asking: You are sure you can't use Stream Lookup for this?
    So long, and thanks for all the fish.

  3. #3

    Default

    Quote Originally Posted by marabu View Post
    Just asking: You are sure you can't use Stream Lookup for this?
    The thought crossed my mind to use lookups which is probably more maintainable and quicker, but I probably need hundreds of CASE expressions to translate SPSS code to something within Kettle. 90% of the SPSS code is about using CASE logic with the nasty SPSS syntax, so recoding with a compact syntax seems the best alternative. The formula step simply is to awkward to use for large CASE expressions.

    I'll take a look into the Stream solution..

    Thanks!

  4. #4
    Join Date
    Nov 2008
    Posts
    777

    Default

    Quote Originally Posted by blom0344 View Post
    whereas:

    var result = null;
    switch (PROID) {
    case 1006: result = 'DUIZENDZES';
    case 1009: result = 'DUIZENDNEGEN';
    case 1011: result = 'DUIZENDELF';
    case 1012: result = 'DUIZENDTWAALF';
    case 1015: result = 'DUIZENDVIJFTIEN';
    default: result = 'unknown';
    }

    simply returns only the default.
    You need a break; statement at the end of each case in order for it to work properly. http://www.w3schools.com/js/js_switch.asp
    pdi-ce-4.4.0-stable
    Java 1.7 (64 bit)
    MySQL 5.6 (64 bit)
    Windows 7 (64 bit)

  5. #5

    Default

    Originally I started out with a textbook example using the break; for each case line. However using the break; or not always returns the default value. In other words, it does not seem to have any influence.

    The following script does work !!: (using compatibility mode)

    var resultx = null;
    var proid_var = PROID.getInteger();

    switch (proid_var)
    {
    case 1006:
    resultx = 'DUIZENDZES';
    break;
    case 1009:
    resultx = 'DUIZENDNEGEN';
    break;
    case 1011:
    resultx = 'DUIZENDELF';
    break;
    case 1012:
    resultx = 'DUIZENDTWAALF';
    break;
    case 1015:
    resultx = 'DUIZENDVIJFTIEN';
    break;
    default: resultx = 'unknown';
    }

    So ,explicitly defining a variable is necessary to work with switch, whereas the alternative use of CASE works by just refering to the ingoing variable
    Last edited by blom0344; 10-28-2013 at 10:21 AM.

  6. #6
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    A couple of recommendations:

    • Don't say Java when you think JavaScript (compare your thread title)
    • Don't think JavaScript when coding in Rhino (PROID is a java object)
    • Don't use Compatibility Mode in modern transformations (you're not writing for Kettle 2.5 after all)
    • Don't use scripting for what other steps can do (Kettle is all about visual programming)


    If you want to translate discrete values, I suggest you use Value Mapper (internal dictionary) or Stream Lookup (external dictionary).
    If you are flirting with MJSV to provide a number-to-text translation for any given number, UDJC might be more scalable.

    BTW: Here is Rhino code that doesn't care about ancient Kettle:

    Code:
    switch (1*PROID) {
        case 1006: 
            resultx = 'DUIZENDZES'; 
            break;
        case 1009: 
            resultx = 'DUIZENDNEGEN'; 
            break;
        case 1011: 
            resultx = 'DUIZENDELF'; 
            break;
        case 1012: 
            resultx = 'DUIZENDTWAALF'; 
            break;
        case 1015: 
            resultx = 'DUIZENDVIJFTIEN'; 
            break;
        default: 
            resultx = 'unknown';
    }
    So long, and thanks for all the fish.

  7. #7

    Default

    Sorry for being a tad lazy about Java/Java script. All code examples are directly derived from Java script examples. Your reference to Rhino does not ring a bell, but I will make sure to check it out.

    Both initial trials were inspired by the same site about Java script, which led me to believe I was comparing apples with apples

    The background to the questions is searching for a neat way to replace masses of SPSS code. I would love to skip using script, but SPSS code (95%) is very easy to convert to Javascript. I'd rather script it AND keep things within my ETL domain than having to write stored procedures in the database.

    I have to do a little more than simply recoding distinct integers to string values , hence the attempt to write Javascript. Roughly 20% of the SPSS code can also be dealt with by defining master data in the database and using lookups

  8. #8
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    Rhino is the embedded JavaScript engine written in Java.
    When using a field name in MJSV you actually are dealing with a Java object.
    That's why your switch statement did fail to behave as you expected.
    So long, and thanks for all the fish.

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.