Hitachi Vantara Pentaho Community Forums
Results 1 to 8 of 8

Thread: Java Script conditions loop

  1. #1

    Question Java Script conditions loop

    Kettle version: 2.3.0.
    Java Script version (Rhino) : Unknown

    I am using a Java script to identify 2 different patterns in input string. The problem is only one of the condition is executed and the other one doesn't. The conditions are checked using 'If ....else' loop.

    Here is what I tried

    If (true) --> print a string
    Else (false) --> print a different string

    The funny thing is whenever the condition gets executed only the 'IF' part works and the 'ELSE' part doesn't work - even if the condition is false.

    I thought this may be a limitation in Kettle 2.3.0 version of Java Script and wish to verify with the group.

    Thanks for any help,

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

    Default

    Well... willing to bet you're doing something wrong, like comparing strings with == e.g.

    Can you post the javascript code you're executing.

    Regards,
    Sven

  3. #3

    Default

    Here is the JavaScript code snippet.


    There are 2 types of input data separated by pipes and carots between values

    (1) ipod||600^^300
    (2) ipod||electronics

    If the data is of type (1), the main IF part should be executed and if the type is (2) ELSE part should be executed. (required output)

    var value_1 = "ipod||600^^300";
    var words, wordsvalue, words1,words2,words3,words4,words5; // list of words in data

    words = value_1.split("\\|"+"\\|");

    if (value_1.contains("\\|\\|\\^\\^")) //main IF part
    {
    for (var i= words.length -1; i >= 0; i--)
    words1 = "number_range"+'\"'+"="+'\"'+"\'"+words[i]+"\'";
    var patrn = java.util.regex.Pattern.compile("\\^"+"\\^");
    for (var i= 0; i < words.length; i++)
    {
    var mtch = patrn.matcher(words[i]);
    words2 = words[i].split("\\^");
    if (mtch.find())
    {
    for (var i= words2.length -1; i >= 0; i--)
    words3 = "\'"+words2[i]+"\'";
    for (var i= 0; i < words2.length ; i++)
    wordsvalue = "\'"+words2[i]+"\'";
    words4 = words1 + words3 + wordsvalue;
    }
    }
    }
    else
    {
    for (var i= words.length -1; i >= 0; i--)
    words1 = "string"+'\"'+"\="+'\"'+"\'"+words[i]+"\'";

    for (var i= 0; i < words.length ; i++)
    wordsvalue = "\'"+words[i]+"\'";
    words4 = words1+wordsvalue;
    }

    Result:

    When the above code is run in Kettle Java Script step, sometimes only the IF part is executed for both type of data (1) and (2). Sometimes only the ESLE part is executed but the IF part doesn't work at all.

    Thanks Sven for your time!

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

    Default

    It already blows up with me on "contains" ... doesn't work in 2.3.1, 2.5, and 3.0.

    Can you make a small transformation using a row generator to show your case, a transfomation that you run successfully (with in your case wrong results, but that does finish without aborting).

    Regards,
    Sven
    Last edited by sboden; 09-16-2007 at 06:44 AM.

  5. #5

    Default Transformation, output and log

    I have attached the transformation for your reference. In this Java Script I used 'indexOf' instead of 'contains', but it didn't work.

    The input text file has the following data:

    ref_name_val_1ref_name_val_2manufacturer||Applesize||60gig^^90manufacturer||Applesize||60gig^^78manufacturer||Microsoftlessthan||500^^56manufacturer||Applesize||60gig^^34

    and here is the text output when IF part is true

    value
    attribute_value_number_range"="'size''60gig''90'
    attribute_value_number_range"="'size''60gig''78'
    attribute_value_number_range"="'lessthan''500''56'
    attribute_value_number_range"="'size''60gig''34'

    and the output for ELSE part

    value
    undefined
    undefined
    undefined
    undefined

    and the Kettle log when the transformation was run

    2007/09/16 09:19:06 - Spoon - Transformation opened.
    2007/09/16 09:19:06 - Spoon - Launching transformation [QueryRowGenerate]...
    2007/09/16 09:19:06 - Spoon - Started the transformation execution.
    2007/09/16 09:19:06 - QueryRowGenerate - Dispatching started for transformation [QueryRowGenerate]
    2007/09/16 09:19:06 - QueryRowGenerate - Nr of arguments detected:10
    2007/09/16 09:19:06 - QueryRowGenerate - This is not a replay transformation
    2007/09/16 09:19:06 - QueryRowGenerate - This transformation can be replayed with replay date: 2007/09/16 09:19:06
    2007/09/16 09:19:06 - QueryRowGenerate - Initialising 3 steps...
    2007/09/16 09:19:06 - Text file output.0 - Opening output stream in default encoding
    2007/09/16 09:19:06 - Text file input.0 - Starting to run...
    2007/09/16 09:19:06 - Text file input.0 - Opening file: C:\input_data.tsv
    2007/09/16 09:19:06 - Java Script Value.0 - Starting to run...
    2007/09/16 09:19:06 - Text file output.0 - Starting to run...
    2007/09/16 09:19:06 - Text file input.0 - Finished processing (I=5, O=0, R=0, W=4, U=0, E=0
    2007/09/16 09:19:06 - Java Script Value.0 - Finished processing (I=0, O=0, R=4, W=4, U=0, E=0
    2007/09/16 09:19:06 - Text file output.0 - Finished processing (I=0, O=5, R=4, W=0, U=0, E=0
    2007/09/16 09:19:06 - Spoon - The transformation has finished!!

    Thanks for the help! I would appreciate if you can suggest any alternate to 'contains' or 'indexOf' to handle the string manipulation in 2.3.0.

    Thanks again....
    Attached Files Attached Files

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

    Default

    In the user guide you can find the functions you can execute on the values. I don't see anything similar to what you need, probably because no-one ever needed it.
    Can you raise a change request at http://jira.pentaho.org/browse/PDI

    Regards,
    Sven

  7. #7

    Default

    I have checked the user maual and found no funtion that supports this requirement.

    I am curious to know if there is any other way to accomplish this in Kettle 2.3.0. As I need a solution immediately I am looking at alternates before the change request comes into place.

    Let me rephrase the issue.

    I am able to split the string as required and this split is based on a condition. For me, the string is split properly but the condition is not working (IF - ELSE). Only the IF part works and the ELSE part doesn't work.

    I tried this logic in Java Script step.

    Is there any other way of doing it? any other combo of steps/tranformations...

    please let me know.

  8. #8
    Join Date
    Nov 1999
    Posts
    459

    Default

    Try to put the if clause together with the { in one line. Sometimes JavaScript assumes a new line as a new command.

    Example:
    if (value_1.contains("\\|\\|\\^\\^")) {

    instead of:
    if (value_1.contains("\\|\\|\\^\\^")) //main IF part
    {

    Nevertheless you are welcome to add the feature request for your use case.

    Hope this helps,
    Jens

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.