SlideShare une entreprise Scribd logo
1  sur  9
Map Reduce
LA PRATIQUE
TP1 : Map reduce en python
 Le Hello World du Map Reduce : compter les mots
 Vous avez à coder plusieurs modules :
 Mapper.py
 Qui lit un texte sur l’entrée standard
 Et qui renvoie un doublet : mot tabulation (t) 1 retour à la ligne (n)
sur la sortie standard
 Reducer.py
 Qui lit les doublets depuis l’entrée standard
 Qui calcule le nombre d’occurrence d’un mot
2
Résultats attendus 3
echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py
foo 1
foo 1
quux 1
labs 1
foo 1
bar 1
quux 1
echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py | sort -k1,1 | /home/hduser/reducer.py
bar 1
foo 3
labs 1
quux 2
Pure Python fonction map 4
map(function_to_apply, list_of_inputs)
Cela permet de remplacer un code comme celui-ci
item = [1,2,3,4,5]
carres = []
for i in items:
carres.append(i**2)
item = [1,2,3,4,5]
Carres = list(map(lambda x: x**2, items))
par
Pure Python fonction filter 5
filter(function_to_apply, list_of_inputs)
Agit comme une boucle mais c’est une fonction interne, plus rapide
liste = range(-5, 5)
negatifs = list(filter(lambda x: x<0, liste))
[-5, -4, -3, -2, -1]
renvoie
Pure Python fonction reduce 6
reduce(function_to_apply, list_of_inputs)
Cela permet de remplacer un code comme celui-ci
produit = 1
list = [1, 2, 3, 4]
for num in list:
produit = produit * num
from functools import reduce
list = [1, 2, 3, 4]
produit = reduce((lambda x,y: x*y), list)
par
Map 7
#!/usr/bin/env python
"""mapper.py"""
import sys
# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
print('%st%s' % (word, 1) )
Reduce 8
#!/usr/bin/env python
from operator import itemgetter
import sys
current_word = None
current_count = 0
word = None
for line in sys.stdin:
line = line.strip()
word, count = line.split('t', 1)
try:
count = int(count)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue
if current_word == word:
current_count += count
else:
if current_word:
print('%st%s' % (current_word, current_count))
current_count = count
current_word = word
if current_word == word:
print('%st%s' % (current_word, current_count))
Pipe (/Paipe/)
 Toutes les commandes un*x ont trois moyens de communiquer avec
l’utilisateur
 Un moyen de lire ce qu’il demande (par défaut par le clavier) : stdin
 Un moyen de transmettre des informations (par défaut à l’écran) : stdout
 Un moyen de signaler les erreurs (par défaut à l’écran) : stderr
 Un*x permet de chainer les commandes
 On relie le stdout d’un programme au stdin du suivant
 C’est la commande pipe, notée |
9

Contenu connexe

Tendances

Corrigé iscae informatique 2014
Corrigé iscae informatique 2014Corrigé iscae informatique 2014
Corrigé iscae informatique 2014Yassine Anddam
 
The iTronics Internet of things Workshop 27-05-2017 - get started
The iTronics Internet of things Workshop 27-05-2017 - get startedThe iTronics Internet of things Workshop 27-05-2017 - get started
The iTronics Internet of things Workshop 27-05-2017 - get startedRomaric Saounde Tsopnang
 
2- The iTronics Internet of things Workshop 27-05-2017 - GPIO
2- The iTronics Internet of things Workshop 27-05-2017 - GPIO2- The iTronics Internet of things Workshop 27-05-2017 - GPIO
2- The iTronics Internet of things Workshop 27-05-2017 - GPIORomaric Saounde Tsopnang
 
Return Type Declaration
Return Type DeclarationReturn Type Declaration
Return Type DeclarationDarkmira
 
Lect14 dev2
Lect14 dev2Lect14 dev2
Lect14 dev2moisko
 
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017Romaric Saounde Tsopnang
 
Correction dc3 3sc2
Correction dc3 3sc2Correction dc3 3sc2
Correction dc3 3sc2Hela Ch
 
ALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionAlexandru Radovici
 
La programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlLa programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlStéphane Legrand
 
Tp1 architecture m.zarboubi
Tp1 architecture m.zarboubiTp1 architecture m.zarboubi
Tp1 architecture m.zarboubiMOHAMED ZARBOUBI
 
ALF 3 - Expressions régulières et Lexer
ALF 3 - Expressions régulières et Lexer ALF 3 - Expressions régulières et Lexer
ALF 3 - Expressions régulières et Lexer Alexandru Radovici
 
