我使用Achilles库在Java中进行对象映射。 Achilles支持Cassandra计数器(link),但问题是当我执行select查询时,Counter类型字段的值为null。
这是我的模特:
@Entity(table = "watchs_per_segment")
public class WatchsPerSegment {
@EmbeddedId
private Key key;
@Column
private Counter morning;
@Column
private Counter noon;
@Column
private Counter afternoon;
@Column
private Counter accesspt;
@Column
private Counter night;
public WatchsPerSegment()
{
}
public WatchsPerSegment(Key key, Counter morning, Counter noon,
Counter afternoon, Counter accesspt, Counter night) {
this.key = key;
this.morning = morning;
this.noon = noon;
this.afternoon = afternoon;
this.accesspt = accesspt;
this.night = night;
}
//getters and setters
public static class Key {
@PartitionKey
private String segment;
@ClusteringColumn(value = 1, reversed = true)
private Date day;
@ClusteringColumn(2)
private boolean afterreco;
public Key() {
}
public Key(String segment, Date day, boolean afterreco) {
this.segment = segment;
this.day = day;
this.afterreco = afterreco;
}
//getter and setter
}
查询是:
List<WatchsPerSegment> watchs = manager
.sliceQuery(WatchsPerSegment.class).forSelect()
.withPartitionComponents("253")
.fromClusterings(new Date(2014, 11, 07), true).get();
我打印了结果,所有计数器都为空:
WatchsPerSegment [key=Key [segment=253, day=Thu Nov 07 00:00:00 CET 2014, afterreco=true], morning=null, noon=null, afternoon=null, accesspt=null, night=null]
这是Achilles的错误还是我的模型有问题?
答案 0 :(得分:0)
您需要调用CQL UPDATE命令来启用计数器数据。
CQL计数器是一种特殊的列类型。要使计数器工作,您需要遵循某些规则。这是伪代码......
//构造关键对象 Key key = new Key();
//使用密钥obejct更新记录 //这个键是新的,这没关系。 C *将会记录一条记录 WatchsPerSegment countRec = cqlDao.findReference(WatchsPerSegment.class,key); countRec.getNoon()INCR(); //增加正午计数器 cqlDao.update(countRec); //这是upsert
//现在你可以拿到柜台了 WatchsPerSegment counter = cqlDao.find(WatchsPerSegment.class,key); Long id = counter.getNoon()。get(); // id是计数器值