Creating view via pipe to Python script


#1

Hello. Please pardon me if it seems like I’m really new at Python and Couchbase. It’s because I am. So, there’s a good chance I’m missing something obvious.

I’ve been going through a series of exercises, performing some basic tasks in Python via the Couchbase API. So, I set a value, retrieved it, store images, read views, listing keys, etc. Most recently I created a view via API call. Here’s my script for that:

from couchbase import Couchbase from couchbase.exceptions import CouchbaseError import sys

c = Couchbase.connect(bucket=‘newBucket’, host=‘192.168.0.25’)

DESIGN = {
’_id’: ‘_design/dateextract’,
‘language’: ‘javascript’,
‘views’: {
‘20140714’: {
‘map’:
""“
function (doc, meta) {
if (meta.id.indexOf(‘20140714’) == 0) {
emit(doc.Usage_Value, null);
}
}
”""
}
}
}

results = c.design_create(“20140714”, DESIGN, use_devmode=True, syncwait=0)
print results

You’ll see that I borrowed the DESIGN portion from lines 45-60 here:

http://nullege.com/codes/show/src%40c%40o%40couchbase-1.2.0%40examples%40search_keywords.py/63/couchbase.connection.Connection.design_create/python

So far, so good. That works properly. Now, I’d like to make it so that I can generate a view elsewhere and pipe it into a script which will send it to Couchbase. That has stumped me. As a first step, I took the content of the DESIGN variable and put it into its own file. Then, I rewrote the script as:

from couchbase import Couchbase from couchbase.exceptions import CouchbaseError import sys, getopt

c = Couchbase.connect(bucket=‘newBucket’, host=‘192.168.0.25’)

DESIGN = sys.stdin.readline()

results = c.design_create(“20140714”, DESIGN, use_devmode=False, syncwait=0)
print results

That’s probably pretty naive, and it fails. I tried putting the DESIGN portion on a single line in the file, which also fails:

$ cat design.doc | ./17-filereadCreateView.py
Traceback (most recent call last):
File “./17-filereadCreateView.py”, line 11, in
results = c.design_create(“20140714”, DESIGN, use_devmode=False, syncwait=0)
File “/usr/local/lib/python2.7/dist-packages/couchbase/connection.py”, line 1344, in design_create
fetch_headers=True)
couchbase.exceptions.HTTPError: <Key=’_design/20140714’, HTTP Request failed. Examine ‘objextra’ for full result, C Source=(src/http.c,304), OBJ=HttpResult<RC=0x0, Value={u’reason’: u’invalid UTF-8 JSON: {{error,{100,\n “lexical error: inside a string, ‘\\’ occurs before a character which it may not.\n”}},\n <<"{\"_id\":\"_design/dateextract\",\“language\”:\“javascript\”,\“views\”:{\“20140715\”:{\“map\”:\“function(doc,meta)\\{if(meta.id.indexOf(\\\“20140714\\\”)==0)\\{emit(doc.Usage_Value,null);\\}\\}\”\"\"}}}\n">>}’, u’error’: u’bad_request’}, HTTP=400, URL=_design/20140714>>

I know that this is very bare-bones-- no error handling, etc. I also know that this has limited utility, but there are already a couple of cases here at work where this sort of piping would be useful. So, how can this be made to work? I think my error is in the structure of the design.doc file, but I’m not sure. Many thanks in advance.


#2

I am attempting to build a similar script for automated view deployment and was thinking of doing something similar. Were you able to find a solution to your problem? Did you have to encode your DESIGN to json using another library first?


#3

Knowing what the contents of the file are would be useful. Internally all the library does is encode the JSON into a string anyway: