Wednesday, June 10, 2015

Mongodb and scripting


To do house keeping job on Mongo DB we can use scripting for it

Steps involved will be

1) Write a java script file using the details given in http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/
2) To run a script use comand : mongo .js
3) If you want to get the shell also when executing like this : mongo testConnction.js --shell

testJsScript.js

` db = connect("localhost:27017/myDatabase"); // db contain the reference of the database printjson(db.getCollectionNames()); // mongodb provided print command print(db.getCollectionNames()); //js print command cursor = db.collection.find(); // this will return a cursor, this cursor can be used to iterate over all the collection object.
you can even use this cursor for updating items, but the catch is you have to fire a query which actually return only the json object which can be directly used in the query.

Mongo DB indexing pitfalls and how to avoid it.

Querying and adding Index to Mongo DB is a bit straight forward. But to use both together to optimize the response time can be a bit tricky.
As adding Index, can help to reduce to response time to find an item, similarly adding unwanted index can lead to other memory and response time-related issue.

So below are some quick  rules/suggestion which should be followed to avoid any pitfalls of indexing

  1.  If you have a field X on which you have an index with a unique key constraint.  Then you don't have to have any other index with other fields along with this X field.
e.g
db.myCollection.ensureIndex({X:1},{unique:true})

Then there is no point of having index like:

db.myCollection.ensureIndex({X:1,b:1})  // no need can be removed
db.myCollection.ensureIndex({X:1,b:1,c:1}) // no need can be removed



2. If you have a compound index and you want to use this index in your query then make sure your query being fired always have the first element of the index.
e.g
compound index : { w:1,x: 1, y: 1, z: 1 }

Then you can fire any query having the FIRST element (w) to use the index.

wxy
wxz
wyz
xyw
yxw
zxw
All above query will use the compound index defined above.
But you CAN'T fire any query which does not have the first element of the indexed as it won't use the index.
e.g  
xyz
xy
zy
For more info read : compound-index   and index-intersection-compound-indexes

3. You should always check your index and write query against it and  not to do vise-verse. If there is no index to cater your query first add the index and then make the query against it.

Friday, June 5, 2015

Camel Technical learnings



Whenever you use a `choice()` in your camel route always use `end()` to end it as opposed to `endChoice()`. Using `endChoice()` drops the exchange from the route.

-------------------------------------

How are bean method is called :
Camel uses a Camel’s method-selection algorithm for selecting a method in the Bean class.
Also uses Bean parameter binding as defined below :
generally this steps are performed:
The BeanProcessor uses the input message to bind its body to the first parameter
of the method of called Bean.

see the different annotation that can be used in defining Bean methods: http://camel.apache.org/parameter-binding-annotations.html

Also see 4.5.1 Binding with multiple parameters in camel in acton.


----------------------------

Configure method of a Routebuilder is called only once at the startup of the camelContext. So any value added to route cant be changed even though  you may have used variable to define them (and variables being dynamically loaded from properties file).




Camel Tuning

1)  Don't use streaming on multicast or splitter


2)  Don't user  allowUseOriginalMessage if you are not going to use the orginal exchange.
scenerios when you should use it when you are going to push back the same message to queue when there is any error.

3) Increase the camel thread pool size if you are using too many splitter or multicast  in your route


<camel:camelContext id="camel-server" allowUseOriginalMessage="false">

        <camel:routeBuilder ref="testRoute"/>

        <camel:threadPoolProfile id="service-threadpool"
                           defaultProfile="true"
                           poolSize="30" maxPoolSize="100" />

    </camel:camelContext>