此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Data Cassandra 4.5.2spring-doc.cadn.net.cn

从 2.x 到 3.x 的迁移指南

Spring Data for Apache Cassandra 3.0 在从早期版本升级时引入了一组重大更改。spring-doc.cadn.net.cn

查看依赖关系

升级到 Spring Data Cassandra 需要升级到 DataStax 驱动程序版本 4。升级到新驱动程序会带来可传递依赖项的变化,最值得注意的是,Google Guava 被驱动程序捆绑和着色。 查看 DataStax Java Driver for Apache Cassandra 4 升级指南,了解有关驱动程序相关更改的详细信息。spring-doc.cadn.net.cn

调整配置

DataStax Java 驱动程序 4 合并ClusterSession对象转换为单个CqlSessionobject,因此,所有Cluster相关的 API 已被删除。 通过删除移至DriverConfigLoader这主要是基于文件的。 这意味着SocketOptions,AddressTranslator现在通过其他方式配置了更多选项。spring-doc.cadn.net.cn

为了反映配置构建器中的更改,ClusterBuilderConfigurer已重命名为SessionBuilderConfigurer现在接受CqlSessionBuilder而不是Cluster.Builder. 确保在配置中还提供本地数据中心,因为正确配置负载平衡需要本地数据中心。spring-doc.cadn.net.cn

连接

的配置元素Cluster (cassandra:cluster) 和Session (cassandra:session) 合并为一个CqlSession (cassandra:session) 元素,用于配置键空间和端点。spring-doc.cadn.net.cn

升级后,架构支持已移至新的命名空间元素:cassandra:session-factory这提供了一个SessionFactory豆。spring-doc.cadn.net.cn

示例 1.版本 2 中的集群、会话和架构配置:
<cassandra:cluster contact-points="localhost" port="9042">
  <cassandra:keyspace action="CREATE_DROP" name="mykeyspace" />
</cassandra:cluster>

<cassandra:session keyspace-name="mykeyspace" schema-action="CREATE">
  <cassandra:startup-cql>CREATE TABLE …</cassandra:startup-cql>
</cassandra:session>
示例 2.版本 3 中的会话和架构配置:
<cassandra:session contact-points="localhost" port="9042" keyspace="mykeyspace" local-datacenter="datacenter1">
  <cassandra:keyspace action="CREATE_DROP" name="mykeyspace" />
</cassandra:session>

<cassandra:session-factory schema-action="CREATE">
  <cassandra:script location="classpath:/schema.cql"/>
</cassandra:session-factory>
使用 XML 命名空间配置时,Spring Data Cassandra 3.0 不再注册默认的 Mapping Context、Context 和 Template API Bean。 默认值应应用于应用程序或 Spring Boot 级别。

模板 API

如果您的应用程序主要与映射的实体或原始 Java 类型交互,则 Spring Data for Apache Cassandra 将驱动程序升级附带的大部分更改封装为模板 API 和存储库支持。spring-doc.cadn.net.cn

我们一般建议创建CqlTemplateCassandraTemplate对象,使用SessionFactory由于工厂使用允许同步创建模式,并在使用多个数据库时引入一定程度的灵活性。spring-doc.cadn.net.cn

示例 3.版本 2 中的模板 API 配置:
<cql:template session-ref="…" />

<cassandra:template session-ref="…" cassandra-converter-ref="…"/>
示例 4.版本 3 中的模板 API 配置:
<cassandra:session-factory />

<cassandra:cql-template session-factory-ref="…" />

<cassandra:template session-factory-ref="…" cassandra-converter-ref="…"/>

您必须在直接使用 DataStax 驱动程序 API 的所有位置调整代码。 典型案例包括:spring-doc.cadn.net.cn

的变化AsyncCqlTemplate

DataStax 驱动程序 4 更改了异步运行的查询的结果类型。 若要反映这些更改,需要调整提供以下内容的代码:spring-doc.cadn.net.cn

结果集提取需要 DataStax 的新接口AsyncResultSet.AsyncCqlTemplate现在使用AsyncResultSetExtractor在以前使用的地方ResultSetExtractor. 请注意AsyncResultSetExtractor.extractData(…)返回一个Future而不是标量对象,因此代码迁移可以在提取器中使用完全非阻塞的代码。spring-doc.cadn.net.cn

数据模型迁移

如果使用以下功能,则数据模型可能需要更新:spring-doc.cadn.net.cn

@CassandraType

DataStax 驱动程序 4 不再附带Name枚举来描述 Cassandra 类型。 我们决定重新引入CassandraType.Name. 确保更新导入以使用新引入的替换类型。spring-doc.cadn.net.cn

强制报价

此标志现已弃用,建议不要再使用它。 Spring Data for Apache Cassandra 在内部使用驱动程序的CqlIdentifier这确保了在需要的地方报价。spring-doc.cadn.net.cn

属性类型

