Mouna
Mouna

Reputation: 3359

Achilles Cassandra counter always null

I use Achilles library to do object mapping in Java. Achilles support Cassandra counters (link), but the problem is that when I do a select query the value of the field of type Counter is null.

Here is my model :

@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

}

The query is :

     List<WatchsPerSegment> watchs = manager
     .sliceQuery(WatchsPerSegment.class).forSelect()
     .withPartitionComponents("253")
     .fromClusterings(new Date(2014, 11, 07), true).get();

I printed the result and all counters were null:

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]

Is it a bug of Achilles or is there a problem with my model?

Upvotes: 1

Views: 364

Answers (1)

Wade H
Wade H

Reputation: 3219

You need to invoke CQL UPDATE command to enable counter data.

CQL counter is a special column type. To make the counter work, you need to follow certain rules. This is a pseudo code....

//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

Upvotes: 1

Related Questions