Editing java Snippet node to check if column exist or create it

Hi all, I have a java Snippet node set as below:

if (c_Column3_Arr1==-10){
	out_New_CLASS =  "Not_subs";
} else {
	out_New_CLASS = c_Column3;
}

I’d like to add a check before the cmd searching if “Column3_Arr1” exist. If exist the node should be follow the setting already present otherwise have to create the column “Column3_Arr1” filled by all -10 values and then follow the cmd out_New_CLASS = “Not_subs”.

Could someone suggest the right correction to the script?

Hello @tommasopalomba,

check here:

Also check this topic by using Table Validator node(s):

Br,
Ivan

2 Likes

The first example you mentioned is something that I already try, but the java snippet node configured as in the example doesn’t work and I really don’t understand why.

The saw also the second solution, but I have a doubt because in the example the column to look for is use in the input file, but if this is missing how it can be use as input? Strange.

@tommasopalomba a pretty basic code with columnExists:

out_colExists = columnExists(v_var_column_to_check) ? 1 : 0;

will give you a flow variable that you can then further use to steer a switch or something.

2 Likes

Hi @tommasopalomba , as an addendum to @mlauber71 's solution, you cannot directly access the value of Column3_Arr1 if there is a chance it won’t exist because that would then result in a configuration error.

image

Instead, to access the contents of your column which might exist, could write something like this:

	try{

		out_New_CLASS = this.getCell("Column3_Arr1", ""); 
		
		
	}	
	catch(ColumnException ex)
	{
		out_New_CLASS = "Not_subs";
	}

This does not result in an error because Column3_Arr1 is referenced only via a call to the getCell function rather than directly through a variable name. The getCell function returns a run-time exception which can be trapped, rather than a compilation/config error which cannot.

Note that you cannot, within a single java snippet optionally choose to create a new column, so you would need to have a flow containing additional nodes to perform actions such as creating new columns, as @mlauber71’s flow indicates.

4 Likes

With the new Array-type variables which I think have happened since this original question, along with the new-ish @Vernalis Configurable IF/CASE... nodes, it is possible to do this with no scripting requirements:

image

The Extract Column Header / GroupBy / Table Row to Variable sequence puts the column names into an array variable, and then the Configurable IF/CASE Switch (Flow Variable Value) node can test various options for members of the array, including the selected Any Equals option for an exact match:

NB Also the options around case sensitivity etc

See If Column Exists – KNIME Community Hub for example workflow

Steve

4 Likes

@s.roughley 's workflow reminded me I have a component on the hub from a few months ago that does something similar…

image

(You will need to have the Column Expressions node installed to use this component)

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.