Sitemap
Low Code for Data Science

A journal of articles written by (and for) the KNIME Community around visual programming, data science algorithms & techniques, integration with external tools, case studies, success stories, data processing, and (of course) KNIME Software.

GETTING STARTED | WORKFLOW CONTROL | KNIME ANALYTICS PLATFORM

KNIME — Cases, Switches and Catching Errors

An overview of the key capabilities for workflow control

6 min readNov 15, 2024

--

Press enter or click to view image in full size
Some of the nodes for workflow control on the KNIME Community Hub.

Besides Loops another important tool in the Data Scientist’s inventory are switches and the ability to catch and handle errors. KNIME as your favorite low-code data analytics tool is well equipped in that regard.

First you have basically two types of nodes here:

  • Switches where you set a rule and know what you are expecting
  • Catching errors. Where something might go wrong and the program should react to it

You can combine the two if you want.

TL:DR: Just show me the sample workflows:
Switch — Empty Tables Switches and Cases — Try-Catch
Switch — Examples of how to use Case When

Similar to loops, you will have to create an enclosed set of nodes with a node at the start that will initiate the action (or the monitoring) and a node at the end that will capture the results. Like always a combination with Flow Variables or Loops is absolutely possible and will enhance your automation capabilities.

CASE Switch Start / End

KNIME has introduced these powerful nodes where you have the flexibility to work with nearly every kind of port type and send the work-stream to the branch you choose. The CASE Switch Start node can handle various port types:

Press enter or click to view image in full size
The KNIME CASE Switch Start node can handle various port types
The KNIME CASE Switch Start node can handle various port types (https://hub.knime.com/n/_XbJ1lfYviQcqS2C).

The port number to continue with can be controlled by a Flow Variable that you can fill from the results of earlier workflows or you could let the user enter a value. Ports start with 0 (zero):

Press enter or click to view image in full size
The KNIME port can be se via an integer Flow Variable — ports starting with 0. You can add more if you need them
The port can be se via an integer Flow Variable — ports starting with 0. You can add more if you need them (https://hub.knime.com/s/JprkwkWJ_CZJIZDq).

You can use all sorts of nodes that support Flow Variables to make your decision, like for example the Rule Engine Variable.

Press enter or click to view image in full size
Fill the port you want to activate using all kinds of KNIME nodes
Fill the port you want to activate using all kinds of KNIME nodes (https://hub.knime.com/s/JprkwkWJ_CZJIZDq).

The last thing to do to finish your basic CASE Switch Start is close the task with a CASE Switch End node. You will have to decide what should happen then. So, for example, if you send through a data (table), you will have to handle RowIDs — and also provide an alternative stream of data:

Press enter or click to view image in full size
At the switch end you decide what to do with RowIDs if you have a table
At the switch end you decide what to do with RowIDs if you have a table (https://hub.knime.com/s/JprkwkWJ_CZJIZDq).

You can also just use Flow Variables at the end and do completely different things in each branch of the switch. You will have to plan ahead what you want to do and also take care of edge cases and make sure your workflow will run through.

You can mix CASE Ports of different types

You can mix different types of ports in a switch construct. For example, you can start with a Flow Variable CASE Switch and then collect the results in a database connection that is taken from one of the branches:

Press enter or click to view image in full size
You can mix the CASE Switch Start and End for different types of ports. Here you have Flow Variables at the start and Database nodes at the end
You can mix the CASE Switch Start and End for different types of ports (https://forum.knime.com/t/db-query-case-switch/63990/7?u=mlauber71).

Catch an Error in KNIME

The next thing is to handle errors in KNIME and continue (or stop) the workflow. A very basic node is the Empty Table Switch node that would allow you to identify (well) empty tables and stop the process or start an alternative one without having to deal with flow variables or specific rules.

Press enter or click to view image in full size
Empty Table switch also does work with CASE Switch end
Empty Table switch also does work with CASE Switch end (https://hub.knime.com/s/cgo0OcDsBSVp7j7A).

Catch the Error and provide an Alternative

Key to working with this Try-Catch Concept in KNIME is to prepare the ‘wiring’ in a correct way. Most of the time you will put one of the Try nodes at the start of the block you expect to possibly fail and then provide an alternative of the same port type.

Press enter or click to view image in full size
An overview of the Try / Catch nodes helping you navigate errors in KNIME
An overview of the Try / Catch nodes helping you navigate errors in KNIME (https://hub.knime.com/search?q=try&type=node&tag=Error+Handling%2CWorkflow+Control&sort=best).

The most basic version would involve a Try node (Flow Variable or Table), then a data process that might fail (in this case failing it deliberately) and then providing an alternative. This alternative might have to have the same structure as the original data, if you want to continue your processes.

Press enter or click to view image in full size
Basic Try-Catch construct in KNIME Analytics Platform
Basic Try-Catch construct in KNIME (https://hub.knime.com/s/cgo0OcDsBSVp7j7A).

This alternative you provide also can be a Flow Variable so you have a wide range of options of what to do. You can even combine the two concepts of Try and Switch to handle errors and then provide a reaction to the error:

Press enter or click to view image in full size
A basic combination of Try/Catch and Case Switch
A basic combination of Try/Catch and Case Switch (https://forum.knime.com/t/try-catch-with-failing-excel-reader-node-cant-activate-alternative-branch/80431/2?u=mlauber71).

You can expand these KNIME concepts further by employing loops.

Loop until a Job is Finished

In the next example you make sure an Excel file is written to a folder (might be a remote folder or via a shaky connection) and the job continues until success (or after a fixed number of trials).

Press enter or click to view image in full size
Loop until a job is finished. If not the try/catch sends the job back to the start
Loop until a job is finished. If not the try/catch sends the job back to the start (https://forum.knime.com/t/error-handling-within-table-row-to-variable-loop-start/77784/5?u=mlauber71).

You could also just record which files have been written (or being downloaded) successfully, so you can deal with the problems later without stopping your task:

I hope you enjoyed this text and move on to improve your workflow handling and automation.

Further hints and examples of switches and cases you might explore

In case you enjoyed this story you can follow me on Medium (https://medium.com/@mlxl) or on the KNIME Community Hub (https://hub.knime.com/mlauber71) or KNIME Forum (https://forum.knime.com/u/mlauber71/summary).

--

--

Low Code for Data Science
Low Code for Data Science

Published in Low Code for Data Science

A journal of articles written by (and for) the KNIME Community around visual programming, data science algorithms & techniques, integration with external tools, case studies, success stories, data processing, and (of course) KNIME Software.

Markus Lauber
Markus Lauber

Written by Markus Lauber

Senior Data Scientist working with KNIME, Python, R and Big Data Systems as well as Google Cloud (BigQuery, Vertex AI) in the telco industry

No responses yet