栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用户定义的类型与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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号