Hung Le
Hung Le

Reputation: 15

Cassandra increment counters with 1 update?

When I update a row with a few counters columns in that row I need to do:

mutator.incrementCounter(rowid1, "cf1", "counter1", value);
mutator.incrementCounter(rowid1, "cf1", "counter2", value);
mutator.incrementCounter(rowid1, "cf1", "counter3", value);

How do I do all these 3 above just in 1 update? Since I don't want to hit the database 3 times. I only want to hit the database 1 time. Also, the reason that I use incrementCounter is to have strong consistency (incrementCounter is more consistent than addCounter). Can you please provide some code for it? Thank you!

Upvotes: 0

Views: 1145

Answers (2)

Gabber
Gabber

Reputation: 7249

if you want hit database only one time then try this

mutator.addCounter(rowid1, "cf1", HFactory.createCounterColumn("counter1", value))
mutator.addCounter(rowid1, "cf1", HFactory.createCounterColumn("counter2", value))
mutator.addCounter(rowid1, "cf1", HFactory.createCounterColumn("counter3", value))
query.execute()

Upvotes: 0

willglynn
willglynn

Reputation: 11520

I asked what research you did so far, because it seems to me that you did not do any. I would normally walk away at this point, but for some reason, I'm choosing to answer anyway.


Let's look up the Mutator documentation. It starts with:

A Mutator inserts or deletes values from the cluster. There are two main ways to use a mutator: 1. Use the insert/delete methods to immediately insert of delete values. or 2. Use the addInsertion/addDeletion methods to schedule batch operations and then execute() all of them in batch. The class is not thread-safe.

The class summary says that Mutator supports performing multiple operations in a single batch, and it points you to add* methods to do it. (You tagged your question with "batch", so you know this is the feature you need.) The very first thing under the class summary is the addCounter() function, described as:

Schedule an increment of a CounterColumn to be inserted in batch mode by Mutator.execute()

Problem solved.

Alternately, you could use your IDE to explore your current code, which works but is inefficient. The one-line summary available on the incrementCounter() calls says that it is a convenience method, strongly suggesting that there are other ways to increment counters. Browsing the Mutator interface even with just your auto-complete would quickly lead you to addCounter(), since there's only a few counter-related functions, and addCounter() is alphabetically first.

A third option, if you prefer to totally ignore documentation, would be to go straight to the incrementCounter() implementation. incrementCounter() is quickly revealed to be addCounter() followed by execute().

Any of these approaches would let you conclude that you could increment multiple counters at once by calling addCounter() multiple times followed by a single execute().


The fourth option, of course, is to come straight to Stack Overflow and ask for code. This will usually get you sent to whathaveyoutried.com, and only occasionally get you answers such as this one explaining in detail all the ways in which you could have answered your own question. I'll end my response now with a snippet from that article:

The problem is that this person’s problem-solving technique is to ask for the solution. Not to seek advice on how to approach the task, or ask for the names of likely classes to look into, or a link to an example - but to just ask for the code, fully formed and ready to go. This is not problem solving, and software engineering is entirely about problem solving.

Upvotes: 1

Related Questions