| by Arround The Web | No comments

Cassandra Delete Function

This article will teach you the simple command to remove any User Defined Function within a Cassandra cluster. Keeping your database optimized and running smoothly is always a good practice. One of the methods to do that is to clean up any unused and unnecessary objects in the cluster.

Cassandra Create Simple Function.

The following code snippet below shows how to use the create function instructions.

CREATE [OR REPLACE] FUNCTION [IF NOT EXISTS] [keyspace_name.]function_name (
    variable_name variable_type [,...] )
[CALLED | RETURNS NULL] ON NULL INPUT
RETURNS data_type
LANGUAGE language_identifier AS
'code_instructions;

We start with the CREATE OR REPLACE FUNCTION statement. This instruction creates a new function if it does not exist. If the function exists, the command will overwrite the instructions of the function.

You can use the IF NOT EXISTS command to hide the errors if you do not include the REPLACE instructions. Therefore, use the OR REPLACE to replace the function if it exists and IF NOT EXISTS to suppress any errors.

The variable_name and variable_type are used to define a variable and the corresponding data type is passed into the code block. To declare multiple variables, you can use specify them as a list of a comma-separated list.

The CALLED ON NULL INPUT section runs the provided code block even if the input value is null.

The RETURN NULL ON NULL INPUT allows the function to return NULL on NULL input.

RETURN data_type specifies the return value of the function. This value must be a supported CQL data type.

The LANGUAGE language_identifier section defines the programming language of the function. By default, Cassandra supports Java and JavaScript out of the box. However, you can add support for other languages such as Ruby, Python, Scala, and etc.

Finally, the `code_block` | $$code_block$$ section defines the code for the function. If the function contains special characters, enclose the code block in dollar signs. Otherwise, enclose the code block in single quotes.

Note that like normal functions, UDFs may result in various exceptions. You can ensure the functions do not fail by implementing error handling with your programming language of choice.

Example

The example below shows how to create a function that returns the maximum value between numerical input values.

CREATE OR REPLACE FUNCTION Get_max(input1 int, input2)
CALLED ON NULL INPUT
RETURNS int LANGUAGE java AS
$$return Math.max(input1, input2);$$;

Once the function is defined, you can use it on a table as shown:

SELECT col_list,Get_mac(col1, col2)
FROM UDF_Function_test
WHERE column IN(values);

Cassandra Delete Function

Once we have defined a function, we can delete it using the DROP FUNCTION command. The syntax is as shown:

DROP FUNCTION [IF EXISTS] [keyspace.]function_name

For example, to remove the UDF Get_max(), we can run the command:

cqlsh:sample> DROP FUNCTION IF EXISTS sample.Get_max;

Conclusion

This post covered how to create and drop a User Defined Function in Cassandra using the CQLSH commands.

Share Button

Source: linuxhint.com

Leave a Reply