Following are the pre-requisites for running this script
Let’s initialize necessary R library for this task
library(mongolite)
library(RMySQL)
## Loading required package: DBI
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(reshape2)
library(ggplot2)
Let’s create database connection to Mongo DB
tb.mongo <- mongo("tb")
tb.mongo$drop()
Let’s create database connection to MySql DB and load data from table tb to R dataframe.
con <- dbConnect(RMySQL::MySQL(), dbname = "moviecatalogue", username = "sachid", password="DatabasePassword@1")
tb.mysql <- dbGetQuery(con, "SELECT * FROM tb")
dim(tb.mysql)
## [1] 3800 6
Insert data from r dataframe to Mongo DB
tb.mongo.df = tb.mongo$insert(tb.mysql)
tb.mongo.df = tb.mongo$find()
dbDisconnect(con)
## [1] TRUE
Let’s use the dataframe loaded form Mongo DB for plotting bar chart. We are plotting poulation count in three categories (Child, Adult and Elders) by gender
tb.mongo.df = na.omit(tb.mongo.df)
tb.mongo.agg = tb.mongo.df %>% group_by(sex) %>% summarize(TotalChild = sum(child), TotalAdult = sum(adult), TotalElder = sum(elderly))
tb.mongo.melt = melt(tb.mongo.agg, id=c("sex"))
p <-ggplot(tb.mongo.melt, aes(variable, value))
p +geom_bar(stat = "identity", aes(fill = sex), position = "dodge") +
xlab("Population") + ylab("Count") +
ggtitle("Total Population by gender") +
theme_bw()
From the bar chart we can see that we were successfully able to load migrated data from Mongo DB and visualize it in the Bar Plot