Error: Java snippet does not recognize relative path

Hi everyone,
1. I am migrating a workflow from KNIME server to Business Hub, for which I am working on KNIME 5.3.3 and using a Java Snippet that creates a table in BigQuery with the following script:

// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;

// Your custom imports:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input flow variables
/** Input flow variable: “project” /
public String v_project;
/
* Input flow variable: “dataset” /
public String v_dataset;
/
* Input flow variable: “table_name” /
public String v_table_name;
/
* Input flow variable: “file_location” /
public String v_file;
/
* Input flow variable: “schema_location” */
public String v_schema;

// Fields for output columns
/** Output column: “Resultado” /
public String out_Resultado;
/
* Output column: “command” /
public String out_command;
/
* Output column: “Message” */
public String out_Message;

// Your custom variables:
int exitValue;
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
String command = new String {“bq”, “–headless=true”, “-q”, “mk”,“–table”,“-f”,
v_project+“:”+v_dataset+“.”+v_table_name,
v_schema};
try {
Process p = Runtime.getRuntime().exec
(
command
);
//Wait for execute and get exit code
exitValue = p.waitFor();
out_Resultado = String.valueOf(exitValue);
// Get error message
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
out_Message = stdInput.lines().collect(Collectors.joining()) + stdError.lines().collect(Collectors.joining());
// Get executed command
out_command = String.join(" ", command);
} catch (Exception e) {
out_Resultado = “False”;
e.printStackTrace();
}
if (exitValue !=0){
throw new Abort(out_Message);
}

// expression end

2. The parameters that this node receives are those necessary to create a table in BigQuery, among them is schema, which is a path in string format (because the Java snippet does not recognize the path format) of a .json file located in a temporary folder of the computer/virtual machine, with the structure of the tables that BigQuery creates.

3. When executing the Java snippet node, it throws the following error:

4. When this workflow was working (in KNIME server), a local path of the computer or virtual machine was sent to the schema variable, but when migrating to the HUB, local paths are not allowed and, therefore, this node stops working, since the HUB creates relative paths that point to the HUB.

5. How can I make it recognize the relative path provided by the HUB?

I am very grateful if you can help me with this problem.

@eediazh The relative path is definitely what is causing your issue. Due to Hub’s containerized architecture the executor wouldn’t be able to access the schema file through a relative path as the file wouldn’t exist inside the container.

If the schema file is stored in a team space on Hub you could use the Create File/Folder Variables node to populate the schema_location flow variable with a path to the file in the hub space, but I don’t think you would be able to translate this to a working string that your java snippet could use.

You could try a mountpoint path that points to your hub instance, but this would get translated to a string as a URI (e.g. knime://knime.space/schema.json) that most likely wouldn’t be understood by the snippet either.

The only option I can think of would be to store the schema file on a network file share. Then provide the full path to that file share location as the schema_location variable. The executor running the java snippet would need to be able to access the file share, but I believe this should work otherwise.

I think this might be easier though if you forego the java snippet altogether and just use the Google BigQuery Connector and a DB Table Creator to create the table you need.

2 Likes

Hello @kpowney,

Thank you for your response.

We were able to resolve the issue by creating a temporary folder in the workflows area. The path could not be relative to the Knime Hub; instead, we used an absolute path to the temporary folder within the machine.