java - 如何在 Java 中使用 Cassandra Map 类型
问题描述
我已经在键空间中创建了表,例如
CREATE TABLE IF NOT EXISTS myks.users (user_id text, device_ids map<text,text>);
I'm trying to insert the below data using `spring-boot-starter-data-cassandra` library in Spring boot
{
"userId" : "krishna",
"deviceIds": {
"DeviceId_0": "TYNLSANID7",
"DeviceId_1": "julasjasd8",
"DeviceId_2": "iu89074hasd",
"DeviceId_3": "Lenovo hjyas|asdqnkasd"
}
}
下面是实体类
@Column("user_id")
@CassandraType(type = DataType.Name.TEXT)
private String userId;
@Column("device_ids")
@CassandraType(type = DataType.Name.MAP)
private Map<String, String> deviceIds= new HashMap<>();
将数据插入表时出现以下异常
org.springframework.dao.InvalidDataAccessApiUsageException: Expected [2] type arguments for property ['deviceIds'] of type ['interface java.util.Map'] in entity [com.kri.entity.Users]; actual was [0]
解决方案
如文档中所述,当您使用 时,您应该typeArguments
在注释定义中提供属性Collection
,特别是对于Map
数据类型,您应该提供两个DataType Name
属性,一个用于键,一个用于值:
@CassandraType(type = DataType.Name.MAP,typeArguments = { DataType.Name.TEXT,DataType.Name.TEXT}
推荐阅读
- macos-big-sur - AndroidStudio 在 macOS Big Sur 测试版上崩溃
- functional-programming - 如何正确使用 |> 运算符?
- ios - Apple 证书,配置文件
- bash - 无法找到可执行文件:Azure Devops Pipeline 上的简单脚本出现“bash”错误
- python - 在pygame中更改显示模式而不关闭游戏
- node.js - 如何使用 joi 库比较两个字符串的相同值
- ruby-on-rails - 正则表达式使用 ruby 从文本中提取段落
- python - 根据日期过滤 Panda
- javascript - 使用 TypeORM 选择内部 JSON 字段
- spring - okhttp3 使用 http2 进行压力测试后内存高