Assignment
For this assignment, you should take information from a relational database and migrate it to a NoSQL database of your own choosing.
Load Libraries
library(RMySQL)
library(mongolite)SQL Connection and Airports Table
con <- dbConnect(MySQL(), user='root', password='password',
dbname='flights', host='localhost')
airports <- dbGetQuery(conn = con, statement = "SELECT * FROM airports")We set up our MySQL and run a query to get all the information from the airports table.
Mongo Connection and Migration
remove(mongo_airports)## Warning in remove(mongo_airports): object 'mongo_airports' not found
mongo_airports <- mongo(collection = "flights", db = "flights"
,url="mongodb://CUNY:SPS@cluster0-shard-00-00-fcscs.mongodb.net:27017,cluster0-shard-00-01-fcscs.mongodb.net:27017,cluster0-shard-00-02-fcscs.mongodb.net:27017/admin?replicaSet=Cluster0-shard-0&ssl=true")
mongo_airports$insert(airports)## List of 5
## $ nInserted : num 1397
## $ nMatched : num 0
## $ nRemoved : num 0
## $ nUpserted : num 0
## $ writeErrors: list()
We set up our Mongo connection and user the insert function to transfer the airport table.
Data Compare
mongo_airports$count()## [1] 1397
nrow(airports)## [1] 1397
Looking at the count from the airport table in Mongo we can see that it equals the count of the airport table in MySQL. Our data migration was successful.
NoSQL vs MySQL
Using a non relational database (NoSQL) has advantages when using large amounts of data. It is easier to scale with no need for multipele expensive servers like relational databases (MySQL). With relational databases being around for longer there is more support for it. Relational databases are also standardize and more structured than non realational databases.