KI-YOUNG BANG
KI-YOUNG BANG

Reputation: 199

How to get size of cassandra list type column's items

cassandra table

 seq | keywords            | timestamp
-----+---------------------+---------------
 100 | ['apple', 'banana'] | 1488637750836

wanted result

 seq | keyword_size
-----+--------------
 100 | 2

query

select
    seq,
    something(keywords) as keyword_size
from
    t
where
    seq = 100

Is there something like function for count column items?

Upvotes: 1

Views: 1986

Answers (1)

Ashraful Islam
Ashraful Islam

Reputation: 12830

There is no builtin method to do this

What you can do is get the keywords using a query and get the size using Java or Other programming language from your application backend.

Or Cassandra 2.2 and later allows users to define functions (UDT) that can be applied to data stored in a table as part of a query resul. You can try the below UDF

CREATE OR REPLACE FUNCTION lsizeof(data list<text>) CALLED ON NULL INPUT RETURNS int LANGUAGE java AS 'return data.size();';

Now you have the UTF function lsizeof, here is how you can get the size of your list using a query like below

SELECT seq, lsizeof(keywords) as keyword_size FROM test_list;

Output :

 seq | keyword_size
-----+--------------
 100 |            2

Note : UDFs (user defined functions) are disabled by default, you can enable it by setting enable_user_defined_functions: true on cassandra.yaml

Upvotes: 3

Related Questions