如何在Cassandra中将日期类型的值转换为文本? [英] How to convert a value of date type to text in Cassandra?

查看:226
本文介绍了如何在Cassandra中将日期类型的值转换为文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的Cassandra表-

I have a Cassandra table with following structure -

CREATE TABLE timeseries_s (
   prop_name text PRIMARY KEY,
   description text,
   value text
);

现在我需要在此表中插入一个属性,其值将是文本格式的当前日期。我创建了以下CQL,但它给了我以下错误-

Now I need to insert a property in this table whose value will be current date in text format. I have created the following CQL, but it gives me the following error -

 INSERT INTO timeseries_s (prop_name, description, value) VALUES ('xyz', 'abc', TODATE(now())));

错误-SyntaxException:第1168行的输入')'不匹配,期望EOF(.. .'abc',TODATE(now())[[]] ...)

Error - SyntaxException: line 1:168 mismatched input ')' expecting EOF (...'abc', TODATE(now()))[)]...)

此后,我在下面尝试-

 INSERT INTO timeseries_s (prop_name, description, value) VALUES ('xyz', 'Migration for DSE started at this time', CAST(TODATE(now()) AS TEXT));

错误-SyntaxException:行1:158输入'('(。 .. VALUES('xyz','abc',CAST

Error - SyntaxException: line 1:158 no viable alternative at input '(' (... VALUES ('xyz', 'abc', CAST

有任何建议吗?

推荐答案

我想想到的仅在Cassandra方面解决此问题的唯一方法就是使用用户定义的函数,首先,您需要在 cassandra.yaml中启用用户定义的函数

The only way I can think of to solve this one purely on the Cassandra side, is with a user defined function. First of all, you'll need to enable user defined functions in the cassandra.yaml:

enable_user_defined_functions: true

节点重新启动后,我通过在 stackoverflow totext 的函数来完成此操作。 c $ c>键空间,例如:

Once the node(s) has been restarted, I accomplished this by defining a function called totext in my stackoverflow keyspace, like this:

aploetz@cqlsh:stackoverflow> CREATE OR REPLACE FUNCTION totext (input DATE)
             RETURNS NULL ON NULL INPUT RETURNS TEXT
             LANGUAGE java AS 'return input.toString();';

使用该功能创建后,您可以在 INSERT 中使用它,例如thi s:

With that function created, you can use it in your INSERT like this:

> INSERT INTO timeseries_s (prop_name, description, value)
VALUES ('xyz', 'Migration for DSE started at this time',
                     stackoverflow.totext(todate(now())));

> SELECT * FROM timeseries_s ;

 prop_name | description                            | value
-----------+----------------------------------------+------------
       xyz | Migration for DSE started at this time | 2020-09-03

(1 rows)

这篇关于如何在Cassandra中将日期类型的值转换为文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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