如何获得卡桑德拉的当年 [英] How to get current year on cassandra

查看:65
本文介绍了如何获得卡桑德拉的当年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Cassandra中获得部分当前日期?在我的特殊情况下,我只需要获取年份。

How can I get just a part of the current date in Cassandra? In my particular case I need to get just the year.

我暂时从系统中选择 select dateof(now())。当地;

但是我找不到任何函数来获取文档
https://docs.datastax.com/en/dse/5.1/cql /cql/cql_reference/refCqlFunction.html#refCqlFunction__toTimestamp

But I could not find any function to get just the year in the documentation https://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/refCqlFunction.html#refCqlFunction__toTimestamp

我是Cassandra的新手,所以这可能是一个愚蠢的问题。

I'm new with Cassandra so this maybe a silly question.

推荐答案

safe 方法是返回时间戳并解析年份客户端,

The safe way, would be to return a timestamp and parse-out the year client-side.

从本质上讲,Cassandra没有任何功能可以帮助解决这一问题。但是,您可以编写一个用户定义函数(UDF)来完成此操作:

Natively, Cassandra does not have any functions that can help with this. However, you can write a user-defined function (UDF) to accomplish this:

首先,默认情况下禁用用户定义函数。您需要在cassandra.yaml中调整该设置,然后重新启动节点。

First, user defined functions are disabled by default. You'll need to adjust that setting in your cassandra.yaml and restart your node(s).

enable_user_defined_functions=true

注意:将此设置默认为有原因。尽管Cassandra 3.x具有适当的防护措施来防范恶意代码,但最好还是关闭此功能,除非您既需要它又知道自己在做什么。即便如此,您还是要关注已定义的UDF。

现在,我将在cqlsh中使用Java创建函数。 :

Now I'll create my function using Java, from within cqlsh:

cassdba@cqlsh:stackoverflow> CREATE OR REPLACE FUNCTION year (input DATE)
RETURNS NULL ON NULL INPUT RETURNS TEXT
LANGUAGE java AS 'return input.toString().substring(0,4);';

请注意,可以通过多种方式(和类型)查询当前日期/时间:

Note that there are a number of ways (and types) to query the current date/time:

cassdba@cqlsh:stackoverflow> SELECT todate(now()) as date,
    totimestamp(now()) as timestamp, now() as timeuuid FROm system.local;

 date       | timestamp                       | timeuuid
------------+---------------------------------+-------------------------------------
 2017-12-20 | 2017-12-20 21:18:37.708000+0000 | 58167cc1-e5cb-11e7-9765-a98c427e8248

(1 rows)

要仅返回年份,我可以在 todate(now())列上调用我的 year 函数:

To return just only year, I can call my year function on the todate(now()) column:

SELECT stackoverflow.year(todate(now())) as year FROm system.local;

 year
------
 2017

(1 rows)

这篇关于如何获得卡桑德拉的当年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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