简体   繁体   中英

How to create bucket of counters in cassandra with composite columns

How can I create a list of counters as a composite column in Cassandra. The problem I am trying to solve is storing count of visits to a particular object. The model in json representation is something like this:

ColumnFamily: view_counts RowKey: object_id

value: { total: 10, referrer: { facebook: 2, twitter: 5, direct: 2, other: 1 } country: { australia: 4, us: 4, other: 2 } }

Thanks in advance

What you want is something like this (where X:Y is a composite column):

ColumnFamily: view_counts
Rows:
object_id -> { "total": 10,
               "referrer:facebook": 2,
               "referrer:twitter": 5,
               "referrer:direct": 2, 
               "referrer:other": 1,
               "country:australia": 4,
               "country:us": 4, 
               "country:other": 2 }

Then when you get a new visit to Object6 via Twitter from Australia, you do the following increments:

incr view_counts[Object6]['total']
incr view_counts[Object6]['referrer:twitter']
incr view_counts[Object6]['country:australia']

There are different ways you could arrange this, of course - you could also put the composites in the row keys:

total:object_id    -> { "count": 10 }
referrer:object_id -> { "facebook": 2,
                        "twitter": 5,
                        "direct": 2, 
                        "other": 1 }
country:object_id  -> { "australia": 4, 
                        "us": 4, 
                        "other": 2 }

or dispense with composite columns altogether and use separate column families for view_counts_by_referrer and view_counts_by_country .

Realtime analytics systems like Acunu Analytics will do all this for you, so you can just specify a query like SELECT COUNT_DISTINCT(user_ip_address) FROM view_counts WHERE object = Object6 AND time > '7 days ago' GROUP BY referrer and it will work out all the appropriate counters behind the scenes.

(ps Supercolumns are deprecated, since you can't modify only part of a supercolumn, which slows things down.)

为什么不只使用带有针对引用者和国家/地区的超级列的超级列族?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM