【问题标题】:Achilles Cassandra counter always nullAchilles Cassandra 计数器始终为空
【发布时间】:2019-06-27 05:34:48
【问题描述】:

我使用 Achilles 库在 Java 中进行对象映射。 Achilles 支持 Cassandra 计数器 (link),但问题是当我进行选择查询时,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]

这是跟腱的错误还是我的模型有问题?

【问题讨论】:

  • 如果没有看到您的查询、架构或任何详细信息,这将很难回答。
  • 你说得对,我更新了我的问题

标签: java cassandra


【解决方案1】:

您需要调用 CQL UPDATE 命令来启用计数器数据。

CQL 计数器是一种特殊的列类型。要使计数器正常工作,您需要遵循某些规则。这是一个伪代码......

//construct key object
Key key = new Key();

// use the key obejct to update a record
// it is ok that this key is new. C* will upsert a record
WatchsPerSegment countRec=cqlDao.findReference(WatchsPerSegment.class, key);
countRec.getNoon().incr();  // increase noon counter
cqlDao.update(countRec);    // this is upsert

// now you can get the counter
WatchsPerSegment counter = cqlDao.find(WatchsPerSegment.class, key);
Long id=counter.getNoon().get();
// id is the counter value

【讨论】:

    猜你喜欢
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    相关资源
    最近更新 更多