Create view C api

I recommend you to take a look at cbc tool and see how does it create buckets

To save design document you should make PUT request with your design document in JSON format, the command payload will look like this:

const char *docid = “_design/test”;
const char *doc = “{“views”:{“all”:{“map”:“function (doc, meta) { emit(meta.id, null); }”}}}”;
lcb_http_cmd_t cmd;
cmd.version = 0;
cmd.v.v0.path = docid;
cmd.v.v0.npath = strlen(docid);
cmd.v.v0.body = doc;
cmd.v.v0.nbody = strlen(doc);
cmd.v.v0.method = LCB_HTTP_METHOD_PUT;
cmd.v.v0.content_type = “application/json”;
rc = lcb_make_http_request(instance, NULL, LCB_HTTP_TYPE_VIEW, &cmd, NULL);

Also I’ve made update in the man file with the example above, so it will be documented in next releases

http://review.couchbase.org/#/c/24892/

Thanks. I was using the yajl example and it was failing. $ ./couchview -b beer-sample _design/country -d '{ "id" : "_design/country", "language" : "javascript", "views" : { "country" : { "map" : "function(doc) { if (doc.country) { emit(doc.country, 1) } }" } } }' Server: MochiWeb/1.0 (Any of you quaids got a smint?) Date: Wed, 27 Feb 2013 09:46:51 GMT Content-Type: application/json Content-Length: 80 Cache-Control: must-revalidate Allow: DELETE,GET,HEAD,POST,PUT "_design/country": OK { "error": "method_not_allowed", "reason": "Only DELETE,GET,HEAD,POST,PUT allowed" } I changed the POST to PUT and it worked. ./couchview -b beer-sample _design/country -d '{ "id" : "_design/country", "language" : "javascript", "views" : { "country" : { "map" : "function(doc) { if (doc.country) { emit(doc.country, 1) } }" } } }' Server: MochiWeb/1.0 (Any of you quaids got a smint?) Location: http://127.0.0.1:8092/beer-sample/_design/country Date: Wed, 27 Feb 2013 09:50:54 GMT Content-Type: application/json Content-Length: 35 Cache-Control: must-revalidate "_design/country": OK { "ok": true, "id": "_design/country" }

If you have libcouchbase2-bin package installed (it includes cbc CLI tool), you can also use ‘cbc view’ for it. ‘cbc view’ actually re-implementation of couchview but in user-land tool.

cbc view -X PUT -b beer-sample _design/country -d ‘{“id”:"_design/country",“language”:“javascript”,“views”:{“country”:{“map”:“function(doc){ if (doc.country) { emit(doc.country, 1) } }”}}}’

Hi,

Can anyone point me to example code to create views using lcb_make_http_request?
I checked example/yajl/couchview.c but unfortunately I can’t understand how to create views.

Thanks

how to install libcouchbase 2-bin package redhat linux? i cannot find this package thanks!

in the same way how you are installing others. list all libcouchbase packages:

$ yum search libcouchbase
…snip…
libcouchbase-debuginfo.i686 : Debug information for package libcouchbase
libcouchbase-devel.i686 : Couchbase Client & Protocol Library - Header files
libcouchbase1.i686 : Couchbase Client & Protocol Library
libcouchbase1-bin.i686 : Couchbase Client Tools
libcouchbase2.i686 : Couchbase Client & Protocol Library
libcouchbase2-bin.i686 : Couchbase Client Tools
libcouchbase2-core.i686 : Couchbase Client & Protocol Library (core)
libcouchbase2-libev.i686 : Couchbase Client & Protocol Library (libev backend)
libcouchbase2-libevent.i686 : Couchbase Client & Protocol Library (libevent backend)

and then install what you need

$ sudo yum install libcouchbase2-bin

Yes, changing in couchbase.c line 471 to PUT seems to make it work

cmd.v.v0.method = bytes ? LCB_HTTP_METHOD_PUT : LCB_HTTP_METHOD_GET;

Sorry I meant couchview.c