Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | Couchbase | Couchbase Server 2.0

Using Reduce Functions - Help!!!

4 replies [Last post]
  • Login or register to post comments
Mon, 09/12/2011 - 09:53
adamdavies
Offline
Joined: 06/02/2011
Groups: None

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

Top
  • Login or register to post comments
Mon, 09/12/2011 - 12:50
mikew
Offline
Joined: 03/14/2011
Groups:

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);

Top
  • Login or register to post comments
Tue, 09/13/2011 - 01:34
adamdavies
Offline
Joined: 06/02/2011
Groups: None

Thanks you very much Mikew! That worked a treet.

Top
  • Login or register to post comments
Tue, 09/20/2011 - 08:56
perry
Offline
Joined: 10/11/2010
Groups:

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

__________________

Forum support is great for free but sometimes you need a guaranteed response time and dedicated resources for your questions or issues.
Consider purchasing enterprise-level support from Couchbase: http://www.couchbase.com/products-and-services/overview
Call or email "sales -at- couchbase-dot- com" today!

Top
  • Login or register to post comments
Tue, 09/20/2011 - 09:58
mikew
Offline
Joined: 03/14/2011
Groups:

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.

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker