從 1.x 到 2.x 的遷移指南

從早期版本升級時,Spring Data for Apache Cassandra 2.0 引入了一系列重大變更

  • spring-cqlspring-data-cassandra 模組合併為一個模組。

  • CqlOperationsCassandraOperations 中的非同步和同步操作分離到專用的介面和模板中。

  • 修訂了 CqlTemplate API,使其與 JdbcTemplate 對齊。

  • 移除了 CassandraOperations.selectBySimpleIds 方法。

  • CassandraRepository 使用了更好的名稱。

  • 移除了 SD Cassandra 的 ConsistencyLevelRetryPolicy 型別,轉而使用 DataStax 的 ConsistencyLevelRetryPolicy 型別。

  • 將 CQL 規範重構為值物件和配置器。

  • QueryOptions 重構為不可變物件。

  • CassandraPersistentProperty 重構為單列。

棄用

  • 棄用 QueryOptionsBuilder.readTimeout(long, TimeUnit),轉而使用 QueryOptionsBuilder.readTimeout(Duration)

  • 棄用 CustomConversions,轉而使用 CassandraCustomConversions

  • 棄用 BasicCassandraMappingContext,轉而使用 CassandraMappingContext

  • 棄用 o.s.d.c.core.cql.CachedPreparedStatementCreator,轉而使用 o.s.d.c.core.cql.support.CachedPreparedStatementCreator

  • 棄用 CqlTemplate.getSession(),轉而使用 getSessionFactory()

  • 棄用 CqlIdentifier.cqlId(…)KeyspaceIdentifier.ksId(…),轉而使用 `.of(…)` 方法。

  • 棄用 QueryOptions 的建構函式,轉而使用其構建器。

  • 棄用 TypedIdCassandraRepository,轉而使用 CassandraRepository

合併 Spring CQL 和 Spring Data Cassandra 模組

Spring CQL 和 Spring Data Cassandra 現已合併為一個模組。獨立的 spring-cql 模組不再可用。你可以在 spring-data-cassandra 中找到所有合併的型別。以下列表展示瞭如何在 Maven 依賴中包含 spring-data-cassandra

<dependencies>

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-cassandra</artifactId>
    <version>4.4.5</version>
  </dependency>

</dependencies>

合併後,我們將所有 CQL 包合併到 Spring Data Cassandra 中

  • o.s.d.cql 移至 o.s.d.cassandra.core.cql

  • o.s.d.cqlo.s.d.cassandra.config 合併,並展平了 XML 和 Java 子包。

  • CassandraExceptionTranslatorCqlExceptionTranslator 移至 o.s.d.c.core.cql

  • 將 Cassandra 異常 o.s.d.c.support.exception 移至 o.s.d.cassandra

  • o.s.d.c.convert 移至 o.s.d.c.core.convert(影響轉換器)。

  • o.s.d.c.mapping 移至 o.s.d.c.core.mapping(影響對映註解)。

  • MapIdo.s.d.c.repository 移至 o.s.d.c.core.mapping

[[revised-cqltemplate/cassandratemplate]] == 修訂 CqlTemplate/CassandraTemplate

我們將 CqlTemplateCassandraTemplate 分為三個方面

  • CassandraTemplate 不再是 CqlTemplate,而是使用一個例項,該例項允許對 fetch size、一致性級別和重試策略進行復用和細粒度控制。你可以透過 CassandraTemplate.getCqlOperations() 獲取 CqlOperations。由於此變更,CqlTemplate 的依賴注入需要額外的 Bean 設定。

  • CqlTemplate 現在反映的是基本的 CQL 操作,而不是混合高階和低階 API 呼叫(例如 count(…)` 與 `execute(…)`),並且精簡後的方法集與 Spring Framework 的 `JdbcTemplate` 及其便利的回撥介面對齊。

  • 非同步方法已在 AsyncCqlTemplateAsyncCassandraTemplate 上使用 ListenableFuture 重新實現。我們移除了 Cancellable 和各種非同步回撥監聽器。ListenableFuture 是一種靈活的方法,允許轉換為 CompletableFuture

移除了 CassandraOperations.selectBySimpleIds()

此方法被移除是因為它不支援複雜 ID。新引入的查詢 DSL 允許對單列 ID 使用對映和複雜 ID,如下例所示

cassandraTemplate.select(Query.query(Criteria.where("id").in(…)), Person.class)

CassandraRepository 使用更好的名稱

我們重新命名了 CassandraRepositoryTypedIdCassandraRepository,以使 Spring Data Cassandra 的命名與其他 Spring Data 模組對齊

  • CassandraRepository 重新命名為 MapIdCassandraRepository

  • TypedIdCassandraRepository 重新命名為 CassandraRepository

  • 引入了 TypedIdCassandraRepository,將其作為已棄用型別擴充套件 CassandraRepository 以方便遷移

移除了 SD Cassandra 的 ConsistencyLevelRetryPolicy 型別,轉而使用 DataStax 的 ConsistencyLevelRetryPolicy 型別

Spring Data Cassandra 的 ConsistencyLevelRetryPolicy 已被移除。請使用 DataStax 驅動提供的型別。

Spring Data Cassandra 型別限制了 Cassandra 原生驅動中提供的和允許的可用功能的使用。因此,每當驅動引入新功能時,Spring Data Cassandra 的型別都需要更新。

將 CQL 規範重構為值物件和配置器

CQL 規範型別現在儘可能地是值型別(例如 FieldSpecificationAlterColumnSpecification),並且物件透過靜態工廠方法構造。這允許簡單的值物件保持不可變性。對強制屬性(例如表名或鍵空間名)進行操作的配置器物件(例如 AlterTableSpecification)最初透過靜態工廠方法構造,並允許進一步配置,直到建立所需的狀態。

QueryOptions 重構為不可變物件

QueryOptionsWriteOptions 現在是不可變的,可以透過構建器建立。接受 QueryOptions 的方法強制要求非 null 物件,這些物件可以透過靜態 empty() 工廠方法獲得。以下示例展示瞭如何使用 QueryOptions.builder()

QueryOptions queryOptions = QueryOptions.builder()
		.consistencyLevel(ConsistencyLevel.ANY)
		.retryPolicy(FallthroughRetryPolicy.INSTANCE)
		.readTimeout(Duration.ofSeconds(10))
		.fetchSize(10)
		.tracing(true)
		.build();

CassandraPersistentProperty 重構為單列

此變更僅影響直接操作對映模型的使用者。

CassandraPersistentProperty 之前允許繫結多個列名用於複合主鍵。CassandraPersistentProperty 的列現在減少到單列。解析後的複合主鍵透過 MappingContext.getRequiredPersistentEntity(…)` 對映到一個類。