我有一个由三个节点组成的Cassandra集群,并创建了一个超过2,000,000行的表。
当我在cqlsh中执行此查询 (select count(*) from userdetails
) 时,我收到以下错误消息:
OperationTimedOut: errors={}, last_host=192.168.1.2
当我对少量行或带有限制的50,000行运行计数函数时,它可以正常工作。
count(*) 实际上会遍历所有数据。因此,一个没有限制的 select count(*) from userdetails
可能会由于大量行而超时。有关详细信息,请参见此处:
http://planetcassandra.org/blog/counting-key-in-cassandra/
您可以考虑使用 Spark 自己维护计数,或者如果只需要个大致数字,可以从 JMX 获取它。
从 JMX 获取可能有点棘手,这取决于您的数据模型。要获取分区数,请获取 org.apache.cassandra.metrics:type=ColumnFamily,keyspace={{Keyspace}},scope={{Table}},name=EstimatedColumnCountHistogram
mbean 并将所有 90 值相加(这是 nodetool cfstats
输出的内容)。它只会给出存在于 sstables 中的数量,因此为了使其更准确,您可以执行 flush 或尝试估算 MemtableColumnsCount
mbean 中 memtables 中的数量。
对于一个非常基础的大致数字,您可以从列出的所有范围的 system.size_estimates
获取估计的分区计数(请注意,这仅是一个节点上的数字)。将其乘以节点数,然后除以 RF。
您还可以在cqlsh命令中增加超时时间,例如:
cqlsh --request-timeout 120 myhost
select
语句。请查看此链接:https://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/cqlsh_commands/cqlsh.html - shruti1810我正在使用Cassandra 3.4和cqlsh获取记录计数。看起来在3.4中有一个代码更改。cqlsh只是调用cqlsh.py。在cqlsh.py内部,有一个名为DEFAULT_REQUEST_TIMEOUT_SECONDS
的变量,默认值为10秒。我将其更改为3600秒(1小时),现在我的SELECT count(*)
查询可以正常工作。
# 3,600,000 is one hour in ms
read_request_timeout_in_ms: 3600000
range_request_timeout_in_ms: 3600000
write_request_timeout_in_ms: 3600000
counter_write_request_timeout_in_ms: 3600000
cas_contention_timeout_in_ms: 3600000
truncate_request_timeout_in_ms: 3600000
request_timeout_in_ms: 3600000
slow_query_log_timeout_in_ms: 3600000
然后在所有节点上重新启动Cassandra。
第二步是使用指定的超时时间运行'cqlsh',如下所示:
cqlsh --request-timeout=3600000 <myhost>
如果我对一天进行计数,我会遇到与你相同的问题,但是作为解决方法,我将计数拆分为两个请求(12小时+12小时),如下所示。
cqlsh:jw_schema1> select count(*) from flight_statistics where insert_time >= '2015-08-20 00:00:00' and insert_time <= '2015-08-20 11:59:59' ALLOW FILTERING;
count
-------
42528
(1 rows)
cqlsh:jw_schema1> select count(*) from flight_statistics where insert_time >= '2015-08-20 12:00:00' and insert_time <= '2015-08-20 23:59:59' ALLOW FILTERING;
count
-------
86580
(1 rows)
cqlsh:jw_schema1>
org.apache.cassandra.metrics:type=ColumnFamily,keyspace={{Keyspace}},scope={{Table}},name=EstimatedRowCount
(新版本中为 EstimatedPartitionCount)来包含内存表中的数量,并更好地获取近似行数。 - Chris Lohfink