我有以下用户信息对象映射到我的Keyspace" data_collection"中的表" user_info"。我在Cassandra数据库中创建了" User_info"表。我正在使用弹簧数据Cassandra与Java的Cassandra数据库连接,以及下面的弹簧注释。
@Table(name="user_info",keyspace="data_collection", caseSensitiveKeyspace = false,caseSensitiveTable = false)
public class UserInfo {
@PartitionKey
private UUID id;
@PrimaryKeyColumn
private String email;
private int phone;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
我正在使用以下代码将记录插入我的" user_info"表中。
@Autowired
CassandraTemplate cassandraTemplate;
public void saveUserInfo(UserInfo userInfo){
logger.debug("userInfo "+new Gson().toJson(userInfo));
String email = userInfo.getEmail();
Select select = QueryBuilder.select().from("user_info");
select.where(QueryBuilder.eq("email", email));
logger.debug("Query "+select.toString());
UserInfo existingUser = cassandraTemplate.selectOne(select, UserInfo.class);
if(existingUser!=null){
cassandraTemplate.update(userInfo);
}
else{
cassandraTemplate.insert(userInfo);
}
}
我的Selectone工作正常工作,而在插入过程中,我会得到以下例外。我清楚地将UserInfo.java类映射到了表名称为" user_info",使用上面的注释。我不知道为什么插入插件试图发生在" UserInfo"表中。
org.springframework.cassandra.support.exception.CassandraInvalidQueryException: unconfigured columnfamily userinfo; nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured columnfamily userinfo
at org.springframework.cassandra.support.CassandraExceptionTranslator.translateExceptionIfPossible(CassandraExceptionTranslator.java:128)
at org.springframework.cassandra.core.CqlTemplate.potentiallyConvertRuntimeException(CqlTemplate.java:946)
at org.springframework.cassandra.core.CqlTemplate.translateExceptionIfPossible(CqlTemplate.java:930)
at org.springframework.cassandra.core.CqlTemplate.translateExceptionIfPossible(CqlTemplate.java:912)
at org.springframework.cassandra.core.CqlTemplate.doExecute(CqlTemplate.java:278)
at org.springframework.cassandra.core.CqlTemplate.doExecute(CqlTemplate.java:559)
at org.springframework.cassandra.core.CqlTemplate.execute(CqlTemplate.java:1333)
at org.springframework.data.cassandra.core.CassandraTemplate.doUpdate(CassandraTemplate.java:895)
at org.springframework.data.cassandra.core.CassandraTemplate.update(CassandraTemplate.java:537)
at org.springframework.data.cassandra.core.CassandraTemplate.update(CassandraTemplate.java:532)
请在Cassandra中的表格描述下面找到。
CREATE TABLE user_info (
name text,
email text,
phone int
PRIMARY KEY ((email))
) WITH
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.100000 AND
gc_grace_seconds=864000 AND
index_interval=128 AND
read_repair_chance=0.000000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
default_time_to_live=0 AND
speculative_retry='99.0PERCENTILE' AND
memtable_flush_period_in_ms=0 AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'LZ4Compressor'};
快速更新:我刚刚尝试保存另一个类Test.java。它被映射到表" test_info"表。我有以下错误
org.springframework.cassandra.support.exception.CassandraInvalidQueryException: unconfigured columnfamily test; nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured columnfamily test
at org.springframework.cassandra.support.CassandraExceptionTranslator.translateExceptionIfPossible(CassandraExceptionTranslator.java:128)
at org.springframework.cassandra.core.CqlTemplate.potentiallyConvertRuntimeException(CqlTemplate.java:946)
at org.springframework.cassandra.core.CqlTemplate.translateExceptionIfPossible(CqlTemplate.java:930)
at org.springframework.cassandra.core.CqlTemplate.translateExceptionIfPossible(CqlTemplate.java:912)
at org.springframework.cassandra.core.CqlTemplate.doExecute(CqlTemplate.java:278)
at org.springframework.cassandra.core.CqlTemplate.doExecute(CqlTemplate.java:559)
at org.springframework.cassandra.core.CqlTemplate.execute(CqlTemplate.java:1323)
at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:708)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:290)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:285)
我只是想知道我的Java班级名称和Cassandra中的表名是否应该始终相同。因为它在@Table注释中指定的列而不是" test_info"列"测试"。
以下是我的键空间的描述
CREATE KEYSPACE data_collection WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '3'
};
编辑 - 已解决:我根据与@pinkpanther的对话找到了解决方案。我曾导入com.datastax.driver.mapping.annotations.table而不是org.springframework.data.cassandra.mapping.table,这就是为什么它不符合表名称映射的原因。感谢您的帮助。
问题可能在于表的套件导入,弹簧数据cassandra需要org.springframework.data.cassandra.mapping.Table
。用它替换导入的com.datastax.driver.mapping.annotations.Table
。