skp*_*ptl 5 spring spring-data-cassandra
我想创建如下模型,如何在spring-data-cassandra中使用用户定义的类型?
{
email: "test@example.com",
name: {
fname: "First",
lname: "Last"
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
Spring Data Cassandra现在支持用户定义的数据类型.最新版本1.5.0.RELEASE使用Cassandra Data stax驱动程序3.1.3,因此它现在正在运行.按照以下步骤使其正常工作
如何在Spring Data Cassandra中使用UserDefinedType(UDT)功能:
我们需要使用最新的Spring数据罐Cassandra(1.5.0.RELEASE)
group:'org.springframework.data',name:'spring-data-cassandra',version:'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
);
Run Code Online (Sandbox Code Playgroud)
员工表:
CREATE TABLE employee(
employee_id uuid,
employee_name text,
address frozen,
primary key (employee_id, employee_name)
);
Run Code Online (Sandbox Code Playgroud)
在域类中,使用注释-CassandraType定义字段,DataType应为UDT:
@Table("employee")
public class Employee {
-- othere fields--
@CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")
private Address address;
}
Run Code Online (Sandbox Code Playgroud)为用户定义的类型创建域类:我们需要确保用户定义的类型模式中的列名必须与域类中的字段名相同.
@UserDefinedType("address_type")
public class Address {
@CassandraType(type = DataType.Name.TEXT)
private String id;
@CassandraType(type = DataType.Name.TEXT)
private String address_type;
}
Run Code Online (Sandbox Code Playgroud)在Cassandra配置中,更改此:
@Bean public CassandraMappingContext mappingContext() throws Exception {
BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext();
mappingContext.setUserTypeResolver(new
SimpleUserTypeResolver(cluster().getObject(), cassandraKeyspace));
return mappingContext;
}
Run Code Online (Sandbox Code Playgroud)用户定义的类型应该在任何地方具有相同的名称.例如
@UserDefinedType("address_type")
@CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")
CREATE TYPE address_type
Run Code Online (Sandbox Code Playgroud)Spring Data Cassandra目前不支持SD Cassandra 映射基础设施中的Cassandra 数据库UDT或元组类型。但是,我们确实计划在即将发布的版本中解决这两个问题。
请参阅并遵循DATACASS-284 - 添加对 TupleType/TupleValue 的支持和DATACASS-172 - 如何处理自定义、用户定义的类型以了解更多详细信息。
笔记...
SD Cassandra 最近被转移到托管/维护的 SD 项目的 Pivotal 保护伞下,我和@mp911de现在都是项目负责人。
我们最近在 SpringOne Platform 2016 上介绍了SD Cassandra,其中概述了该项目的路线图(幻灯片 11,第 3 个项目符号)。
归档时间: |
|
查看次数: |
5323 次 |
最近记录: |