Good or Evil: les fonctions anonymes en Javascript
Good or Evil: les fonctions anonymes en JavascriptGood or Evil: les fonctions anonymes en Javascript
Good or Evil: les fonctions anonymes en JavascriptNoirdes
 
Programming language python 2021
Programming language python 2021Programming language python 2021
Programming language python 2021Dalila Chouaya
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniShellmates
 
Résumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementRésumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementborhen boukthir
 
Null Coalescing Operator
Null Coalescing OperatorNull Coalescing Operator
Null Coalescing OperatorDarkmira
 

Tendances (20)

Corrigé iscae informatique 2014
Corrigé iscae informatique 2014Corrigé iscae informatique 2014
Corrigé iscae informatique 2014
 
The iTronics Internet of things Workshop 27-05-2017 - get started
The iTronics Internet of things Workshop 27-05-2017 - get startedThe iTronics Internet of things Workshop 27-05-2017 - get started
The iTronics Internet of things Workshop 27-05-2017 - get started
 
2- The iTronics Internet of things Workshop 27-05-2017 - GPIO
2- The iTronics Internet of things Workshop 27-05-2017 - GPIO2- The iTronics Internet of things Workshop 27-05-2017 - GPIO
2- The iTronics Internet of things Workshop 27-05-2017 - GPIO
 
Return Type Declaration
Return Type DeclarationReturn Type Declaration
Return Type Declaration
 
Lect14 dev2
Lect14 dev2Lect14 dev2
Lect14 dev2
 
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017
The iTronics - Arduino + Raspberry Pi Workshop 20-04-2017
 
ALF 8 - Generation de code
ALF 8 - Generation de codeALF 8 - Generation de code
ALF 8 - Generation de code
 
Correction dc3 3sc2
Correction dc3 3sc2Correction dc3 3sc2
Correction dc3 3sc2
 
ALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonction
 
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
 
La programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlLa programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCaml
 
ALF 11 - WebAssembly
ALF 11 - WebAssemblyALF 11 - WebAssembly
ALF 11 - WebAssembly
 
Tp1 architecture m.zarboubi
Tp1 architecture m.zarboubiTp1 architecture m.zarboubi
Tp1 architecture m.zarboubi
 
ALF 3 - Expressions régulières et Lexer
ALF 3 - Expressions régulières et Lexer ALF 3 - Expressions régulières et Lexer
ALF 3 - Expressions régulières et Lexer
 
Good or Evil: les fonctions anonymes en Javascript
Good or Evil: les fonctions anonymes en JavascriptGood or Evil: les fonctions anonymes en Javascript
Good or Evil: les fonctions anonymes en Javascript
 
Programming language python 2021
Programming language python 2021Programming language python 2021
Programming language python 2021
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
 
Résumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementRésumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrement
 
Ch04 23
Ch04 23Ch04 23
Ch04 23
 
Null Coalescing Operator
Null Coalescing OperatorNull Coalescing Operator
Null Coalescing Operator
 

Similaire à 09 big data mapreduce

Formation python
Formation pythonFormation python
Formation pythonj_lipaz
 
Cours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMACours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMALoic Yon
 
C1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partieC1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partieLoic Yon
 
Seance 3- Programmation en langage C
Seance 3- Programmation en langage C Seance 3- Programmation en langage C
Seance 3- Programmation en langage C Fahad Golra
 
02 Spécificité du C++ COURS SYS SYSSSSSS
02 Spécificité du C++  COURS SYS SYSSSSSS02 Spécificité du C++  COURS SYS SYSSSSSS
02 Spécificité du C++ COURS SYS SYSSSSSSAyoubElmrabet6
 
Tp1 compte rendu en langage c
Tp1 compte rendu en langage cTp1 compte rendu en langage c
Tp1 compte rendu en langage cEbrima NJIE
 
01 - Introduction à Python chaines de caractères.pdf
01 - Introduction à Python chaines de caractères.pdf01 - Introduction à Python chaines de caractères.pdf
01 - Introduction à Python chaines de caractères.pdfMARYAM510573
 
Interception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appelInterception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appelThierry Gayet
 
Python avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de donnéesPython avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de donnéesECAM Brussels Engineering School
 
cours lanagage c avec des exemples d'application
cours lanagage c avec des exemples d'applicationcours lanagage c avec des exemples d'application
cours lanagage c avec des exemples d'applicationkamalomari2
 
Seance 4- Programmation en langage C
Seance 4- Programmation en langage CSeance 4- Programmation en langage C
Seance 4- Programmation en langage CFahad Golra
 
