Using Reduce Functions - Help!!!
The following code accesses a view. But how do I get the results of a reduce function?
View view = couchbaseClient.getView(DOCUMENT_NAME, "temp");
Query query = new Query();
query.setKey("my-key");
ViewResponse response = couchbaseClient.query(view, query);
for (ViewRow rowWithDocs : response) {
String doc = (String) rowWithDocs.getDocument();
}If I have (in the Couchbase console), a map function such as:
function (doc) {
emit(doc._id, doc);
}and run it. I get a list of key/value documents. If I then add the
_count reduce function to the view. I get a value (62), but no key!
Does anyone know if there is any magic that can be used to get the value?
ALSO:
The reason why I am doing the above is because I want to create a short unique ID (kind of like SELECT myseq.NEXTVAL FROM dual; in SQL).
I first started with:
couchbaseClient.set("COUNTER", 10000, 1);
...
long value = couchbaseClient.incr("COUNTER", 1);Except value was always -1. In otherwords it didn't work. And could not find out why. So if anyone can help with this problem I'll be most greatful.
Thanks
Adam
Thanks you very much Mikew! That worked a treet.
Adam, for your reduce function question, when a reduce function is defined, the view will use it by default. You can turn this off by setting reduce=false. I'll have to check on the syntax of doing this within the Java client for you.
Perry
The Query object has a way to define all of the parameters a query can possible take. For setting reduce=false you do this:
Query query = new Query(); query.setReduce(false);
And then put that query into one of your view calls. I also recommend taking a look at the Query class since you will be able to see all of the options you have for specifying how a query will act.
One a side note, we are in the process of updating our documentation and we will try to get stuff about the Query class included.
I'll let someone else answer your first question since I'm not the great with reduce functions, but the issue with you set/incr question is that you didn't put 1 in quotes when you did the set. What happened here is that the 1 was serialized IntegerTranscoder and sent to memcached. When you tried to do the incr you weren't incrementing what you thought you were. See my post on stackoverflow for more info, http://stackoverflow.com/questions/5928964/spymemcached-get-and-incr-met....
Try this:
couchbaseClient.set("COUNTER", 10000, "1");
...
long value = couchbaseClient.incr("COUNTER", 1);