In this blog, we will discuss the
procedures in querying MongoDB collection and parse the JSON document
returned, to use it laterthroughout Kamailio routes.
We make useof following Kamailio
modules:
ndb_mongodb module for querying MongoDB
jansson moduleto parse the JSON document data
We
have already installed the mongo-c-driver thatis
a dependency for this module. If you have not installed, refer to ourprevious
blog.
Load ndb_mongodb module and configure in your
kamailioconfiguration
# under modules
section in yourkamailio.cfg
loadmodule"ndb_mongodb.so" #
Configure the
mongodb server path inmodule parameters modparam("ndb_mongodb",
"server",
"name=apayaa;uri=mongodb://admin:password@localhost/database")
There
are many functions provided by the ndb_mongodb module to queryMongoDB.
- mongodb_cmd
- mongodb_cmd_simple
- mongodb_find
- mongodb_find_one
- mongodb_next
- mongodb_free
You can refer to Documentation above
for details on functions providedby the ndb_mongodb module.
Example on using mongodb_find_one function:
mongodb_find_one: This will query the database and return
firstmatching response.
if(mongodb_find_one("server","database",
"employee", "{ \"username\" :\"1001\"
}", "response")) {
xlog("L_INFO","Employee
Data from mongodb is \" $mongodb(response=>value)\" \n");
$var(jsondata)
=$mongodb(response=>value);
}
You will have the response JSON Data available in
this container $mongodb(response=>value) and we assign to $var(jsondata) variable.
As
we haveJSON data available, now we will parse the response document to get the
values,using JANSSON module.
JANSSONmodule
configuration
Install the Jansson library dependency
cd /usr/src/
# Download using
GIT git clonehttps://github.com/akheron/jansson.git
# Compile JANSSON
cd /usr/src/jansson
autoreconf -i
./configure
&& make &&make install
Load Janssonmodule and configure in your Kamailio
configuration.
# under modules section in
yourkamailio.cfg
loadmodule “jansson.so”
There are no module parameters available.There are many functions provided by the Jansson
module to parse JSON documents.
- jansson_get
- jansson_set
- jansson_append
- jansson_array_size
- jansson_get_field
You can refer to Documentation above
for details on functions providedby the Jansson module.
Example on using jansson_get function:
jansson_get: Fetch the value at the provided ‘path’
fromthe json object and store it in result variable. This also supports
dotdelimited notation to parse the json tree object.
eg. employee.contact.phone[0]
jansson_get("employee.contact.phone[0]",$var(jsondata),
"$var(phonenumber)");
xlog("L_INFO","Employee
Contact Phone number is $var(phonenumber)");
Now
we have employee’s contact phone number in a variable, $var(phonenumber).We
can use this anywhere in our script. Similarly we can fetch any data fromour
JSON response document from mongodb.
The
ndb_mongodb and Jansson modules are helpful when you need to fetch data in real
time from mongodb and use it in your Kamailio routes.
The data fetching and parsing will be pretty fast,
when using in-built modules rather than using external scripts.
No comments:
Post a Comment