只需连续设置TTL [英] Just set the TTL on a row
问题描述
使用Java,我可以扫描Cassandra表并仅更新一行的TTL吗?我不想更改任何数据。我只想扫描Cassandra表并设置几行TTL。
Using Java, can I scan a Cassandra table and just update the TTL of a row? I don't want to change any data. I just want to scan Cassandra table and set TTL of a few rows.
此外,使用java,我可以设置绝对的TTL吗?例如(2016-11-22 00:00:00)。因此,我不想以秒为单位指定TTL,而是指定时间的绝对值。
Also, using java, can I set TTL which is absolute. for example (2016-11-22 00:00:00). so I don't want to specify the TTL in seconds, but specify the absolute value in time.
推荐答案
Cassandra并没有允许为行设置TTL值,仅允许为值列设置TTL。
Cassandra doesn't allow to set the TTL value for a row, it allows to set TTLs for columns values only.
如果您想知道为什么行发生行 到期,这是因为如果全部记录的所有列的值都是TTL的,则当您尝试 SELECT
时,该行会消失。
In the case you're wondering why you are experiencing rows expiration, this is because if all the values of all the columns of a record are TTLed then the row disappears when you try to SELECT
it.
但是,只有当您使用 USING TTL执行
然后使用TTL进行 INSERT
时,这才是正确的/ code>。如果您 UPDATE
,您仍会看到该行,但其值为空。以下是一些示例和一些陷阱:
However, this is only true if you perform an INSERT
with the USING TTL
. If you INSERT
without TTL and then do an UPDATE
with TTL you'll still see the row, but with null values. Here's a few examples and some gotchas:
CREATE TABLE test (
k text PRIMARY KEY,
v int,
);
INSERT INTO test (k,v) VALUES ('test', 1) USING TTL 10;
... 10 seconds after...
SELECT * FROM test ;
k | v
---------------+---------------
带有TTLed INSERT
和TTLed <$ c的示例$ c>更新:
Example with a TTLed INSERT
and a TTLed UPDATE
:
INSERT INTO test (k,v) VALUES ('test', 1) USING TTL 10;
UPDATE test USING TTL 10 SET v=0 WHERE k='test';
... 10 seconds after...
SELECT * FROM test;
k | v
---------------+---------------
使用非TTLed INSERT
且使用TTLed UPDATE
Example with a non-TTLed INSERT
with a TTLed UPDATE
INSERT INTO test (k,v) VALUES ('test', 1);
UPDATE test USING TTL 10 SET v=0 WHERE k='test';
... 10 seconds after...
SELECT * FROM test;
k | v
---------------+---------------
test | null
现在您可以看到解决问题的唯一方法是重写 all 具有新TTL的行中所有列的值。
Now you can see that the only way to solve you problem is to rewrite all the values of all the columns of your row with a new TTL.
此外,无法指定明确的到期日期,但是您可以在秒内获得一个简单的TTL值
In addition, there's no way to specify an explicit expiration date, but you can get a simple TTL value in seconds with simple math (as other suggested).
看看官方的文档有关数据过期的信息。并且不要忘了看看 DELETE 更新TTL的部分。
Have a look at the official documentation about data expiration. And don't forget to have a look at the DELETE section for updating TTLs.
HTH。
这篇关于只需连续设置TTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!