5 Spark Low Level-8
5 Spark Low Level-8
5 Spark Low Level-8
Programation en Spark
Algassimou diallo
Introduction
• Il existe deux options pour écrire une application Spark :
• Programmation de bas niveau, en utilisant des opérations sur une
structure de données de bas niveau appelée Resilient Distributed
Dataset (RDD).
• Programmation de haut niveau, en utilisant des bibliothèques de haut
niveau, telles que SparkSQL et Structured Streaming.
Algassimou Diallo 3
Big Data
Introduction
Algassimou Diallo 4
Big Data
Introduction
Algassimou Diallo 4
Big Data
Introduction
Algassimou Diallo 5
Big Data
Introduction
Algassimou Diallo 5
Big Data
Introduction
Algassimou Diallo 5
Big Data
Introduction
sc.parallelize([1, 5, 3, 2, 6, 7])
Algassimou Diallo 6
Big Data
Introduction
sc.textFile("hdfs://sar01:9000/data/sample_text.txt")
Algassimou Diallo 6
Big Data
Introduction
Nombre de partitions
• RDDs créés avec parallelize
• Mode local : nombre de cœurs sur les machines locales.
• Mode cluster : nombre total de cœurs sur tous les noeuds d’exécution,
ou 2, le plus grand des deux.
• RDD créés à partir de fichiers stockés dans HDFS
• Nombre de blocs HDFS dans le fichier d’entrée, ou 2, la valeur la plus
élevée étant retenue.
Algassimou Diallo 7
Big Data
Introduction
Transformation RDD
• Une transformation est une opération qui prend en charge un ou
plusieurs RDD et renvoie un nouveau RDD.
• Une transformation est appliquée en parallèle sur chaque partition.
Algassimou Diallo 8
Big Data
Introduction
Algassimou Diallo 8
Big Data
Introduction
Algassimou Diallo 9
Big Data
Introduction
Algassimou Diallo 9
Big Data
Introduction
Algassimou Diallo 10
Big Data
Introduction
Algassimou Diallo 11
Big Data
Introduction
Algassimou Diallo 12
Big Data
Introduction
Algassimou Diallo 12
Big Data
Introduction
Algassimou Diallo 13
Big Data
Introduction
Algassimou Diallo 13
Big Data
Introduction
Algassimou Diallo 14
Big Data
Introduction
Algassimou Diallo 14
Big Data
Introduction
Algassimou Diallo 14
Big Data
Introduction
𝑝 = hashCode(𝐾) mod 𝑛
Algassimou Diallo 14
Big Data
Introduction
Algassimou Diallo 15
Big Data
Introduction
Algassimou Diallo 15
Big Data
Introduction
Transformation Narrow
Une transformation “Narrow” est une transformation dans laquelle
chaque partition du RDD de sortie dépend d’au plus une partition du RDD
d’entrée
• Les transformations “Narrow” sont peu coûteuses.
• Pas besoin de communication entre les executors
Lesquelles des transformations ci-dessus sont “Narrow” ?
Algassimou Diallo 16
Big Data
Introduction
Transformation Narrow
Une transformation “Narrow” est une transformation dans laquelle
chaque partition du RDD de sortie dépend d’au plus une partition du RDD
d’entrée
• Les transformations “Narrow” sont peu coûteuses.
• Pas besoin de communication entre les executors
filter, map, flatMap and union sont des transformations “Narrow"
Algassimou Diallo 16
Big Data
Introduction
Transformation Wide
Une transformation “Wide” est une transformation dans laquelle chaque
partition du RDD de sortie de sortie peut dépendre de plusieurs partitions
du RDD d’entrée.
• Les transformations “Wide” sont plus coûteuses.
• Les executor doivent communiquer.
• Les données sont réparties sur le cluster.
Lesquelles des transformations ci-dessus sont “Wide” ?
Algassimou Diallo 17
Big Data
Introduction
Transformation Wide
Une transformation “Wide” est une transformation dans laquelle chaque
partition du RDD de sortie de sortie peut dépendre de plusieurs partitions
du RDD d’entrée.
• Les transformations “Wide” sont plus coûteuses.
• Les executor doivent communiquer.
• Les données sont réparties sur le cluster.
distinct and intersection sont des transformations “Wide"
Algassimou Diallo 17
Big Data
Introduction
RDD actions
Une action est une opération qui prend en charge un RDD et renvoie une
valeur au pilote après avoir effectué un calcul de l’ensemble de données.
Algassimou Diallo 18
Big Data
Introduction
RDD actions
Une action est une opération qui prend en charge un RDD et renvoie une
valeur au pilote après avoir effectué un calcul de l’ensemble de données.
• Le résultat d’une action est envoyé au programe pilote.
• Si le résultat est une liste de valeurs, toutes les valeurs sont envoyées au
programe pilote.
• Le résultat d’une action peut également être écrit sur le disque(le
système de fichiers local ou sur HDFS)
Algassimou Diallo 18
Big Data
Introduction
Algassimou Diallo 19
Big Data
Introduction
Algassimou Diallo 19
Big Data
Introduction
Algassimou Diallo 20
Big Data
Introduction
Algassimou Diallo 21
Big Data
Introduction
Algassimou Diallo 21
Big Data
Introduction
Algassimou Diallo 22
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 23
Big Data
Introduction
Algassimou Diallo 23
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 23
Big Data
Introduction
Algassimou Diallo 23
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 24
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 24
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 25
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 25
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 26
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Le code est incorrect, car le type de retour (liste) de la fonction reduce est
différent du type des éléments RDD en entrée (string).
Algassimou Diallo 26
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 27
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 27
Big Data
Introduction
Comprendre le code
Que fait le code suivant ?
Algassimou Diallo 27
Big Data
Introduction
Clé-Valeur RDD
RDD dont chaque élément est une paire (k, v ), k étant la clé et v la valeur.
• Les RDD clé-valeur sont des éléments de base importants dans de
nombreuses applications.
• Les RDD clé-valeur prennent en charge toutes les transformations et
actions qui peuvent être appliquées aux RDD ordinaires.
• Les RDD clé-valeur prennent en charge des transformations et des
actions spéciales.
Algassimou Diallo 28
Big Data
Introduction
Algassimou Diallo 29
Big Data
Introduction
Algassimou Diallo 29
Big Data
Introduction
Algassimou Diallo 29
Big Data
Introduction
Algassimou Diallo 30
Big Data
Introduction
Algassimou Diallo 31
Big Data
Introduction
Algassimou Diallo 32
Big Data
Introduction
Algassimou Diallo 32
Big Data
Introduction
Algassimou Diallo 32
Big Data
Introduction
Algassimou Diallo 32
Big Data
Introduction
RDD lineage
Spark dispose d’un planificateur DAG
qui divise le graphe en plusieurs
“Stage” (étapes).
Algassimou Diallo 33
Big Data
Introduction
Algassimou Diallo 34
Big Data
Introduction
Algassimou Diallo 34
Big Data
Introduction
Algassimou Diallo 35
Big Data
Introduction
Algassimou Diallo 36
Big Data
Introduction
Algassimou Diallo 36
Big Data
Introduction
Algassimou Diallo 36
Big Data
Introduction
Évaluation paresseuse
• Dans Spark, les transformations sont évaluées paresseusement.
• Lorsqu’une transformation est invoquée, Spark ne l’exécute pas
immédiatement. Les transformations ne sont exécutées que lorsque la
première action est appelée
• Un RDD peut être considéré comme un ensemble d’instructions sur la
manière de calculer les données que nous construisons au moyen de
transformations.
Algassimou Diallo 37
Big Data
Introduction
Algassimou Diallo 37
Big Data
Introduction
Algassimou Diallo 38
Big Data
Introduction
Algassimou Diallo 38
Big Data
Introduction
Algassimou Diallo 38
Big Data
Introduction
Algassimou Diallo 38
Big Data
Introduction
lines = sc.textFile("./data/logfile.txt")
exceptions = lines.filter(lambda line : "exception" in line)
nb_lines = exceptions.count()
exceptions.collect()
Algassimou Diallo 39
Big Data
Introduction
Algassimou Diallo 39
Big Data
Introduction
lines = sc.textFile("./data/logfile.txt")
exceptions = lines.filter(lambda line : "exception" in line)
nb_lines = exceptions.count()
exceptions.collect()
Algassimou Diallo 39
Big Data
Introduction
Algassimou Diallo 39
Big Data
Introduction
lines = sc.textFile("./data/logfile.txt")
exceptions = lines.filter(lambda line : "exception" in line)
nb_lines = exceptions.count()
exceptions.collect()
Algassimou Diallo 39
Big Data
Introduction
Algassimou Diallo 39
Big Data
Introduction
Algassimou Diallo 40
Big Data
Introduction
lines = sc.textFile("./data/logfile.txt")
exceptions = lines.filter(lambda line : "exception" in line)
exceptions.persist(StorageLevel.MEMORY_AND_DISK)
nb_lines = exceptions.count()
exceptions.collect()
Algassimou Diallo 40
Big Data
Introduction
Algassimou Diallo 40