You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataStax Cassandra 4.x(4.13)中设置读取超时?

在DataStax Cassandra 4.x(4.13)中设置读取超时的方法

在Spring Data Cassandra适配DataStax Driver 4.x的版本中,原3.x版本的read-timeout-millis配置已被替换——因为Driver 4.x采用了更细分的超时模型。以下是具体设置方法:

1. Spring Boot配置文件方式

通过spring.data.cassandra.request.timeout配置项设置全局读取请求超时,单位支持ms(毫秒)、s(秒)等:

spring:
  data:
    cassandra:
      request:
        timeout: 2000ms

该配置对应DataStax Driver 4.x的basic.request.timeout参数,会作用于所有Cassandra读取(及其他请求)的超时控制。

2. 细粒度查询超时设置

如果需要针对特定查询单独设置超时,可通过以下方式:

方式一:使用CqlTemplate编程控制

CqlTemplate cqlTemplate = new CqlTemplate(session);
// 针对单个查询设置2000ms超时
Optional<String> result = cqlTemplate.queryForOptional(
    "SELECT value FROM my_table WHERE id = ?",
    String.class,
    "123"
).withTimeout(Duration.ofMillis(2000));

方式二:在Repository接口中通过注解设置

@Repository
public interface MyEntityRepository extends CassandraRepository<MyEntity, String> {
    // 为该查询单独设置2000ms超时
    @Query(value = "SELECT * FROM my_table WHERE id = ?0", timeout = "2000ms")
    Optional<MyEntity> findByIdWithCustomTimeout(String id);
}

3. 直接配置DataStax Driver Session

若需手动构建Session,可通过DriverConfigLoader配置超时:

DriverConfigLoader configLoader = DriverConfigLoader.programmaticBuilder()
    .withString("basic.request.timeout", "2000ms")
    .build();

CqlSession session = CqlSession.builder()
    .withConfigLoader(configLoader)
    .withKeyspace("my_keyspace")
    .build();

内容的提问来源于stack exchange,提问作者Sudhirkumar Murkute

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:20:08