Chap 1 Initiation.pptx
Chap 1 Initiation.pptxChap 1 Initiation.pptx
Chap 1 Initiation.pptxolfaharrabi2
 
Les structures de données.pptx
Les structures de données.pptxLes structures de données.pptx
Les structures de données.pptxPROFPROF11
 

Similaire à 09 big data mapreduce (20)

Formation python
Formation pythonFormation python
Formation python
 
Cours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMACours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMA
 
C1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partieC1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partie
 
COURS_PYTHON_22.ppt
COURS_PYTHON_22.pptCOURS_PYTHON_22.ppt
COURS_PYTHON_22.ppt
 
Seance 3- Programmation en langage C
Seance 3- Programmation en langage C Seance 3- Programmation en langage C
Seance 3- Programmation en langage C
 
C++ 11/14
C++ 11/14C++ 11/14
C++ 11/14
 
Formation python
Formation pythonFormation python
Formation python
 
Theme 6
Theme 6Theme 6
Theme 6
 
02 Spécificité du C++ COURS SYS SYSSSSSS
02 Spécificité du C++  COURS SYS SYSSSSSS02 Spécificité du C++  COURS SYS SYSSSSSS
02 Spécificité du C++ COURS SYS SYSSSSSS
 
Tp1 compte rendu en langage c
Tp1 compte rendu en langage cTp1 compte rendu en langage c
Tp1 compte rendu en langage c
 
01 - Introduction à Python chaines de caractères.pdf
01 - Introduction à Python chaines de caractères.pdf01 - Introduction à Python chaines de caractères.pdf
01 - Introduction à Python chaines de caractères.pdf
 
Chap1_Entrees_Sorties.pptx
Chap1_Entrees_Sorties.pptxChap1_Entrees_Sorties.pptx
Chap1_Entrees_Sorties.pptx
 
POO-chapitre2.pptx
POO-chapitre2.pptxPOO-chapitre2.pptx
POO-chapitre2.pptx
 
Interception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appelInterception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appel
 
Python avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de donnéesPython avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de données
 
cours lanagage c avec des exemples d'application
cours lanagage c avec des exemples d'applicationcours lanagage c avec des exemples d'application
cours lanagage c avec des exemples d'application
 
Seance 4- Programmation en langage C
Seance 4- Programmation en langage CSeance 4- Programmation en langage C
Seance 4- Programmation en langage C
 
Chap 1 Initiation.pptx
Chap 1 Initiation.pptxChap 1 Initiation.pptx
Chap 1 Initiation.pptx
 
Les structures de données.pptx
Les structures de données.pptxLes structures de données.pptx
Les structures de données.pptx
 
Php4 Mysql
Php4 MysqlPhp4 Mysql
Php4 Mysql
 

Plus de Patrick Bury

16 graph databases
16 graph databases16 graph databases
16 graph databasesPatrick Bury
 
15 map reduce on azure
15 map reduce on azure15 map reduce on azure
15 map reduce on azurePatrick Bury
 
14 big data gitlab
14 big data gitlab14 big data gitlab
14 big data gitlabPatrick Bury
 
13 big data docker
13 big data docker13 big data docker
13 big data dockerPatrick Bury
 
10 big data hadoop
10 big data hadoop10 big data hadoop
10 big data hadoopPatrick Bury
 
08 big data dataviz
08 big data dataviz08 big data dataviz
08 big data datavizPatrick Bury
 
06 cloud souverain
06 cloud souverain06 cloud souverain
06 cloud souverainPatrick Bury
 
05 creation instance ovh
05 creation instance ovh05 creation instance ovh
05 creation instance ovhPatrick Bury
 
04 big data fournisseurs
04 big data fournisseurs04 big data fournisseurs
04 big data fournisseursPatrick Bury
 
03 big data stockage
03 big data stockage03 big data stockage
03 big data stockagePatrick Bury
 
03 big data échelle
03 big data échelle03 big data échelle
03 big data échellePatrick Bury
 
02 big data definition
02 big data definition02 big data definition
02 big data definitionPatrick Bury
 
01 big data introduction
01 big data introduction01 big data introduction
01 big data introductionPatrick Bury
 
16 graph databases
16 graph databases16 graph databases
16 graph databasesPatrick Bury
 
15 map reduce on azure
15 map reduce on azure15 map reduce on azure
15 map reduce on azurePatrick Bury
 

Plus de Patrick Bury (20)

100 évaluation
100 évaluation100 évaluation
100 évaluation
 
16 graph databases
16 graph databases16 graph databases
16 graph databases
 
