Experiment 10 Aim:To Implement Nosql Database in An Application Theory
Experiment 10 Aim:To Implement Nosql Database in An Application Theory
Theory:
NoSQL databases don’t enforce a strict schema and hence allow for highly flexible data modeling,
not to mention dramatically better scalability than even the most hefty of relational database
management systems (RDBMSes).
Both aspects are critical for Internet of Things applications,while RDBMSes will continue to play a
part, the more disruptive aspect of the Internet of Things is all NoSQL:
The traditional relational database management systems will continue to have a role in the Internet
of Things when processing structured, highly uniform data sets, generated from a vast number of
enterprise IT systems and where this data is managed in a relatively isolated manner. When it comes
to managing more heterogeneous data generated by millions and millions of sensors, devices and
gateways, each with their own data structures and potentially becoming connected and integrated
over the course of many years, databases will require new levels of flexibility, agility and
scalability. In this environment, NoSQL databases are proving their value.
DynamoDb:
Amazon DynamoDB is a fully managed proprietary NoSQL database service that supports key-
value and document data structures and is offered by Amazon.com as part of the Amazon Web
Services portfolio.
DynamoDB differs from other Amazon services by allowing developers to purchase a service based
on throughput, rather than storage. If Auto Scaling is enabled, then the database will scale
automatically. Additionally, administrators can request throughput changes and DynamoDB will
spread the data and traffic over a number of servers using solid-state drives, allowing predictable
performance. It offers integration with Hadoop via Elastic MapReduce.
Working:
try:
import os
import sys
import datetime
import time
import boto3
import threading
print("All Modules Loaded ...... ")
except Exception as e:
print("Error {}".format(e))
class MyDb(object):
self.db = boto3.resource('dynamodb')
self.table = self.db.Table(Table_Name)
self.client = boto3.client('dynamodb')
@property
def get(self):
response = self.table.get_item(
Key={
'Sensor_Id':"1"
}
)
return response
def delete(self,Sensor_Id=''):
self.table.delete_item(
Key={
'Sensor_Id': Sensor_Id
}
)
def describe_table(self):
response = self.client.describe_table(
TableName='Sensor'
)
return response
@staticmethod
def sensor_value():
pin = 23
sensor = 1
humidity= random() * 32
temperature = random() * 65
moisture;
stemperatue;
threading.Timer(interval=10, function=main).start()
obj = MyDb()
Temperature , Humidity = obj.sensor_value()
obj.put(Sensor_Id=str(counter), Temperature=str(Temperature), Humidity=str(Humidity))
counter = counter + 1
print("Uploaded Sample on Cloud T:{},H{} ".format(Temperature, Humidity))
if __name__ == "__main__":
global counter
counter = 0
main()
Output:
Python code:
import boto3
client = boto3.client('dynamodb')
DB = boto3.resource('dynamodb')
table = DB.Table("DHT")
conn = sqlite3.connect('database2.db')
cursor = conn.execute("SELECT ID, Sensor_Id from students")
for row in cursor:
id=row[0]
sensor_Id=row[1]
sensor_Id=sensor_Id+1
try:
response = table.get_item(
Key={
"Sensor_Id":str(sensor_Id)
}
)
a=response["Item"]
print(sensor_Id)
except Exception as e:
sensor_Id=sensor_Id-1
JAVASCRIPT:
var chartTemperatue;
var chartHumidity;
var chartMoisture;
var chartSTemperatue;
function requestData()
{
// Ajax call to get the Data from Flask
var requests = $.get('/data');
// Humidity
var seriesHumidity = chartHumidity.series[0],
shiftHumidity = seriesTemperature.data.length > 20;
$(".sensor2").text("");
$(".sensor2").text("Humidity : " + Math.round(data2[1]) );
$(".sensor3").text("");
$(".sensor3").text("Moisture : " + Math.round(data3[1]) );
$(".sensor4").text("");
$(".sensor4").text("STemperature : " + Math.round(data4[1]) );
Output:
Conclusion: Thus we have successfully used a nosql database in our application.