Java Snippet

This node allows you to execute arbitrary java code to create new columns or replace existing ones. Enter the java code in the text area.

Output Columns and Variables

Output columns are defined by creating and binding a java field for each column. The value assigned to this field will be written to the output.

This node allows adding or modifying flow variables in the same manner.

Additional libraries

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

Advanced users may want to make use of a library that is already present in the KNIME Analytics Platform installation as a bundle. Adding them to the list in the "Additional Bundles" tab will make them available to the java snippet's classpath.

Templates

You can define reusable templates with the "Create templates..." button. Templates are stored in the user's workspace by default and can be accessed via the "Templates" tab.

Options

Java Snippet

Contains the Java code editor to edit the snippet.

Column List
This list contains the columns that are available in the input table. Double click any of the entries to insert a respective field in the code editor and insert the field name at the current cursor position (replacing the selection, if any).
Flow Variable List
This list contains the flow variables that are currently available at the node input. Double click any of the entries to insert a respective field in the code editor and insert the field name at the current cursor position (replacing the selection, if any).
Snippet text area

Enter your java code here. You can access input columns or flow variables by defining them in the "Input" table of the dialog. Alternatively use the methods getCell(name, type) or getFlowVariable(name, type) where name is the name of the input column or the flow variable. You can use the following values for the type parameter.

  • tString : String (f)
  • tInt : Integer (f)
  • tDouble : Double (f)
  • tLong : Long
  • tBoolean : Boolean
  • tDate : java.util.Date
  • tCalendar : java.util.Calendar
  • tXML : org.w3c.document
(f) These Types can be used for flow variables.

Java Snippet Code

For columns:

  • getCell(name, type)* : Get value of a cell by column name
  • isType(name, type)* : Test for type by column name
  • isMissing(name)* : Check if missing by column name
  • columnExists(name)* : Check if column with given name exists
  • getColumnCount(index, type) : Number of columns in the input table
  • getColumnName(index) : Retrieve column name at index

For flow variables:

  • getFlowVariable(name, type) : Get value of a flow variable by name
  • getFlowVariables(type) : Get all flow variables by type. A name to value map is returned.
  • flowVariableExists(name) : Check if a flow variable with given name exists
  • isFlowVariableOfType(name, type) : Check if a flow variable is of type

* For these methods there is a variant that takes the column index instead of its name.

Note that the snippet allows defining custom global variables and imports.

Exceptions in the snippet are caught by the framework and will result in missing values for all output columns of the row being processed—unless there are flow variables defined by the snippet (in which case the execution will always fail). You can force the node to fail execution by throwing an exception of class Abort.

Code Assistance

CTRL+SPACE for auto completion will popup a list with all classes, methods and fields available at your current cursor location.

ENTER will generate missing import statements for a selected class.

Input
Define input fields for the snippet here. Every field will be populated with the data of the associated input column or flow variable during execution. For missing values in the input the field is set to null.
Output
Define output columns and flow variables here. Every row in the Output table defines a java field bound to an output column or flow variable. Values assigned to this field in the snippet will show up at the output.

Additional Libraries

This tab allows adding custom java library archives to make them available on the classpath of the java snippet.

Library List
List of libraries that will be present in the snippets classpath. Use the buttons below to add or remove libraries.
Add File(s)...
Add a .jar or .zip file from your system.
Add KNIME URL...
Add a KNIME protocol url to a .jar or .zip file. E.g. "knime://knime.workflow/example.jar".

Additional Bundles

This tab allows making installed bundles available on the classpath of the java snippet, allowing you to use classes e.g. from org.apache.commons, org.json, com.google.guava and more.
Once a bundle has been added, its version will be saved with the settings. Should the bundle not be present on an installation the workflow is then opened with, the node will produce an error and not run.
Should the bundle be present but in another version, it will suffice as long as the major version did not change and the minor version is larger or equal to the version the workflow was saved with ("semantic versioning").

Active Bundles
Bundles that have been manually added to the classpath of the snippet. Double click to remove bundles or use DEL or the "Remove Selected Bundles" button to remove a range of bundles.
Custom Type Bundles
Bundles implicitly required by fields created for inputs and outputs. To make a bundle requirement explicit, double click to add it to "Active Bundles".
Available Bundles List
List of bundles available in the current KNIME Analytics Platform installation. Filter through the list using the field above and either double click, hit ENTER or use the "Add Selected Bundles" to add bundles to "Active Bundles".

Input Ports

Icon
Input table.

Output Ports

Icon
Input table with additional or replaced columns.

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.