我正在尝试使用 Mapper 访问我存储在 Cassandra 中的元组数据结构。但是,我做不到。我在网上没有找到任何示例。
这是我创建的表和数据。
cqlsh:test> CREATE TABLE test.test_nested (id varchar PRIMARY KEY, address_mapping list<frozen<tuple<text,text>>>);
cqlsh:test> INSERT INTO test.test_nested (id, address_mapping) VALUES ('12345', [('Adress 1', 'pin1'), ('Adress 2', 'pin2')]);
cqlsh:test>
cqlsh:test> select * from test.test_nested;
id | address_mapping
-------+----------------------------------------------
12345 | [('Adress 1', 'pin1'), ('Adress 2', 'pin2')]
(1 rows)
我的映射类(使用 lombok 作为构建器、getter、setter):
@Builder
@Table(keyspace = "test", name = "test_nested")
public class TestNested {
@PartitionKey
@Column(name = "id")
@Getter
@Setter
private String id;
@Column(name = "address_mapping")
@Frozen
@Getter
@Setter
private List<Object> address_mapping;
}
我的映射器类:公共类 TestNestedStore {
private final Mapper<TestNested> mapper;
public TestNestedStore(Mapper<TestNested> mapper) {
this.mapper = mapper;
}
public void insert(TestNested userDropData) {
mapper.save(userDropData);
}
public void remove(String id) {
mapper.delete(id);
}
public TestNested findByUserId(String id) {
return mapper.get(id);
}
public ListenableFuture<TestNested> findByUserIdAsync(String id) {
return mapper.getAsync(id);
}
}
我正在尝试在测试方法中访问数据,如下所示:
@Test
public void testConnection2(){
MappingManager manager = new MappingManager(scyllaDBConnector.getSession());
Mapper<TestNested> mapper = manager.mapper(TestNested.class);
TestNestedStore testNestedStore = new TestNestedStore(mapper);
ListenableFuture<TestNested> fut = testNestedStore.findByUserIdAsync("12345");
Futures.addCallback(fut, new FutureCallback<TestNested>() {
@Override
public void onSuccess(TestNested testNested) {
}
@Override
public void onFailure(Throwable throwable) {
System.out.println("Call failed");
}
});
}
位,我无法访问元组。我收到此错误:
java.lang.IllegalArgumentException: Error while checking frozen types on field address_mapping of entity com.example.model.TestNested: expected List to be not frozen but was frozen
at com.datastax.driver.mapping.AnnotationChecks.validateAnnotations(AnnotationChecks.java:73)
at com.datastax.driver.mapping.AnnotationParser.parseEntity(AnnotationParser.java:81)
at com.datastax.driver.mapping.MappingManager.getMapper(MappingManager.java:148)
at com.datastax.driver.mapping.MappingManager.mapper(MappingManager.java:105)
我也试过了private List<TupleValue> address_mapping;
。但是没用!
如何通过 cassandra 的对象映射器访问元组值?