在 where 子句中使用 cassandra 的 ttl()
Using cassandra's ttl() in where clause
我想问一下是否可以从 cassandra 中获取 ttl(生存时间)大于 0 的行。因此在下一步中我可以用 ttl 0 更新这些行。目标基本上是将 db 中每个条目的所有列的 ttl 更改为 0。
我试过SELECT * FROM table where ttl(column1) > 0
,但似乎无法在 where 子句中使用 ttl() 函数。
我还找到了一种方法,我们可以将所有行导出到 csv,删除 table 中的数据,然后使用新的 ttl 从 csv 再次导入它们。这行得通,但它很危险,因为我们有超过百万的生产条目,我们不知道它会如何表现。
仅使用 CQL 无法做到这一点 - 您需要一些工具的支持,例如:
- DSBulk - 您可以将 all 数据卸载到 CSV 文件中,然后使用新的 TTL 集加载回来(如果将其设置为 0,则只需加载数据)。这里有一个blog post that shows how to use DSBulk with TTL。但是你不能对 TTL 有条件,这就是为什么你需要卸载所有数据
- Spark 与 Spark Cassandra 连接器(即使在本地主模式下)。版本 2.5.0 supports TTL in the Dataframe API (earlier versions supported it only for RDD API) - for Spark 2.4 you need to correctly register functions。这可以一次性完成,直接在
spark-shell
中使用类似这样的东西(您需要调整 select
& filter
语句中的列):
import org.apache.spark.sql.cassandra._
val data = spark.read.cassandraFormat("table", "keyspace").load
val ttlData = data.select(ttl("col1").as("col_ttl"), $"col2", $"col3").filter($"col_ttl" > 0)
ttlData.drop("col_ttl").write.cassandraFormat("table", "keyspace").mode("append").save
我想问一下是否可以从 cassandra 中获取 ttl(生存时间)大于 0 的行。因此在下一步中我可以用 ttl 0 更新这些行。目标基本上是将 db 中每个条目的所有列的 ttl 更改为 0。
我试过SELECT * FROM table where ttl(column1) > 0
,但似乎无法在 where 子句中使用 ttl() 函数。
我还找到了一种方法,我们可以将所有行导出到 csv,删除 table 中的数据,然后使用新的 ttl 从 csv 再次导入它们。这行得通,但它很危险,因为我们有超过百万的生产条目,我们不知道它会如何表现。
仅使用 CQL 无法做到这一点 - 您需要一些工具的支持,例如:
- DSBulk - 您可以将 all 数据卸载到 CSV 文件中,然后使用新的 TTL 集加载回来(如果将其设置为 0,则只需加载数据)。这里有一个blog post that shows how to use DSBulk with TTL。但是你不能对 TTL 有条件,这就是为什么你需要卸载所有数据
- Spark 与 Spark Cassandra 连接器(即使在本地主模式下)。版本 2.5.0 supports TTL in the Dataframe API (earlier versions supported it only for RDD API) - for Spark 2.4 you need to correctly register functions。这可以一次性完成,直接在
spark-shell
中使用类似这样的东西(您需要调整select
&filter
语句中的列):
import org.apache.spark.sql.cassandra._
val data = spark.read.cassandraFormat("table", "keyspace").load
val ttlData = data.select(ttl("col1").as("col_ttl"), $"col2", $"col3").filter($"col_ttl" > 0)
ttlData.drop("col_ttl").write.cassandraFormat("table", "keyspace").mode("append").save