15 map reduce on azure
15 map reduce on azure15 map reduce on azure
15 map reduce on azure
 
11 big data aws
11 big data aws11 big data aws
11 big data aws
 
14 big data gitlab
14 big data gitlab14 big data gitlab
14 big data gitlab
 
13 big data docker
13 big data docker13 big data docker
13 big data docker
 
10 big data hadoop
10 big data hadoop10 big data hadoop
10 big data hadoop
 
08 big data dataviz
08 big data dataviz08 big data dataviz
08 big data dataviz
 
12 big data azure
12 big data azure12 big data azure
12 big data azure
 
07 big data sgbd
07 big data sgbd07 big data sgbd
07 big data sgbd
 
06 cloud souverain
06 cloud souverain06 cloud souverain
06 cloud souverain
 
05 creation instance ovh
05 creation instance ovh05 creation instance ovh
05 creation instance ovh
 
04 big data fournisseurs
04 big data fournisseurs04 big data fournisseurs
04 big data fournisseurs
 
03 big data stockage
03 big data stockage03 big data stockage
03 big data stockage
 
03 big data échelle
03 big data échelle03 big data échelle
03 big data échelle
 
02 big data definition
02 big data definition02 big data definition
02 big data definition
 
01 open data
01 open data01 open data
01 open data
 
01 big data introduction
01 big data introduction01 big data introduction
01 big data introduction
 
16 graph databases
16 graph databases16 graph databases
16 graph databases
 
15 map reduce on azure
15 map reduce on azure15 map reduce on azure
15 map reduce on azure
 

09 big data mapreduce

  • 2. TP1 : Map reduce en python  Le Hello World du Map Reduce : compter les mots  Vous avez à coder plusieurs modules :  Mapper.py  Qui lit un texte sur l’entrée standard  Et qui renvoie un doublet : mot tabulation (t) 1 retour à la ligne (n) sur la sortie standard  Reducer.py  Qui lit les doublets depuis l’entrée standard  Qui calcule le nombre d’occurrence d’un mot 2
  • 3. Résultats attendus 3 echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py foo 1 foo 1 quux 1 labs 1 foo 1 bar 1 quux 1 echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py | sort -k1,1 | /home/hduser/reducer.py bar 1 foo 3 labs 1 quux 2
  • 4. Pure Python fonction map 4 map(function_to_apply, list_of_inputs) Cela permet de remplacer un code comme celui-ci item = [1,2,3,4,5] carres = [] for i in items: carres.append(i**2) item = [1,2,3,4,5] Carres = list(map(lambda x: x**2, items)) par
  • 5. Pure Python fonction filter 5 filter(function_to_apply, list_of_inputs) Agit comme une boucle mais c’est une fonction interne, plus rapide liste = range(-5, 5) negatifs = list(filter(lambda x: x<0, liste)) [-5, -4, -3, -2, -1] renvoie
  • 6. Pure Python fonction reduce 6 reduce(function_to_apply, list_of_inputs) Cela permet de remplacer un code comme celui-ci produit = 1 list = [1, 2, 3, 4] for num in list: produit = produit * num from functools import reduce list = [1, 2, 3, 4] produit = reduce((lambda x,y: x*y), list) par
  • 7. Map 7 #!/usr/bin/env python """mapper.py""" import sys # input comes from STDIN (standard input) for line in sys.stdin: # remove leading and trailing whitespace line = line.strip() # split the line into words words = line.split() # increase counters for word in words: # write the results to STDOUT (standard output); # what we output here will be the input for the # Reduce step, i.e. the input for reducer.py # # tab-delimited; the trivial word count is 1 print('%st%s' % (word, 1) )
  • 8. Reduce 8 #!/usr/bin/env python from operator import itemgetter import sys current_word = None current_count = 0 word = None for line in sys.stdin: line = line.strip() word, count = line.split('t', 1) try: count = int(count) except ValueError: # count was not a number, so silently # ignore/discard this line continue if current_word == word: current_count += count else: if current_word: print('%st%s' % (current_word, current_count)) current_count = count current_word = word if current_word == word: print('%st%s' % (current_word, current_count))
  • 9. Pipe (/Paipe/)  Toutes les commandes un*x ont trois moyens de communiquer avec l’utilisateur  Un moyen de lire ce qu’il demande (par défaut par le clavier) : stdin  Un moyen de transmettre des informations (par défaut à l’écran) : stdout  Un moyen de signaler les erreurs (par défaut à l’écran) : stderr  Un*x permet de chainer les commandes  On relie le stdout d’un programme au stdin du suivant  C’est la commande pipe, notée | 9