|
For the latest stable version, please use Spring Data Cassandra 4.5.2! |
Migration Guide from 2.x to 3.x
Spring Data for Apache Cassandra 3.0 introduces a set of breaking changes when upgrading from earlier versions.
Review dependencies
Upgrading to Spring Data Cassandra requires an upgrade to the DataStax Driver version 4. Upgrading to the new driver comes with transitive dependency changes, most notably, Google Guava is bundled and shaded by the driver. Check out the DataStax Java Driver for Apache Cassandra 4 Upgrade Guide for details on the Driver-related changes.
Adapt Configuration
DataStax Java Driver 4 merges Cluster and Session objects into a single CqlSession object, therefore, all Cluster-related API was removed.
The configuration was revised in large parts by removing most configuration items that were moved into DriverConfigLoader that is mostly file-based.
This means that SocketOptions, AddressTranslator and many more options are configured now through other means.
If you’re using XML-based configuration, make sure to migrate all configuration files from the cql namespace (www.springframework.org/schema/cql www.springframework.org/schema/cql/spring-cql.xsd) to the cassandra namespace (www.springframework.org/schema/data/cassandra www.springframework.org/schema/data/cassandra/spring-cassandra.xsd).
To reflect the change in configuration builders, ClusterBuilderConfigurer was renamed to SessionBuilderConfigurer accepting now CqlSessionBuilder instead of the Cluster.Builder.
Make sure to also provide the local data center in your configuration as it is required to properly configure load balancing.
Connectivity
The configuration elements for Cluster (cassandra:cluster) and Session (cassandra:session) were merged into a single CqlSession (cassandra:session) element that configures both, the keyspace and endpoints.
With the upgrade, schema support was moved to a new namespace element: cassandra:session-factory that provides a SessionFactory bean.
<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>
<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>
| Spring Data Cassandra 3.0 no longer registers default Mapping Context, Context and Template API beans when using XML namespace configuration. The defaulting should be applied on application or Spring Boot level. |
Template API
Spring Data for Apache Cassandra encapsulates most of the changes that come with the driver upgrade as the Template API and repository support if your application mainly interacts with mapped entities or primitive Java types.
We generally recommend to create CqlTemplate and CassandraTemplate objects by using SessionFactory as the factory usage allows synchronization for schema creation and introduces a level of flexibility when working with multiple databases.
<cql:template session-ref="…" />
<cassandra:template session-ref="…" cassandra-converter-ref="…"/>
<cassandra:session-factory />
<cassandra:cql-template session-factory-ref="…" />
<cassandra:template session-factory-ref="…" cassandra-converter-ref="…"/>
You will have to adapt your code in all places, where you use DataStax driver API directly. Typical cases include:
-
Implementations of
ResultSetExtractor -
Implementations of
RowCallbackHandler -
Implementations of
RowMapper -
Implementations of
PreparedStatementCreatorincluding async and reactive variants -
Calls to
CqlTemplate.queryForResultSet(…) -
Calling methods that accept
Statement
Changes in AsyncCqlTemplate
DataStax driver 4 has changed the result type of queries that are run asynchronously. To reflect these changes, you need to adapt your code that provides:
-
Implementations of
AsyncSessionCallback -
Implementations of
AsyncPreparedStatementCreator
Result set extraction requires a new interface for DataStax' AsyncResultSet.
AsyncCqlTemplate now uses AsyncResultSetExtractor in places where it used previously ResultSetExtractor.
Note that AsyncResultSetExtractor.extractData(…) returns a Future instead of a scalar object so a migration of code comes with the possibility to use fully non-blocking code in the extractor.
Data model migrations
Your data model may require updates if you use the following features:
-
@CassandraType -
forceQuotein@Table,@Column,@PrimaryKeyColumn,@PrimaryKeyand@UserDefinedType -
Properties using
java.lang.Date -
Properties using
UDTValueorTupleValue
@CassandraType
DataStax driver 4 no longer ships with a Name enumeration to describe the Cassandra type.
We decided to re-introduce the enumeration with CassandraType.Name.
Make sure to update your imports to use the newly introduced replacement type.
Other changes
-
Driver’s
ConsistencyLevelconstant class was removed and reintroduced asDefaultConsistencyLevel.@Consistencywas adapted toDefaultConsistencyLevel. -
RetryPolicyonQueryOptionsand…CqlTemplatetypes was removed without replacement. -
Drivers’s
PagingStatetype was removed. Paging state now usesByteBuffer. -
SimpleUserTypeResolveracceptsCqlSessioninstead ofCluster. -
SimpleTupleTypeFactorywas migrated toenum.SimpleTupleTypeFactory.INSTANCEno longer requires aCluster/CqlSessioncontext. -
Introduction of
StatementBuilderto functionally build statements as the QueryBuilder API uses immutable statement types. -
Sessionbean renamed fromsessiontocassandraSessionandSessionFactorybean renamed fromsessionFactorytocassandraSessionFactory. -
ReactiveSessionbean renamed fromreactiveSessiontoreactiveCassandraSessionandReactiveSessionFactorybean renamed fromreactiveSessionFactorytoreactiveCassandraSessionFactory. -
ReactiveSessionFactory.getSession()now returns aMono<ReactiveSession>. Previously it returned justReactiveSession. -
Data type resolution was moved into
ColumnTypeResolverso allDataType-related methods were moved fromCassandraPersistentEntity/CassandraPersistentPropertyintoColumnTypeResolver(affected methods areMappingContext.getDataType(…),CassandraPersistentProperty.getDataType(),CassandraPersistentEntity.getUserType(), andCassandraPersistentEntity.getTupleType()). -
Schema creation was moved from
MappingContexttoSchemaFactory(affected methods areCassandraMappingContext.getCreateTableSpecificationFor(…),CassandraMappingContext.getCreateIndexSpecificationsFor(…), andCassandraMappingContext.getCreateUserTypeSpecificationFor(…)).
Deprecations
-
CassandraCqlSessionFactoryBean, useCqlSessionFactoryBeaninstead. -
KeyspaceIdentifierandCqlIdentifier, usecom.datastax.oss.driver.api.core.CqlIdentifierinstead. -
CassandraSessionFactoryBean, useCqlSessionFactoryBeaninstead. -
AbstractCqlTemplateConfiguration, useAbstractSessionConfigurationinstead. -
AbstractSessionConfiguration.getClusterName(), useAbstractSessionConfiguration.getSessionName()instead. -
CodecRegistryTupleTypeFactory, useSimpleTupleTypeFactoryinstead. -
Spring Data’s
CqlIdentifier, use the driverCqlIdentifierinstead. -
forceQuoteattributes as quoting is no longer required.CqlIdentifierproperly escapes reserved keywords and takes care of case-sensitivity. -
fetchSizeonQueryOptionsand…CqlTemplatetypes was deprecated, usepageSizeinstead -
CassandraMappingContext.setUserTypeResolver(…),CassandraMappingContext.setCodecRegistry(…), andCassandraMappingContext.setCustomConversions(…): Configure these properties onCassandraConverter. -
TupleTypeFactoryandCassandraMappingContext.setTupleTypeFactory(…):TupleTypeFactoryis no longer used as the Cassandra driver ships with aDataTypes.tupleOf(…)factory method. -
Schema creation via
CqlSessionFactoryBean(cassandra:session) is deprecated. Keyspace creation viaCqlSessionFactoryBean(cassandra:session) is not affected.
Removals
Configuration API
-
PoolingOptionsFactoryBean -
SocketOptionsFactoryBean -
CassandraClusterFactoryBean -
CassandraClusterParser -
CassandraCqlClusterFactoryBean -
CassandraCqlClusterParser -
CassandraCqlSessionParser -
AbstractClusterConfiguration -
ClusterBuilderConfigurer(useSessionBuilderConfigurerinstead
Utilities
-
GuavaListenableFutureAdapter -
QueryOptionsandWriteOptionsconstructor takingConsistencyLevelandRetryPolicyarguments. Use the builder in conjunction of execution profiles as replacement. -
CassandraAccessor.setRetryPolicy(…)andReactiveCqlTemplate.setRetryPolicy(…)methods. Use execution profiles as replacement.
Namespace support
-
cqlnamespace (www.springframework.org/schema/cql, usewww.springframework.org/schema/data/cassandrainstead) -
cassandra:cluster(endpoint properties merged tocassandra:session) -
cql:template, usecassandra:cql-templateinstead -
Removed implicit bean registrations Mapping Context, Context and Template API beans. These must be declared explicitly.