Spring Data Cassandra现在支持用户定义的数据类型。最新版本1.5.0.RELEASE使用Cassandra Data stax驱动程序3.1.3,因此现在可以工作。请按照以下步骤操作
如何在Spring Data Cassandra中使用UserDefinedType(UDT)功能:
- 我们需要使用最新版本的Spring数据Cassandra(1.5.0.RELEASE)
组:“ org.springframework.data”,名称:“ spring-data-cassandra”,版本:“ 1.5.0.RELEASE”
- 确保使用以下版本的jar:
datastax.cassandra.driver.version = 3.1.3 spring.data.cassandra.version = 1.5.0.RELEASE spring.data.commons.version = 1.13.0.RELEASE spring.cql.version = 1.5.0.RELEASE
- 在Cassandra中创建用户定义的类型:类型名称应与POJO类中定义的名称相同
地址数据类型
CREATE TYPE address_type ( id text, address_type text, first_name text, phone text );
- 在Cassandra中使用UDT创建列之一,以创建列族:
员工表:
CREATE TABLE employee( employee_id uuid, employee_name text, address frozen, primary key (employee_id, employee_name) );
- 在域类中,定义带有注释的字段-CassandraType,并且DataType应该为UDT:
@Table("employee") public class Employee { -- othere fields-- @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type") private Address address; }- 为用户定义的类型创建域类:我们需要确保用户定义的类型架构中的列名必须与域类中的字段名相同。
@UserDefinedType("address_type") public class Address { @CassandraType(type = DataType.Name.TEXT) private String id; @CassandraType(type = DataType.Name.TEXT) private String address_type; }- 在Cassandra Config中,更改以下内容:
@Bean public CassandraMappingContext mappingContext() throws Exception { BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext(); mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), cassandraKeyspace)); return mappingContext; }用户定义的类型在任何地方都应具有相同的名称。例如
@UserDefinedType("address_type")@CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")CREATE TYPE address_type


