Creating a counter table

Steps for defining a counter column, which is a specific kind of column whose user-visible value is a 64-bit signed integer that can be incremented (or decremented) by a client application.

A counter table contains counter columns. A counter column is a specific kind of column whose user-visible value is a 64-bit signed integer that can be incremented (or decremented) by a client application. The counter column tracks the most recent value (or count) of all updates made to it. A counter column cannot be mixed in with regular columns of a table, you must create a table specifically to hold counters.

Procedure

  1. Create a table that holds counter columns by setting the default_validation_class of the table to CounterColumnType.
    [default@demo] CREATE COLUMN FAMILY page_view_counts
                     WITH default_validation_class=CounterColumnType
                     AND key_validation_class=UTF8Type AND comparator=UTF8Type;
  2. Insert a row and counter column into the table (with the initial counter value set to 0).
    [default@demo] INCR page_view_counts['www.datastax.com'][home] BY 0;
  3. Increment the counter.
    [default@demo] INCR page_view_counts['www.datastax.com'][home] BY 1;