The concept of VIEWS in couchbase is similar to what we had in RDBMS. The main reason for creation of the view is to have data hiding and to show only the relevant information to the end user. i.e. if we have three table and each table have 3 column and we want the user to show user with below combination where in we take 2 column from table1, 1 column from table2 and all column from table3 in that case we create an view and expose it to the client. By this ways client can query that view and get the data keeping other fields of respective table as hidden to them.
Before understanding VIEWS lets try to understand the Index concept in couchbase. Generally Indexing is the process that is used to speed up the lookup of things easily,quickly and most resource efficient ways. Best example are hard page books. We have index in them generally in few pages at the last of the book. When ever we want to get reference of any spelling in that book pages we search the work in the index and we get directly the page number where we can see that word is used. Same is the case with Indexing in RDBMS. We do index on Primary key and we kept them in another system (generally databse enduser don’t understand these differnce between the storage system and index system) . when ever we make a search query DB find the data index from this index system and give us the result quickly to us.
In couchbase system we use the concept of mapreduce to create index.
In couchbase VIEWS are used to create index using MapReduce function. Let me try to explain you what this mapreduce function means.
MapReduce are combination of two functions
1- MAP
2- Reduce
1- MAP:-
1 2 3 | function (doc, meta) { emit(doc.name, doc.type); } |
This function take input as doc and meta data generally metadata contains informaion like
1 2 3 4 5 6 | { "id": "key4", "rev": "2-16940aa3e3c900000000000002000006", "expiration": 0, "flags": 33554438 } |
Then using the emit fucntion to generate the index. If we do the bifurcation of the above function it would be like this
emit(doc.name, doc.type);
emit :- indicate insert or create of a row for index.
Doc.name :- This the primarkey used for index. Like we do in RDBMS
doc.type :- This is the output value that is going to be indexed. Generally developer store whole doc in side this, but this is not good practice because anyway by default your whole doc is going to be indexed.
Let's say our document has this information
1 2 3 4 5 | { "address": "address4", "name": "new hotel", "type": "hotel" } |
In this case for using this emit(doc.name, doc.type);
will create index with doc.name and output will be doc.type.
so in short Map take the doc and its meta data and create index with the name of emit function first argument i.e. doc.name and do the indexing for the data that will be output to the end user given in second argument i.e. doc.type
2- Reducer
This function take input from the MAP function ie. index values and reduce it using some aggregation function such as count, sum, average i.e. Reduce (built in: _count, _sum, _stats)
Now, let's see how we can create Views in couchbase ui.
Modify the map function as give below
function (doc, meta) {
emit(doc.name, doc.type);
}
Click on the save changes and click on show results
Note:- Make sure you should have document in your bucket so that you can see the result, else you will get empty as you did not have data in your buckets.
You will see the value as given below
Key will have id of meta data and doc.name as specified in map function first argument (index value) and value will be the map function second argument i.e. out put data.
You can also apply filter on the result using below option provided on the screen
Now lets try to use reduce function.we use the _count as an aggregation and check the result it will show you the count of your rows.
Now the question is how we can use these views.
There are many ways the best way as the views are exposed as REST url so you can use it using curl or using any rest supporting client.
for this I had used postman
The url to access the following is the HTTP method and URI used to query views:
GET /[bucket-name]/_design/[ddoc-name]/_view/[view-name]
for more information browse https://docs.couchbase.com/server/current/learn/views/views-querying.html
as our bucker is siddhu, ddoc-name is dev_siddhudocument and view name is byName hit the below url in post man
http://admin:admin1@localhost:8092/siddhu/_design/dev_siddhudocument/_view/byName
Now lets export this view to production, this can be done using the following option on the screen
Now lets the below production version of the view in postman
http://admin:admin1@localhost:8092/siddhu/_design/siddhudocument/_view/byName
No comments:
Post a Comment