1
votes

I am new to Apache Cassandra and javascript, I need to create javascript udf in Apache Cassandra. I created a simple udf in Cassandra but I am not able to call the function in the query an error pops up

here are all the details related to my question

my table looks like this

 id   | age | course | name    | sex
------+-----+--------+---------+--------
 st05 |  23 | b.tech |  nafees |   male
 st03 |  27 |    mca | lengdon |   male
 st02 |  24 |    mca |  jaanvi | female
 st01 |  22 |    mca |   sahil |   male
 st04 |  23 | b.tech |     jay |   male

CREATE TABLE test.student (
        id text PRIMARY KEY,
        age int,
        course text,
        name text,
        sex text
    )

I have also created an index on age column

CREATE INDEX age_index ON test.student (age);

my function looks like this

CREATE OR REPLACE FUNCTION  demojs (input int) 
   CALLED ON NULL INPUT 
   RETURNS int 
   LANGUAGE javascript AS 'function(input){
    return input;
   }';

my query is

 select * from student where age=demojs(22);

when I try to execute the query I got the following error

Traceback (most recent call last): File "C:\Program Files\apache-cassandra-3.11.6\bin\cqlsh.py", line 1050, in perform_simple_statement result = future.result() File "C:\Program Files\apache-cassandra-3.11.6\bin..\lib\cassandra-driver-internal-only-3.11.0-bb96859b.zip\cassandra-driver-3.11.0-bb96859b\cassandra\cluster.py", line 3925, in result raise self._final_exception FunctionFailure: Error from server: code=1400 [User Defined Function failure] message="execution of 'test.demojs[int]' failed: com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type int"

can anyone please tell what am I doing wrong here, it will be of great help

1

1 Answers

1
votes

the body of the function will be like this

   CREATE OR REPLACE FUNCTION  demojs (input int) 
   CALLED ON NULL INPUT 
   RETURNS int 
   LANGUAGE javascript AS 'input;';