首页 文章

在Cassandra的Spring数据中使用复杂的数据结构

提问于
浏览
1

我正在为 Spring 天为Cassandra设置一个DAO .

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

我有这个课程设置:

@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;
}

和 class 用户:

@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;
}

所以我两次使用类User . 一旦作为主键“所有者”,一次作为“updatedBy” .

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

它抱怨已经使用了列名 userId . 说得通 . 那么我怎样才能做到这一点呢?

我可以使用UserDefined类型吗?

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

但是我如何从java Annotations中做到这一点?

或者,我怎样才能重用同一个类呢?考虑到Cassandra中相对简单的数据结构,我可以将User类扁平化为单个字符串,如 idType.id .

谢谢你的帮助!

1 回答

  • 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;
    
    }
    

    很好地工作 .

相关问题