URL manipulation is one of those things you may not even think about until it becomes the biggest headache of your week. Thankfully KNIME has some things we can use to make life easier. Here are some of the common situations you could encounter when working with URLs in KNIME and how to handle them.
The Workflow Needs to Run in the Any System’s KNIME Directory
Perhaps the most common issue you’ll run into, most of the time you won’t want a KNIME workflow to only run on your machine or server. You still want it to use a specific file structure beyond a certain point, but you don’t want to hard-code everything before that.
KNIME has a remarkably simple way of letting you deal with this in the form of standard variable keywords. Take this Create Directory node as an example:
This syntax uses two such variables, “knime://” and “knime.workflow”. The former, “knime://”, is the system’s KNIME mount point, specified when you open your work space in the editor. This mount point almost entirely solves the problem on its own. The second keyword, “knime.workflow”, is the directory of the current workflow. Without having to define anything on your own, KNIME provides these. We can then use generic manipulation syntax like “..” to climb back up the directory structure. In this example a directory named “temp” will be made in the mount point directory which KNIME will store as a variable called “temp_dir” for this workflow. Now the rest of your workflow can reference this directory freely on any system that we run the workflow on.
The File Path to the Workflow Contains a Space, and KNIME Can’t Find the Location
Sometimes your workflow will be mounted in a URL that contains a space, and anyone who has worked with those before can tell you they’re trouble. KNIME inherently doesn’t account for these, as far it knows you’re supplying it intended syntax and you are getting intended results. Luckily there is an easy workaround for this in the URL to File Path nodes.
After creating our new directory “temp”, we can use a URL to File Path (Variable) node to turn the URL into a proper file path.
Simply choose the variable you want to clean up and you’ll end up with a nice file path for KNIME to reference with proper syntax, overwriting the variable with the output. You could also do this with some String Manipulation and Java Edit nodes if you want to alter multiple variables at once.
The Workflow Needs to Run in Windows and Linux
For as long as we live in a bipartisan world of popular operating systems, we will need to deal with this thorn in our side. KNIME doesn’t have a node to handle this inherently, but with the help of our old friend the Java Edit nodes this can be handled elegantly. The Java Edit Variable (Simple) node is quite efficient for changing a single variable, as shown below, building on our previous examples:
As KNIME is built on Java we can use any of the libraries in Java to accomplish this. Just as before this will overwrite the “temp_dir” variable with our desired output, making the workflow Windows and Linux compatible.