我試圖用cassandra將用戶定義類型中的對象存儲爲分區鍵。我正在使用datastax java驅動程序進行對象映射,雖然我能夠插入到數據庫中,但我無法檢索該對象。如果我更改分區鍵以使用非udt(例如文本),我可以保存並檢索(即使對象上存在其他相同類型的udt)。從閱讀文檔看來,UDTs被允許作爲鍵。我也一直無法找到任何跡象表明java驅動程序不支持udts作爲鍵。它看起來像在對象映射期間失敗,但只有當UDT是分區鍵時才失敗。使用datastax java驅動程序無法通過cassandra檢索UDT密鑰
這是一個不受支持的功能?使用對象映射器時,是否需要僅使用默認類型作爲鍵?或者我只是做錯了什麼?
下面是我用於建立該數據庫的CQL命令:
create keyspace example_keyspace;
use example_keyspace;
create type my_data_type (value text);
create table my_classes (key frozen<my_data_type>, value frozen<my_data_type>, primary key (key));
這裏是我曾經嘗試插入java代碼和檢索:
package me.example;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.annotations.Frozen;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import com.datastax.driver.mapping.annotations.UDT;
public class Main {
public static void main(String[] args) {
try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1")
.build()) {
Mapper<MyClass> mapper = new MappingManager(cluster.newSession())
.mapper(MyClass.class);
MyDataType value = new MyDataType();
value.setValue("theValue");
MyDataType key = new MyDataType();
key.setValue("theKey");
MyClass myClass = new MyClass();
myClass.setKey(key);
myClass.setValue(value);
mapper.save(myClass);
MyClass toret = mapper.get(key);
System.out.println(toret.getKey());
System.out.println(toret.getValue().getValue());
}
}
@Table(keyspace = "example_keyspace", name = "my_classes")
public static class MyClass {
@PartitionKey
@Frozen
private MyDataType key;
@Frozen
private MyDataType value;
public MyDataType getKey() {
return key;
}
public void setKey(MyDataType key) {
this.key = key;
}
public MyDataType getValue() {
return value;
}
public void setValue(MyDataType value) {
this.value = value;
}
}
@UDT(keyspace = "example_keyspace", name = "my_data_type")
public static class MyDataType {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MyDataType)) {
return false;
}
MyDataType other = (MyDataType) obj;
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}
}
甲選擇顯示對象是插入成功:
select * from my_classes;
收益率:
key | value
-------------------+---------------------
{value: 'theKey'} | {value: 'theValue'}
但我的日食控制檯輸出錯誤:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided
at com.datastax.driver.core.DataType.serialize(DataType.java:619)
at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:320)
at com.datastax.driver.mapping.Mapper.get(Mapper.java:342)
at me.example.Main.main(Main.java:31)
我也使用UDTValue對象爲重點,以檢索對象試過,但我仍然得到同樣的錯誤。
我在OS X 10.10.4上運行cassandra 2.1.7。 Java版本是1.8.0_45。 Datastax Java驅動程序核心和映射器是2.1.6版本。
謝謝!
這工作,謝謝! – njenan