DataStax 驱动程序 4 不再使用java.lang.Date. 请升级您的数据模型以使用java.time.LocalDateTime. 请同时将原始 UDT 和元组类型迁移到新的驱动程序类型UdtValue各自TupleValue.spring-doc.cadn.net.cn

其他变化

  • Drivers的ConsistencyLevel常量类被删除并重新引入为DefaultConsistencyLevel.@Consistency被改编为DefaultConsistencyLevel.spring-doc.cadn.net.cn

  • RetryPolicyQueryOptions…CqlTemplate类型被删除而不替换。spring-doc.cadn.net.cn

  • Drivers的PagingState类型已被删除。 分页状态现在使用ByteBuffer.spring-doc.cadn.net.cn

  • SimpleUserTypeResolver接受CqlSession而不是Cluster.spring-doc.cadn.net.cn

  • SimpleTupleTypeFactory已迁移到enum.SimpleTupleTypeFactory.INSTANCE不再需要Cluster/CqlSession上下文。spring-doc.cadn.net.cn

  • 介绍StatementBuilder在功能上构建语句,因为 QueryBuilder API 使用不可变语句类型。spring-doc.cadn.net.cn

  • Sessionbean 重命名为sessioncassandraSessionSessionFactorybean 重命名为sessionFactorycassandraSessionFactory.spring-doc.cadn.net.cn

  • ReactiveSessionbean 重命名为reactiveSessionreactiveCassandraSessionReactiveSessionFactorybean 重命名为reactiveSessionFactoryreactiveCassandraSessionFactory.spring-doc.cadn.net.cn

  • ReactiveSessionFactory.getSession()现在返回一个Mono<ReactiveSession>. 以前它只返回ReactiveSession.spring-doc.cadn.net.cn

  • 数据类型解析移至ColumnTypeResolver所以所有DataType相关的方法从CassandraPersistentEntity/CassandraPersistentPropertyColumnTypeResolver(受影响的方法是MappingContext.getDataType(…),CassandraPersistentProperty.getDataType(),CassandraPersistentEntity.getUserType()CassandraPersistentEntity.getTupleType()).spring-doc.cadn.net.cn

  • 架构创建已从MappingContextSchemaFactory(受影响的方法是CassandraMappingContext.getCreateTableSpecificationFor(…),CassandraMappingContext.getCreateIndexSpecificationsFor(…)CassandraMappingContext.getCreateUserTypeSpecificationFor(…)).spring-doc.cadn.net.cn

弃用

  • CassandraCqlSessionFactoryBeanCqlSessionFactoryBean相反。spring-doc.cadn.net.cn

  • KeyspaceIdentifierCqlIdentifiercom.datastax.oss.driver.api.core.CqlIdentifier相反。spring-doc.cadn.net.cn

  • CassandraSessionFactoryBeanCqlSessionFactoryBean相反。spring-doc.cadn.net.cn

  • AbstractCqlTemplateConfigurationAbstractSessionConfiguration相反。spring-doc.cadn.net.cn

  • AbstractSessionConfiguration.getClusterName()AbstractSessionConfiguration.getSessionName()相反。spring-doc.cadn.net.cn

  • CodecRegistryTupleTypeFactorySimpleTupleTypeFactory相反。spring-doc.cadn.net.cn

  • Spring Data 的CqlIdentifier,使用驱动程序CqlIdentifier相反。spring-doc.cadn.net.cn

  • forceQuote属性不再需要引用。CqlIdentifier正确转义保留关键字并注意区分大小写。spring-doc.cadn.net.cn

  • fetchSizeQueryOptions…CqlTemplate类型已被弃用,使用pageSize相反spring-doc.cadn.net.cn

  • CassandraMappingContext.setUserTypeResolver(…),CassandraMappingContext.setCodecRegistry(…)CassandraMappingContext.setCustomConversions(…):在CassandraConverter.spring-doc.cadn.net.cn

  • TupleTypeFactoryCassandraMappingContext.setTupleTypeFactory(…):TupleTypeFactory不再使用,因为 Cassandra 驱动程序附带了DataTypes.tupleOf(…)工厂方法。spring-doc.cadn.net.cn

  • 通过CqlSessionFactoryBean (cassandra:session) 已弃用。 通过CqlSessionFactoryBean (cassandra:session) 不受影响。spring-doc.cadn.net.cn

清除

配置 API

Utilities

  • GuavaListenableFutureAdapterspring-doc.cadn.net.cn

  • QueryOptionsWriteOptions构造函数获取ConsistencyLevelRetryPolicy参数。 将构建器与执行配置文件结合使用作为替代品。spring-doc.cadn.net.cn

  • CassandraAccessor.setRetryPolicy(…)ReactiveCqlTemplate.setRetryPolicy(…)方法。 使用执行配置文件作为替换。spring-doc.cadn.net.cn

命名空间支持

增加

配置 API

命名空间支持