我有一个用例,其中我需要编写一个脚本来清除 cassandra 数据库中超过 90 天的所有数据的表。 现在的问题是数据库具有以下格式的事件日期(时间戳):
2018-01-21 12:33:12+0000
我无法从我的脚本中获得准确的时间戳:如小时、分钟和秒,除非我知道查询不会获取此数据。
谁能建议我如何从只有日期的表中选择数据:yyyy-mm-dd。
注意:eventdate 是表的分区键。 此外,我目前正在使用 python 编写此脚本。
更新: 当我尝试使用 token (事件日期)时:
SELECT * from solr_reports_table_v1 WHERE key1='test' and key2 = 'test' and key3='test' and token(eventdate) > '2018-01-20';
我收到以下错误:
InvalidRequest: code=2200 [Invalid query] message="Invalid STRING constant (2018-01-20) for "partition key token" of type bigint"
这是事件日期字段:
eventdate timestamp
最佳答案
无法在分区键上指定范围过滤器。所以,做这样的事情是不可能的
select * from mytable where eventdate > some_date_1 and eventdate < some_date_2
此外,简单地选择所有行并遍历它们是行不通的,因为当您遇到异常时,您无法从离开的地方继续。
即使您不能在分区键上指定范围过滤器,您也可以使用 token function 指定范围过滤器CQL:
SELECT eventdate FROM mytable WHERE token(eventdate) >= start_token AND token(eventdate ) < end_token;
每次迭代后,您应该将end_token+1
赋值给start_token
,并计算新的end_token
。您还可以通过将整个 token 范围划分并共享到同时运行的多个线程中来实现并行化。
关于python - 仅使用日期从cassandra中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576496/