Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra query string length

How can I get the length of a string in Cassandra? There don't seem to be any built-in functions to do this for me.

Looking for something like this:

SELECT size(myStr) FROM myTable WHERE ...

I know this may be possible using user-defined functions, but I'm not sure if I have the appropriate permissions to be able to create functions.

like image 243
Ares Avatar asked Oct 17 '22 20:10

Ares


1 Answers

There is no inbuilt function for it but you can create UDF in Cassandra to find the length of the String column and then use that UDF in your query. Before creating this UDF make sure you have enabled udf property in your cassandra.yml file i.e. enable_user_defined_functions: true

CREATE FUNCTION IF NOT EXISTS len (input text) 
   CALLED ON NULL INPUT 
   RETURNS int 
   LANGUAGE java AS '
   return input.length();';

Then you can use it in your query as,

cqlsh:test> SELECT len(event_data) as length from event where id = 1;

And you should get output,

 length
--------
     14
like image 197
vindev Avatar answered Oct 21 '22 09:10

vindev