用户定义的类型与spring-data-cassandra

用户定义的类型与spring-data-cassandra,第1张

用户定义类型与spring-data-cassandra

Spring Data Cassandra现在支持用户定义的数据类型。最新版本1.5.0.RELEASE使用Cassandra Data stax驱动程序3.1.3,因此现在可以工作。请按照以下步骤 *** 作

如何在Spring Data Cassandra中使用UserDefinedType(UDT)功能:

  1. 我们需要使用最新版本的Spring数据Cassandra(1.5.0.RELEASE)

组:“ org.springframework.data”,名称:“ spring-data-cassandra”,版本:“ 1.5.0.RELEASE”

  1. 确保使用以下版本的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
  1. 在Cassandra中创建用户定义的类型:类型名称应与POJO类中定义的名称相同

地址数据类型

CREATE TYPE address_type (     id text,     address_type text,     first_name text,     phone text );
  1. 在Cassandra中使用UDT创建列之一,以创建列族:
    员工表:
CREATE TABLE employee(    employee_id uuid,    employee_name text,    address frozen,    primary key (employee_id, employee_name) );
  1. 在域类中,定义带有注释的字段-CassandraType,并且DataType应该为UDT:
@Table("employee") public class Employee {    -- othere fields--    @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")    private Address address; }
  1. 为用户定义的类型创建域类:我们需要确保用户定义的类型架构中的列名必须与域类中的字段名相同。
@UserDefinedType("address_type") public class Address {     @CassandraType(type = DataType.Name.TEXT)     private String id;     @CassandraType(type = DataType.Name.TEXT)     private String address_type; }
  1. 在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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5014345.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-15
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存