2017-02-19 10 views
1

Ich versuche MapReduce von Jupyter Notebook auf einem Dataset in u.data Datei auszuführen, aber ich bekomme weiterhin eine Fehlermeldung, die besagt "TypeError: 'str' Objekt unterstützt nicht das Löschen von Elementen".Running MapReduce von Jupyter Notebook

Wie kann ich den Code erfolgreich ausgeführt werden?

Die u.data enthält Informationen wie die folgenden:

196 242 3 881250949 
186 302 3 891717742 
22 377 1 878887116 
244 51 2 880606923 
166 346 1 886397596 
298 474 4 884182806 
115 265 2 881171488 
253 465 5 891628467 
305 451 3 886324817 
6 86 3 883603013 

Und hier ist der Code:

from mrjob.job import MRJob 

class MRRatingCounter(MRJob): 
    def mapper(self, key, line): 
     (userID, movieID, rating, timestamp) = line.split("\t") 
     yield rating, 1 

    def reducer(self, rating, occurences): 
     yield rating, sum(occurences) 

if __name__ == "main__": 
    MRRatingCounter.run() 

filepath = "u.data" 

MRRatingCounter(filepath) 

Dieser Code wird erfolgreich ausgeführt, wenn es unter .py Datei speichert und verwendet einen Befehl Zeile: "python ratingCounter.py u.data"

Antwort

0

MRRatingCounter in eigenen Py-Datei vorhanden sein muss, lassen Sie uns MRRatingCounter.py sagen:

from mrjob.job import MRJob 

class MRRatingCounter(MRJob): 

    def mapper(self, key, line): 
     (userID, movieID, rating, timestamp) = line.split("\t") 
     yield rating, 1 

    def reducer(self, rating, occurences): 
     yield rating, sum(occurences) 

if __name__ == "__main__": 
    MRRatingCounter.run() 

Importieren Sie die Klasse in Ihrem Notebook und es durch die Läufer aus:

from MRRatingCounter import MRRatingCounter 

mr_job = MRRatingCounter(args=['u.data']) 
with mr_job.make_runner() as runner: 
    runner.run() 
    for line in runner.stream_output(): 
     #handle each line however you like 
     print line