This document outlines a presentation on developing and deploying edge analytics. The agenda includes demonstrations of streaming sensor data into Redis on an edge device and running Spark analytics on the edge using Redis. Open source projects that will be used include the Lua Amalgamator for Redis, Stuart for Spark ML in Lua, and Stuart-Redis for reading and writing Spark RDDs in Redis. The demonstrations will stream temperature and pressure data from a sensor into a Redis instance on a Raspberry Pi and perform simple analytics and clustering using Spark functions within the Redis instance.
2. PRESENTED BY
1 IoT and Edge Architectures
A brief overview of Enterprise Architectures with Cloud, Fog, and Edge components
2 Open Source Projects Used
A brief overview of the open source projects used in this demo and links to them
3 Demo: Streaming Data into Redis on Edge Devices
A stream of sensor data will be collected and stored in Redis on an edge device.
Agenda:
4 Demo: Running Spark on the Edge with Redis Integration
A simple analytic will be developed using Spark & Redis, deployed to the edge, and executed
4. PRESENTED BY
Why are Analytics Required at the Source of Data?
Because:
Sense, Infer, Act
“Return to the Edge” (2016)
by Peter Levine, Andreessen Horowitz
5. PRESENTED BY
Cloud and Device Edge: Two Worlds Under Pressure to Meet
Devices
•Under pressure to add intelligence and inference
•Which requires frequent updates (a tectonic shift for
the embedded world)
•Which requires best-of-breed cloud workflows like
aPaaS, IaaS, and Continuous Integration
Cloud
•Under pressure to realize the final 92% of Digital
Transformation, which hasn’t happened yet
because products live outside of datacenters
•Data Scientists don’t use desktop power supplies
and serial cables
•Don’t have tooling to program in 32k of RAM
7. PRESENTED BY
• Use your favorite LuaRocks modules within Redis
• GitHub: github.com/BixData/lua-amalg-redis
• Blog: https://medium.com/nubix-open-source-edge-computing/introducing-the-
lua-amalgamator-for-redis-c498434c2154
Open Source #1: Lua Amalgamator for Redis
9. PRESENTED BY
Moses: the Lodash / Underscore of the Lua ecosystem
Using Moses within Redis
$ luarocks install moses
$ vi main.lua
local moses = require 'moses'
local data = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
local sum = moses.reduce(data, function(r, n)
return r + n
end)
print(string.format('Sum of first %d primes is %d', #data, sum))
return sum
$ lua –lamalg-redis main.lua
Sum of first 10 primes is 129
$ amalg-redis.lua –s main.lua –o main-with-dependencies.lua –c
$ redis-cli --eval main-with-dependencies.lua 0,0
(integer) 129
11. PRESENTED BY
Using Spark ML Vectors within Redis
$ luarocks install stuart-ml
$ vi main.lua
local BLAS = require 'stuart-ml.linalg.BLAS'
local Vectors = require 'stuart-ml.linalg.Vectors'
local a = Vectors.dense({1,2,3})
local b = Vectors.dense({4,-5,6})
local dotProduct = BLAS.dot(a, b)
local isAcuteAngle = dotProduct > 0
print(string.format('The dot product of [{1,2,3}, {4,-5,6}] is %d, which %s an acute angle',
dotProduct, isAcuteAngle and 'is' or 'is not'))
return dotProduct
$ lua –lamalg-redis main.lua
The dot product of [{1,2,3}, {4,-5,6}] is 12, which is an acute angle
$ amalg-redis.lua –s main.lua –o main-with-dependencies.lua –c
$ redis-cli --eval main-with-dependencies.lua 0,0
(integer) 12
12. PRESENTED BY
Using Spark ML Matrices within Redis
$ vi main.lua
local Matrices = require 'stuart-ml.linalg.Matrices'
local matrix = Matrices.sparse(3, 2, {0, 2, 3}, {0, 2, 1}, {0.0, -1.2, 0.0})
local msg = string.format('The sparse matrix %s has %d non-zeros and %d actives',
'(3, 2, {0, 2, 3}, {0, 2, 1}, {0.0, -1.2, 0.0})',
matrix:numNonzeros(),
matrix:numActives())
print(msg)
return msg
$ lua –lamalg-redis main.lua
The sparse matrix (3, 2, {0, 2, 3}, {0, 2, 1}, {0.0, -1.2, 0.0}) has 1 non-zeros and 3 actives
$ amalg-redis.lua –s main.lua –o main-with-dependencies.lua –c
$ redis-cli --eval main-with-dependencies.lua 0,0
"The sparse matrix (3, 2, {0, 2, 3}, {0, 2, 1}, {0.0, -1.2, 0.0}) has 1 non-zeros and 3 actives"
13. PRESENTED BY
Using Spark ML K-means Clustering within Redis
$ vi main.lua
local KMeans = require 'stuart-ml.clustering.KMeans'
local Vectors = require 'stuart-ml.linalg.Vectors'
local VectorWithNorm = require 'stuart-ml.clustering.VectorWithNorm'
local centers = {
VectorWithNorm.new(Vectors.dense(1,2,6)),
VectorWithNorm.new(Vectors.dense(5,3,9)),
VectorWithNorm.new(Vectors.dense(9,4,7))
}
local point = VectorWithNorm.new(Vectors.dense(6,2,5))
local bestIndex, bestDistance = KMeans.findClosest(centers, point)
local msg = string.format('The point %s is closest to cluster center #%d, with a distance of %d',
tostring(point), bestIndex, bestDistance)
print(msg)
return msg
$ lua –lamalg-redis main.lua
The point ((6,2,5),8.0622577482985) is closest to cluster center #3, with a distance of 17
$ amalg-redis.lua –s main.lua –o main-with-dependencies.lua –c
$ redis-cli --eval main-with-dependencies.lua 0,0
"The point ((6,2,5),8.0622577482985) is closest to cluster center #3, with a distance of 17"
15. PRESENTED BY
Reading+Writing Spark RDDs within Redis$ luarocks install stuart-redis
$ vi main.lua
local stuart = require 'stuart'
local stuartRedis = require 'stuart-redis'
local function split(str, pattern)
local res = {}
for s in string.gmatch(str, pattern) do table.insert(res, s) end
return res
end
local sc = stuart.NewContext()
sc = stuartRedis.export(sc)
-- writing
local doc = [[I mean, think about music. Music is all about repetition and patterns.
If you didn’t have repetition in music, it would all just be noise.]]
local words = sc:parallelize(split(doc, '%w+'))
local wordCountsRDD = words
:map(function(word) return {string.lower(word), 1} end)
:reduceByKey(function(r, x) return r+x end)
:map(function(e) return {e[1], e[2]} end)
sc:toRedisZSET(wordCountsRDD, 'wordCounts')
-- reading
local wordFrequencies = sc:fromRedisZSetWithScore('wordCounts')
:map(function(e) return e[1] .. '=' .. e[2] end)
:collect()
local msg = table.concat(wordFrequencies, ', ')
print(msg)
return msg
17. PRESENTED BY
Hardware
• Raspberry Pi 3 B+
• BME280 Atmospheric Sensor
• I²C Communication Bus(shown in
orange+yellow)
Software
• Raspbian OS
• Redis 5.0
• Nubix Agent
DEMO 1: Streaming Data into Redis on an Edge Device
18. PRESENTED BY
Hardware
• Raspberry Pi 3 B+
• BME280 Atmospheric Sensor
• I²C Communication Bus(shown in
orange+yellow)
Software
•Redis 5.0 (unmodified, no
plugins)
• Lua submitted via standard
redis-cli
DEMO 2: Spark Analytics within Redis on Edge Device