Cassandra custom type mapper

时间:2016-02-12 20:03:43

标签: java cassandra

I am trying to use mapping annotations of Cassandra-Java-driver. I have an object of

@Table(keyspace = "ks", name = "logs")
public Log() {
    @PartitionKey
    private String source;
    @ClusteringColumn
    private long timestamp;
    private String message;
}

Business logic is written according to this. The problem is I need to bound growth of partitions. So, I decided to use part of timestamp in the partition key. The new primary key will be ((source, date), timestamp).

Can I do this without adding a new field to the Log class? I can change annotations of the 'Log' class or add any other intermediary class.

1 个答案:

答案 0 :(得分:2)

The new primary key will be ((source, date), timestamp).

Easy

@Table(keyspace = "ks", name = "logs")
public Log() {
    @PartitionKey(0)
    private String source;

    @PartitionKey(1)
    private long date;

    @ClusteringColumn
    private long timestamp;

    private String message;
}

Just create a new date field which is extracted from timestamp field and define the partition key order on the @PartitionKey annotation