3

我正在尝试使用 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 的对象映射器访问元组值?

4

2 回答 2

2

您定义address_mappinglist<frozen<tuple<text,text>>>,即冻结元组值的列表。要将此传达给MappingManager,您可以使用@FrozenValue属性

TestNested应该看起来像:

@Builder
@Table(keyspace = "test", name = "test_nested")
public class TestNested {
    ...

    @Column(name = "address_mapping")
    @Frozen
    @Getter
    @Setter
    @FrozenValue
    private List<Object> address_mapping;
}
于 2017-10-31T18:53:36.263 回答
1

用于定义cassandra数据类型

map<text,frozen<tuple<text,text,int,text>>>

在java实体类中提到数据类型,

import com.datastax.driver.core.TupleValue;

@FrozenValue

private Map<String,TupleValue> event_driven;
于 2018-06-23T11:56:50.693 回答