快速估计 Cassandra 中的行数 table

Fast estimated count of rows in Cassandra table

我很惊讶以前没有人提出这个问题。

假设我们在cassandra中有一个巨大的table,我们需要获得其中的估计行数(不是精确的,只是近似值)。

看似简单select count(*) from table效率不高而且会花费很多时间。我们需要一些又脏又快的东西。

Datastax 博客建议 the following:

I don’t care about the exact number, can I have a ballpark estimate?

Because Cassandra knows how many rows there are in each SSTable it is possible to get an estimate. The ‘nodetool cfstats’ output tells you these counts in the ‘Number of Keys (estimate)’ line. This is the sum of rows in each SStable (again approximate due to the indexing used but can’t be off by more than 128 by default).

我的问题:我们可以使用 DataStax Enterprise Java driver 执行相同的操作吗?

P.S. 我无法更改 table 的结构或其他任何内容。考虑一下我使用旧模式。换句话说,我对添加计数器或其他特殊字段等变通方法不感兴趣。

Cassandra 也通过 JMX 公开了近似计数(从 "nodetool cfstats" 获得)。该代码可以连接到此 JMX 指标,以编程方式获取计数。

EstimatedPartitionCount Gauge Approximate number of keys in table.

 {
    "type": "READ",
    "mbean": "org.apache.cassandra.metrics:type=Table,keyspace=*,scope=*,name=*",
    "attribute": "Count"
  }

这里是关于公开的所有 JMX 指标的 link