Increment counter table

87 views
Skip to first unread message

Greg Saylor

unread,
Aug 26, 2014, 3:05:36 PM8/26/14
to ceq...@googlegroups.com
Hello,

I'm at a bit of a loss on how to implement an increment to a counter table using cequel.  The code shows this as an example:


# Increment one or more counter columns
#
# @param deltas [Hash<Symbol,Integer>] map of counter column names to
# amount by which to increment each column
# @return [void]
#
# @example
# post_analytics.
# where(blog_subdomain: 'cassandra', permalink: 'cequel').
# increment(pageviews: 10, tweets: 2)
#
# @note This can only be used on counter tables
# @since 0.5.0
# @see #decrement
# @see http://cassandra.apache.org/doc/cql3/CQL.html#counters
# CQL documentation for counter columns
#
def increment(deltas, options = {})
incrementer { increment(deltas) }.execute(options)
end

But alas, when I call:

Stat.where(hash_of_unique_keys).increment(count: count)

I get:

NoMethodError: undefined method `increment' for []:Cequel::Record::RecordSet


<off topic point>
Side bar: I've noticed similar behavior on methods like "destroy" and had to do things like:

 EventLogCounter.where(hash_of_unique_keys).first.destroy 

vs what I think I am reading in the documentation should have been:


# posts.where(blog_subdomain: 'cassandra', permalink: 'cequel').
# delete(:body)
#
</off topic point>

There's probably just something really simple I am missing here… 


- Greg

Matthew A. Brown

unread,
Aug 26, 2014, 9:03:28 PM8/26/14
to Greg Saylor, ceq...@googlegroups.com
Hi Greg,

I think in both cases you're confusing the Metal layer—which simply provides Ruby-ish syntactic sugar on top of raw CQL, without any object mapping or knowledge of your schema—with the Record layer, which provides a more standard ActiveRecord-like ORM.

Unfortunately, Record does not currently have any explicit support for counter columns—I started working on that a while ago and made some progress, but it's quite complicated because counter columns can't comingle with regular data columns in the same table, meaning each Record class would need to be backed by two different tables (assuming it has both counter and non-counter columns). At the time there didn't seem to be much demand for counter support at the Record level, so I decided to focus on other things, but I'd definitely be willing to revisit it if folks would find it useful.

Records can be deleted without reading them; you'd just want to do:

EventLogCounter[key, values, in, order].destroy

Hope that helps!
Mat


--
You received this message because you are subscribed to the Google Groups "Cequel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cequel+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Saylor

unread,
Aug 27, 2014, 2:31:01 PM8/27/14
to ceq...@googlegroups.com, gr...@nexgate.com
I guess I can't figure out how one would execute the lower-level increment call.   I don't mind a method inside my ActiveRecord class that does some extra work behind the scenes to make use of the lower-level stuff, but I just can't seem to pull all the pieces together.

Any ideas/examples of that?  Maybe just something generic about how to access Metal layer methods from within an ActiveRecord object to make use of Cassandra-specific features such as this?

I setup the ActiveRecord table with :counter types and it seemed to be able to generate the schema fine.  Though in my case I am only using it for counters, so I'm not worried about non-key columns other then those.

- Greg

Praateek Mahajan

unread,
Jul 23, 2016, 11:37:14 AM7/23/16
to Cequel, gr...@nexgate.com
Almost after two years, it's the same issue I am stuck with. I have a counter table, and cannot find a way to increment the counter. The clash between Metal and Record still remains, atleast for me as I can't put all parts together.

Any updates?

Peter Williams

unread,
Jul 24, 2016, 3:40:35 PM7/24/16
to Praateek Mahajan, Cequel, gr...@nexgate.com
I think Cequel still does not support counter tables, unfortunately. I usually resort to using something like `Cequel::Record.connection.client.execute_async(batch_query)`. This is not great but it does work.

If someone put a PR together to implement proper counter table support i would merge it with a joyful heart. :)

Peter

Reply all
Reply to author
Forward
0 new messages