在cassandra中使用UDT(用户定义类型)的地图 [英] Working of map with UDT(user defined type) in cassandra
问题描述
假设我在Cassandra和信息UDT中有一个产品表
Let's say I have a product table in Cassandra and Information UDT
Create table Product{
id text,
details map<text,<frozen<Information>>,
detailsMore list<frozen<Information>>
}
Create Type Information{
info1 text,
info2 text
}
对于这些,我在 Java 中有两个类作为
For these, I have two classes in Java as
@Table
Class Product{
String id;
Map<String, Information> details;
List<Information> details;
}
@UserDefinedType('Information')
Class Information{
String info1;
String info2;
}
现在我使用数据提取CRUDRepository(spring)现在我就像
Now I fetch data using CRUDRepository(spring) and now I am like
Product prodObj = repo.find(id);
现在这个prodObj包含像
Now this prodObj contains data like
details map<String,UDTValue>,
detailsMore list<Information>
为什么在List中将用户定义的类型转换为Information,但它不是转换为Map的情况下?或者我做错了什么?。如果我直接尝试将Map的这个UDTValue赋值给Information,我会得到下面的异常(很明显)。
java.lang.ClassCastException:com.datastax.driver.core.UDTValue不能转换为信息
Why user-defined type is converted to Information in case of List but it wasn't converted in case of Map? Or I am doing something wrong?. If I directly try to assign this UDTValue of Map to Information I get the below exception(obvious). java.lang.ClassCastException: com.datastax.driver.core.UDTValue cannot be cast to Information
推荐答案
您需要为您的类编写自定义编解码器,该编解码器会将 UDTValue
映射到您的信息
类中。请参阅 Java驱动程序文档(我不喜欢不想复制他们的代码作为插图)。
You need to write custom codec for your class that will map UDTValue
into your Information
class. Please refer to Java Driver documentation (I don't want to copy their code as illustration).
提供示例后更新:我不熟悉Spring Cassandra Mapper,但是您直接使用 Object Driver for Java Driver 。您需要使用 @UDT
注释标记代表用户定义类型的类,并且驱动程序会照顾它。我已经创建了这个工作示例 - 可以在此处找到(请参阅输出
执行结果的一部分)。
Update after providing example: I'm not familiar with Spring Cassandra Mapper, but you get the same functionality directly using the Object Mapper from Java Driver. You need to mark the class that represents user-defined type with @UDT
annotation, and driver will take care for it. I've created the working example - it's available here (see output
part for result of execution).
这篇关于在cassandra中使用UDT(用户定义类型)的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!