Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to store couchbase views

My application has couchbase views (map-reduce). Presently, I am writing them on a text file and loading them for each new couchbase server from the couchbase admin page (tedious and error-prone process).

Is there anyway I can load all those views from text files into couchbase when am deploying a fresh couchbase server or when I create a fresh bucket ?

I remember in mysql, we used to write all insert queries and procedures onto a file and feed the file to mysql (via command prompt) for each new instance. Is there any such strategy available for couchbase ?

like image 914
aneez Avatar asked Dec 06 '14 10:12

aneez


People also ask

How does Couchbase store data?

The data is stored in a Couchbase cluster using buckets. Buckets are isolated, virtual containers which logically group records within a cluster. A bucket is the equivalent of a database. They provide a secure mechanism for organizing, managing and analyzing data storage.

Which kind of storage types does Couchbase belong to?

Couchbase Server, originally known as Membase, is an open-source, distributed (shared-nothing architecture) multi-model NoSQL document-oriented database software package optimized for interactive applications.


2 Answers

From your previous couchbase related questions, it seems you are using the java SDK? Both 1.4 and 2.0 lines of the SDK allow for programmatically creating desing documents and views.

With Java SDK 1.4.x

You have to load your view definitions (map functions, reduce functions, in which design document to put them) somehow, as Strings. See the documentation at http://docs.couchbase.com/couchbase-sdk-java-1.4/#design-documents.

Basically you create a ViewDesignin a DesignDocument that you insert in the database via the 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 have to load your view definitions (map functions, reduce functions, in which design document to put them) somehow, as Strings.

Then you deal with DesignDocument, adding DefaultView to it, and insert the design document in the bucket via Bucket's 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);
like image 187
Simon Baslé Avatar answered Sep 23 '22 04:09

Simon Baslé


At Rounds, we use couchbase for some of our server side apps and use docker images for development environment. I wrote 2 scripts for dumping an existing couchbase and re-creating couchbase buckets and views from the dumped data.

The view map and reduce functions are dumped as plain javascript files in a directory hierarchy that represents the design docs and buckets in couchbase. It is very helpful to commit the whole directory tree into your repo so you can track changes made to your views. As the files are plain javascript, you can edit them with your favourite IDE and enjoy automatic syntax checks.

You can use the scripts from the following github repo:

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

Dump all your couchbase buckets and views as javascript files in a directory hierarchy that you can commit to your repo. Then you can recreate the couchbase buckets and views from previously dumped data.

If you find this helpful and have something to add please create an issue or contribute on github.

like image 32
Doody P Avatar answered Sep 23 '22 04:09

Doody P