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

眉间皱痕 提交于 2021-01-05 12:27:54

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/51604961/how-to-get-size-of-cassandra-list-type-columns-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!