In [1]:
from pyot.mapreduce.mapreduceApp import pMap, pReduce, pMapReduce
%matplotlib inline
import matplotlib.pyplot as plt
from networkx import *
from pyot.rplApp import *
from pyot.models import *
from IPython.display import Image, display
from networkx.algorithms import *
import numpy as np

def pngShow(path):
    i = Image(filename=path)
    display(i)

In [10]:
rin = Resource.objects.filter(uri='/sensors/light')
input_hosts = get_hosts_from_resources(rin)
output = Resource.objects.filter(uri='/actuator')[0]

#g = RplGraph.objects.all()[0]
#highlight the input resources
#pngShow(g.getPNG(highlight_list=input_hosts))

#create the mapper
myMap = pMap(inputS=rin, period=60)

#create the reducer
redcode = """
from tres_pymite import *
print 'reduce'
i = getIntInput()
print i
setOutput(10)
"""
myReduce = pReduce.fromSource(redcode, 'red')

#create the mapReduce object
mm = pMapReduce(myMap, myReduce, output, min_input=3)

#select a network to deploy the mapreduce on
net = Network.objects.get(hostname='cooja@pyot')

#start the deployment process
result = mm.deploy(net)
print result

reducers = mm.get_reducer_hosts()

#highlight the reducers
#pngShow(g.getPNG(highlight_list=reducers, color='blue'))


Validating /home/andrea/pyot/pyot/media/scripts/red.py ...
Deploy mapReduce task:

map = map -- inputs=[<Resource: bbbb::200:0:0:2 - /sensors/light>, <Resource: bbbb::200:0:0:4 - /sensors/light>, <Resource: bbbb::200:0:0:5 - /sensors/light>, <Resource: bbbb::200:0:0:8 - /sensors/light>, <Resource: bbbb::200:0:0:a - /sensors/light>, <Resource: bbbb::200:0:0:3 - /sensors/light>, <Resource: bbbb::200:0:0:6 - /sensors/light>, <Resource: bbbb::200:0:0:7 - /sensors/light>, <Resource: bbbb::200:0:0:9 - /sensors/light>] -- period=60
reduce = red
output = bbbb::200:0:0:b - /actuator
state = CREATED

...on network: bbbb::/64 - cooja@pyot

Bfs successors 6lbr
Visiting host bbbb::200:0:0:6 --> [<Resource: bbbb::200:0:0:8 - /sensors/light>, <Resource: bbbb::200:0:0:6 - /sensors/light>] 2 inputs

Visiting host bbbb::200:0:0:5 --> [<Resource: bbbb::200:0:0:5 - /sensors/light>, <Resource: bbbb::200:0:0:a - /sensors/light>, <Resource: bbbb::200:0:0:9 - /sensors/light>] 3 inputs
**** Allocating a reducer on  bbbb::200:0:0:5
selected inputs [<Resource: bbbb::200:0:0:5 - /sensors/light>, <Resource: bbbb::200:0:0:a - /sensors/light>, <Resource: bbbb::200:0:0:9 - /sensors/light>]
first put result = 2.01
PF put result = 2.04
IS put result = 2.04 coap://[bbbb::200:0:0:5]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:a]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:9]/sensors/light

Visiting host bbbb::200:0:0:4 --> [<Resource: bbbb::200:0:0:4 - /sensors/light>, <Resource: bbbb::200:0:0:7 - /sensors/light>] 2 inputs

Visiting host bbbb::200:0:0:3 --> [<Resource: bbbb::200:0:0:8 - /sensors/light>, <Resource: bbbb::200:0:0:3 - /sensors/light>, <Resource: bbbb::200:0:0:6 - /sensors/light>, <Resource: bbbb::200:0:0:5 - /tasks/red/lo>] 4 inputs
**** Allocating a reducer on  bbbb::200:0:0:3
selected inputs [<Resource: bbbb::200:0:0:8 - /sensors/light>, <Resource: bbbb::200:0:0:3 - /sensors/light>, <Resource: bbbb::200:0:0:6 - /sensors/light>, <Resource: bbbb::200:0:0:5 - /tasks/red/lo>]
first put result = 2.01
PF put result = 2.04
IS put result = 2.04 coap://[bbbb::200:0:0:8]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:3]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:6]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:5]/tasks/red/lo

Visiting host bbbb::200:0:0:2 --> [<Resource: bbbb::200:0:0:2 - /sensors/light>, <Resource: bbbb::200:0:0:4 - /sensors/light>, <Resource: bbbb::200:0:0:7 - /sensors/light>, <Resource: bbbb::200:0:0:3 - /tasks/red/lo>] 4 inputs
**** Allocating a reducer on  bbbb::200:0:0:2
selected inputs [<Resource: bbbb::200:0:0:2 - /sensors/light>, <Resource: bbbb::200:0:0:4 - /sensors/light>, <Resource: bbbb::200:0:0:7 - /sensors/light>, <Resource: bbbb::200:0:0:3 - /tasks/red/lo>]
first put result = 2.01
PF put result = 2.04
OD put result = 2.04
IS put result = 2.04 coap://[bbbb::200:0:0:2]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:4]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:7]/sensors/light
IS put result = 2.04 coap://[bbbb::200:0:0:3]/tasks/red/lo

1 - Successfully installed mapreduce task on networkbbbb::/64 - cooja@pyot

In [11]:
result = mm.start()
print result


bbbb::200:0:0:5 - /tasks Task now running 2.05
bbbb::200:0:0:3 - /tasks Task now running 2.05
bbbb::200:0:0:2 - /tasks Task now running 2.05
1 - PyoT mapreduce process successfully started

In [8]:
result = mm.stop()
print result


bbbb::200:0:0:5 - /tasks Task now halted 2.05
bbbb::200:0:0:3 - /tasks Task now halted 2.05
bbbb::200:0:0:2 - /tasks Task now halted 2.05
1 - PyoT mapreduce process successfully stopped

In [9]:
result = mm.uninstall()
print result


Successfully removed task: Pf=red, inputs=[<Resource: bbbb::200:0:0:5 - /sensors/light>, <Resource: bbbb::200:0:0:a - /sensors/light>, <Resource: bbbb::200:0:0:9 - /sensors/light>], output=None
Successfully removed task: Pf=red, inputs=[<Resource: bbbb::200:0:0:8 - /sensors/light>, <Resource: bbbb::200:0:0:3 - /sensors/light>, <Resource: bbbb::200:0:0:6 - /sensors/light>], output=None
Successfully removed task: Pf=red, inputs=[<Resource: bbbb::200:0:0:2 - /sensors/light>, <Resource: bbbb::200:0:0:4 - /sensors/light>, <Resource: bbbb::200:0:0:7 - /sensors/light>], output=bbbb::200:0:0:b - /actuator
1 - PyoT mapreduce process successfully uninstalled

In [7]:


In [ ]: