Java Snippet (simple)

This node allows you to execute arbitrary java code to create a new column or to replace an existing one. Enter the java code in the text area. To address certain input values of the table use $name_of_col$ (a double click on the elements in the column list will insert a the place holder at the current cursor position). For instance, the sum of the first two columns is $col_1$ + $col_2$ (replace col_1 and col_2 by the name of the columns). Please also specify the return type with one of the radio buttons. You can also use more complex statements such as if-else switches. For instance, to bin a numeric column you could enter code as follows and check the String return type:

  String bin;
  if ($col_1$ < 0.5) {
    bin  = "Very small";
  } else if ($col_1$ < 2.0) {
    bin  = "Small";
  } else if ($col_1$ < 10.0) {
    bin  = "Medium";
  } else if ($col_1$ < 20) {
    bin  = "Large";
  } else {
    bin  = "Very Large";
  }
  return bin;

You can also use static methods of java utility classes, e.g. to get a column with random values, simply enter Math.random() and check the double return type.

It is also possible to use external java libraries (that is, libraries that are not part of java boot classpath). In order to include such external jar or zip files, add their location in the "Additional Libraries" tab using the control buttons.

Exceptions in the snippet are caught by the framework and will result in a missing value for the current row. If the snippet throws an exception of class Abort (an inner class that is available to each snippet without further import statements), the entire execution will halt, using the exception message as error that is presented to the user.

Options

Column List
The list contains the column that are available in the input table. Double click any of the column names to insert the respective field in the expression editor. It will replace your current selection or simply insert at the current caret position.
Flow Variable List
The list contains the flow variables that are currently available at the node input. Double clicking any of the entries will insert the respective identifier at the current cursor position (replacing the selection, if any).
Global Variable Declaration
Define global variables here. These variables are available during the execution and they are also modifiable. Names starting with '_' (underscore) are discouraged due to potential name conflicts with internally used variables.
Method Body
Enter your java code here (the entire source will be wrapped in a method). You can add the name of a column by double-clicking its name in the left list. Note that numeric (double-compatible) columns will be handled as Double, int-compatible columns as Integer and any other column type as String. If the input type represents a collection type, it will be available as (typed) array in the snippet.
Insert Missing As Null
If selected, missing values in the input table will be represented by null in the expression. It is your responsibility to check for null in this case (the fields are all real classes, so even int and double are represented as their respective class Integer and Double). If unselected, a missing value in the relevant input cell will immediately produce a missing output cell (snippet not called).
Compile on close
If unchecked, the syntax test is skipped when the settings are applied to the model, allowing the dialog to close even if there are syntax errors encountered. This enables the user to temporarily close the dialog, investigate the input table and then return to the almost-complete-and-running snippet code in the dialog. The node will not be executable (it will be red) if the code contains syntax errors. This option is only for convenience, it does not have any effect on the output of this node.
Return type
Specify here the resulting type of your code snippet. Unfortunately KNIME cannot determine what will be the correct return type - so you need to assist here.
Array Return
If the result is an array, check this box. It will be represented by a KNIME collection cell.
Replace/Append
Specify here if you want the result to be put into a separate or existing column.

Input Ports

Icon
Input table.

Output Ports

Icon
Input table with an additional calculated column or one column replaced.

Popular Successors

Views

This node has no views

Workflows

Links

Developers

You want to see the source code for this node? Click the following button and we’ll use our super-powers to find it for you.