1

我正在为春季的Cassandra建立一个DAO。在Cassandra的春季数据中使用复杂的数据结构

现在我有一个关于在一个对象中多次使用复合类的问题。

我有这个类设置:

@Table(value = "settings") 
public class Setting { 

    @PrimaryKey 
    private User owner; 

    @Column("key") 
    private String key; 

    @Column("value") 
    private String value; 

    @Column("updated_by") 
    private User updatedBy; 
} 

和类用户:

@PrimaryKeyClass 
public class User implements Serializable{ 

    @PrimaryKeyColumn(name = "userId", ordinal = 0, type = PrimaryKeyType.PARTITIONED) 
    private String id; 

    @PrimaryKeyColumn(name = "userIdType", ordinal = 1, type = PrimaryKeyType.PARTITIONED) 
    private String idType; 
} 

所以我使用了类用户的两倍。一次作为主要关键“所有者”,一次作为“updatedBy”。

在CassandraOperations类中使用它时,它可以很好地作为主键,但不能再作为另一列。

它抱怨已经在使用的列名称userId。说得通。 那么我该如何做这项工作?

我可以使用UserDefined Types?

CREATE TYPE test_keyspace.user (
    id text, 
    id_type text 
); 

但是我怎样才能做到这一点从Java注释?

或者,我该如何重复使用同一个类呢? 考虑到Cassandra中相对简单的数据结构,我可以将User类展平为一个单独的字符串,如idType.id

感谢您的帮助!

回答

3

好的,发现它here,通过回答denzal

我的类现在看起来像:

@Table("settings") 
public class Setting { 

    @PrimaryKeyColumn(name = "owner", ordinal = 0, type = PrimaryKeyType.PARTITIONED) 
    @CassandraType(type = DataType.Name.UDT, userTypeName = "user_type") 
    private User owner; 

    @PrimaryKeyColumn(name = "key", ordinal = 1, type = PrimaryKeyType.CLUSTERED) 
    @CassandraType(type = DataType.Name.TEXT) 
    private String key; 

    @CassandraType(type = DataType.Name.TEXT) 
    private String value; 

    @CassandraType(type = DataType.Name.UDT, userTypeName = "user_type") 
    private User lastUpdatedBy; 
} 

和用户等级:

@UserDefinedType("user_type") 
public class User implements Serializable{ 

    @CassandraType(type = DataType.Name.TEXT) 
    private String id; 

    @CassandraType(type = DataType.Name.TEXT) 
    private IdType idType; 

} 

很好地工作。

+0

虽然也需要TypeCodec。 [此处为示例](http://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/) – Denno