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!
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
Mutatordocumentation. It starts with:The class summary says that
Mutatorsupports performing multiple operations in a single batch, and it points you toadd*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 theaddCounter()function, described as: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 theMutatorinterface even with just your auto-complete would quickly lead you toaddCounter(), since there’s only a few counter-related functions, andaddCounter()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 beaddCounter()followed byexecute().Any of these approaches would let you conclude that you could increment multiple counters at once by calling
addCounter()multiple times followed by a singleexecute().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: