0% found this document useful (0 votes)
39 views6 pages

Experiment 10 Aim:To Implement Nosql Database in An Application Theory

The document describes an experiment to implement a NoSQL database (Amazon DynamoDB) in a sensor application. Python code is used to upload temperature and humidity sensor data from a Raspberry Pi to the DynamoDB database. The data is then retrieved from the database and loaded into JavaScript charts for visualization. This allows storing and visualizing heterogeneous sensor data in a flexible, scalable way using a NoSQL database.

Uploaded by

Nihar Sardal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Experiment 10 Aim:To Implement Nosql Database in An Application Theory

The document describes an experiment to implement a NoSQL database (Amazon DynamoDB) in a sensor application. Python code is used to upload temperature and humidity sensor data from a Raspberry Pi to the DynamoDB database. The data is then retrieved from the database and loaded into JavaScript charts for visualization. This allows storing and visualizing heterogeneous sensor data in a flexible, scalable way using a NoSQL database.

Uploaded by

Nihar Sardal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 10

Aim:To implement NoSql database in an application

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:

Uploading Data from raspberry pi to DynamoDb


Python Code:

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):

def __init__(self, Table_Name='DHT'):


self.Table_Name=Table_Name

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 put(self, Sensor_Id='' , Temperature='', Humidity=''):


self.table.put_item(
Item={
'Sensor_Id':Sensor_Id,
'Temperature':Temperature,
'Humidity' :Humidity
}
)

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;

if humidity is not None and temperature is not None:


print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
return temperature, humidity
def main():
global counter

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:

Retrieve Data from Table and load it into Chart :

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');

var tm = requests.done(function (result)


{
// Temperature
var seriesTemperature = chartTemperatue.series[0],
shiftTemperature = seriesTemperature.data.length > 20;

// Humidity
var seriesHumidity = chartHumidity.series[0],
shiftHumidity = seriesTemperature.data.length > 20;

var seriesMoisture = chartMoisture.series[0],


shiftMoisture = seriesTemperature.data.length > 20;

var seriesSTemperature = chartSTemperatue.series[0],


shiftSTemperature = seriesTemperature.data.length > 20;

// Add the Point


// Time Temperature\
var data1 = [];
data1.push(result[0]);
data1.push(result[1]);

// Add the Point


// Time Humidity
var data2 = [];
data2.push(result[0]);
data2.push(result[2]);

var data3 = [];


data3.push(result[0]);
data3.push(result[3]);

var data4 = [];


data4.push(result[0]);
data4.push(result[4]);

chartTemperatue.series[0].addPoint(data1, true, shiftTemperature);


chartHumidity.series[0].addPoint(data2, true, shiftHumidity);
chartMoisture.series[0].addPoint(data3, true, shiftMoisture);
chartSTemperatue.series[0].addPoint(data4, true, shiftSTemperature);
$(".sensor1").text("");
$(".sensor1").text("Temperature : " + Math.round(data1[1]) );

$(".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]) );

// call it again after one second


setTimeout(requestData, 6000);

Output:
Conclusion: Thus we have successfully used a nosql database in our application.

You might also like