如何获取Cassandra列表类型列的项目的大小 [英] How to get size of cassandra list type column's items

查看:57
本文介绍了如何获取Cassandra列表类型列的项目的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

卡桑德拉表

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

想要的结果

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

查询

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

计数列项目是否有类似功能?

Is there something like function for count column items?

推荐答案

没有内置方法可以做到这一点

您可以做的是使用查询来获取关键字,并使用Java或其他编程语言从应用程序后端获取大小.

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.

或者Cassandra 2.2和更高版本允许用户定义函数(UDT),这些函数可以作为查询结果的一部分应用于表中存储的数据.您可以尝试下面的UDF

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();';

现在,您有了UTF函数 lsizeof ,这是使用以下查询来获取列表大小的方法

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;

输出:

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

注意:默认情况下,UDF(用户定义的函数)处于禁用状态,您可以通过在cassandra.yaml上设置enable_user_defined_functions:true来启用它

这篇关于如何获取Cassandra列表类型列的项目的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