Best practices for storing couch views

My application has the form couchbase (map-reduce). I am currently writing them in a text file and downloading them for each new couchbase server from the couchbase admin page (a tedious and error-prone process).

Is there anyway I can load all these views from text files into couchbase when I deploy a new couchbase server or when I create a new bucket?

I remember that in mysql we used all the queries and insert procedures in the file and loaded the file into mysql (via the command line) for each new instance. Is there such a strategy for couchbase?

+6
source share
2 answers

From the previous couchbase related questions, do you seem to be using the java SDK? Both 1.4 and 2.0 lines of the SDK allow you to programmatically create design documents and presentations.

With Java SDK 1.4.x

You need to load the view definitions (map functions, reduce the functions in which the project document should be executed), as lines. See the documentation at http://docs.couchbase.com/couchbase-sdk-java-1.4/#design-documents .

Basically you create a ViewDesign in a DesignDocument , which you insert into the database via CouchbaseClient :

 DesignDocument designDoc = new DesignDocument("beers"); designDoc.setView(new ViewDesign("by_name", "function (doc, meta) {" + " if (doc.type == \"beer\" && doc.name) {" + " emit(doc.name, null);" + " }" + "}")); client.createDesignDoc(designDoc); 

With Java SDK 2.0.x

In the same way, you should load definitions of your kind (map functions, shorten the functions in which the project document should be executed), as lines.

Then you deal with DesignDocument by adding a DesignDocument to it and inserting the design document into the bucket through the Bucket BucketManager :

 List<View> viewsForCurrentDesignDocument = new ArrayList<View>(viewCountForCurrentDesignDoc); //... for each view definition you loaded View v = DefaultView.create(viewName, viewMapFunction, viewReduceFunction); viewsForCurrentDesignDocument.add(v); //... then create the designDocument proper DesignDocument designDocument = DesignDocument.create(designDocName, viewsForCurrentDesignDocument); //optionally you can insert it as a development design doc, retrieve an existing one and update, etc... targetBucket.bucketManager().insertDesignDocument(designDocument); 
+4
source

At Rounds, we use couchbase for some of our server-side applications and use docker images for the development environment. I wrote 2 scripts to flush an existing couchbase and re-create kush-like buckets and views from the flushed data.

Functions of the form map and reduce are unloaded as simple javascript files in the directory hierarchy, which are documents and design buckets in couchbase. It is very useful to commit the entire directory tree in your repo so that you can track the changes made to your views. Because the files have simple javascript, you can edit them using your favorite IDE and get automatic syntax checks.

You can use scripts from the following github repo:

https://github.com/rounds/couchbase-dump

Drop all your couches and javascript views in the directory hierarchy that you can commit to your repo. You can then recreate the couch buckets and views from previously dumped data.

If you find this useful and add something, create a problem or contribute to github.

+3
source

Source: https://habr.com/ru/post/979218/


All Articles