從 1.x 到 2.x 的遷移指南
從早期版本升級時,Spring Data for Apache Cassandra 2.0 引入了一系列重大變更
-
將
spring-cql
和spring-data-cassandra
模組合併為一個模組。 -
將
CqlOperations
和CassandraOperations
中的非同步和同步操作分離到專用的介面和模板中。 -
修訂了
CqlTemplate
API,使其與JdbcTemplate
對齊。 -
移除了
CassandraOperations.selectBySimpleIds
方法。 -
為
CassandraRepository
使用了更好的名稱。 -
移除了 SD Cassandra 的
ConsistencyLevel
和RetryPolicy
型別,轉而使用 DataStax 的ConsistencyLevel
和RetryPolicy
型別。 -
將 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.cql
與o.s.d.cassandra.config
合併,並展平了 XML 和 Java 子包。 -
將
CassandraExceptionTranslator
和CqlExceptionTranslator
移至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
(影響對映註解)。 -
將
MapId
從o.s.d.c.repository
移至o.s.d.c.core.mapping
。
[[revised-cqltemplate/cassandratemplate]] == 修訂 CqlTemplate
/CassandraTemplate
我們將 CqlTemplate
和 CassandraTemplate
分為三個方面
-
CassandraTemplate
不再是CqlTemplate
,而是使用一個例項,該例項允許對 fetch size、一致性級別和重試策略進行復用和細粒度控制。你可以透過CassandraTemplate.getCqlOperations()
獲取CqlOperations
。由於此變更,CqlTemplate
的依賴注入需要額外的 Bean 設定。 -
CqlTemplate
現在反映的是基本的 CQL 操作,而不是混合高階和低階 API 呼叫(例如count(…)` 與 `execute(…)`),並且精簡後的方法集與 Spring Framework 的 `JdbcTemplate` 及其便利的回撥介面對齊。
-
非同步方法已在
AsyncCqlTemplate
和AsyncCassandraTemplate
上使用ListenableFuture
重新實現。我們移除了Cancellable
和各種非同步回撥監聽器。ListenableFuture
是一種靈活的方法,允許轉換為CompletableFuture
。
移除了 CassandraOperations.selectBySimpleIds()
此方法被移除是因為它不支援複雜 ID。新引入的查詢 DSL 允許對單列 ID 使用對映和複雜 ID,如下例所示
cassandraTemplate.select(Query.query(Criteria.where("id").in(…)), Person.class)
為 CassandraRepository
使用更好的名稱
我們重新命名了 CassandraRepository
和 TypedIdCassandraRepository
,以使 Spring Data Cassandra 的命名與其他 Spring Data 模組對齊
-
將
CassandraRepository
重新命名為MapIdCassandraRepository
-
將
TypedIdCassandraRepository
重新命名為CassandraRepository
-
引入了
TypedIdCassandraRepository
,將其作為已棄用型別擴充套件CassandraRepository
以方便遷移
移除了 SD Cassandra 的 ConsistencyLevel
和 RetryPolicy
型別,轉而使用 DataStax 的 ConsistencyLevel
和 RetryPolicy
型別
Spring Data Cassandra 的 ConsistencyLevel
和 RetryPolicy
已被移除。請使用 DataStax 驅動提供的型別。
Spring Data Cassandra 型別限制了 Cassandra 原生驅動中提供的和允許的可用功能的使用。因此,每當驅動引入新功能時,Spring Data Cassandra 的型別都需要更新。
將 CQL 規範重構為值物件和配置器
CQL 規範型別現在儘可能地是值型別(例如 FieldSpecification
、AlterColumnSpecification
),並且物件透過靜態工廠方法構造。這允許簡單的值物件保持不可變性。對強制屬性(例如表名或鍵空間名)進行操作的配置器物件(例如 AlterTableSpecification
)最初透過靜態工廠方法構造,並允許進一步配置,直到建立所需的狀態。
將 QueryOptions
重構為不可變物件
QueryOptions
和 WriteOptions
現在是不可變的,可以透過構建器建立。接受 QueryOptions
的方法強制要求非 null 物件,這些物件可以透過靜態 empty()
工廠方法獲得。以下示例展示瞭如何使用 QueryOptions.builder()
QueryOptions queryOptions = QueryOptions.builder()
.consistencyLevel(ConsistencyLevel.ANY)
.retryPolicy(FallthroughRetryPolicy.INSTANCE)
.readTimeout(Duration.ofSeconds(10))
.fetchSize(10)
.tracing(true)
.build();