Wednesday 22 March 2017

mongodb live streaming, mongoc

I was learning mongodb and and mongo-c-driver, i have used many existing example programs available in  mongo-c-driver/example/ directory, to download mongoc drive click here  and follow all the steps to compile and install the build.
But i have used the following steps in order to install the mongo-c-driver:

 =>  wget https://github.com/mongodb/mongo-c-driver/releases/download/1.6.1/mongo-c-driver-1.6.1.tar.gz
=> tar xzf mongo-c-driver-1.6.1.tar.gz
=> cd mongo-c-driver-1.6.1
=> /configure --disable-automatic-init-and-cleanup
=> make
=> sudo make install

inside /root/Downloads/mongo-c-driver-1.6.1/examples/ directory you will find many example mongoc programs, you can simply use example-gridfs.c to read/write/list the gridfs files to/from mongodb.

use following command to to compile the any mongoc program:

   gcc -o example-gridfs example-gridfs.c $(pkg-config --cflags --libs libmongoc-1.0)


use following command to read/write/list the data from/to mongodb:
For writing:
   ./example-gridfs write filename.pcap filename.pcap

For reading:
 ./example-gridfs read filename.pcap > filenameReceived.pcap

For List:
 ./example-gridfs list test


If you have mongodb running in the same system then the above examples will run fine, but if you have a different mongodb server then you have to change the mongodb IP in example-gridfs.c file:

/* connect to localhost client */
     client =
         mongoc_client_new ("mongodb://127.0.0.1:27017?appname=gridfs-example");
         assert (client);

then compile the code and run it again.




Now its time for live streaming
I got stuck when i was trying to save live data into mongodb, I have tried a lot searched it in internet and found nothing to move forward, then i and my senior together written a code using the mongoc APIs, the program was getting input from stdin and writing the stream directly into mongodb
following is the program:
#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>

int
main (int argc, char *argv[])
{
   mongoc_gridfs_t *gridfs;
   mongoc_gridfs_file_t *file;
   mongoc_gridfs_file_opt_t opt = {0};
   mongoc_client_t *client;
   mongoc_stream_t *stream;
   bson_t opts;
   bson_error_t error;
   const char *filename;
   bson_value_t id;


   mongoc_init ();


   /* connect to localhost client */
   client =
      mongoc_client_new ("mongodb://10.201.1.155:27017?appname=tcpdump-realtime-capture-example");
   assert (client);
   mongoc_client_set_error_api (client, 2);

   /* grab a gridfs handle in test prefixed by fs */
   gridfs = mongoc_client_get_gridfs (client, "test", "fs", &error);
   assert (gridfs);

   filename = argv[1];


   stream = mongoc_stream_file_new (STDIN_FILENO);
   assert (stream);

   opt.filename = filename;

   /* the driver generates a file_id for you */
   file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt);
   assert (file);

   id.value_type = BSON_TYPE_INT32;
   id.value.v_int32 = 1;

   /* optional: the following method specifies a file_id of any
      BSON type */
   if (!mongoc_gridfs_file_set_id (file, &id, &error)) {
      fprintf (stderr, "%s\n", error.message);
      return 1;
   }

   mongoc_gridfs_file_save (file);
   mongoc_gridfs_file_destroy (file);
   mongoc_gridfs_destroy (gridfs);
   mongoc_client_destroy (client);

   mongoc_cleanup ();

   return 0;
}

use the below command to compile it:
    gcc -o livestreamToMongodb livestreamToMongodb.c $(pkg-config --cflags --libs libmongoc-1.0)

We were use tshark to capture the packets live and send to this file, and the program writes the stream into mongodb,
    tshark -i any -w - | ./livestreamToMongodb filename.pcap



I hope it would solve the tie of live streaming to mongodb.


Follow me on instagram: Click Here ( Instagram Account)
Subscribe my Youtube channel: Click Here ( Youtube Channel)
Facebook account: Click Here ( Facebook Account)
Linkedin Account: Clicke Here ( Linkedin Account)
My Books:
    India Tourism: India Tourism EBook
    Love story of an engineer: Love Story of an Engineer Ebook





















No comments:

Post a Comment

Nirmit Camp Life & Came back to Gurgaon

D ear readers, You knew that I have reached the Naukuchiatal in my previous blog, As everyone knows that the entire Uttarakhand is the place...