5

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.

2 Answers 2

4

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
0

It is possible to make a UDF to do it, but there is none built in cassandra.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.