Slide 1

Slide 1 text

Jornadas Técnicas Uex-CIEMAT // 10-12 Febrero 2015 Procesando grandes volúmenes de datos con HADOOP César Suárez Ortega [email protected] Procesando datos con Hadoop: MapReduce y YARN

Slide 2

Slide 2 text

Software Developer / Researcher César Suárez Ortega tharandur csuarez

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Software Engineer / Researcher César Suárez Ortega

Slide 5

Slide 5 text

Software Engineer / Researcher César Suárez Ortega

Slide 6

Slide 6 text

Índice 1. 2. 3.   4. Exprimiendo al máximo MapReduce 5.

Slide 7

Slide 7 text

Introducción a MapReduce

Slide 8

Slide 8 text

Modelo de programación para el procesamiento de datos

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

¿Qué es MapReduce?   MUCHOS  

Slide 11

Slide 11 text

by MapReduce: Simplified Data Processing on Large Clusters. Dean J. and Ghemawat S. (2004)

Slide 12

Slide 12 text

Aprendiendo con ejemplos

Slide 13

Slide 13 text

National Climatic Data Center     

Slide 14

Slide 14 text

1958 -0021 1959 +0065 1960

Slide 15

Slide 15 text

Soluciones 1. Script  2. Paralelización por año  3. Paralelización por partes iguales  4. MapReduce 

Slide 16

Slide 16 text

MapReduce 101    Hay que definir una función para cada etapa. MAP REDUCE

Slide 17

Slide 17 text

Map MAP <0, 005733213…> //line1 <160, 006844324…> //line2

Slide 18

Slide 18 text

Map MAP

Slide 19

Slide 19 text

Map MAP list(K2, V2)

Slide 20

Slide 20 text

1958 -0021 1959 +0065 1960

Slide 21

Slide 21 text

Map MAP <0, 005733213…> //line1 <160, 006844324…> //line2 <1958, -21> <1959, +65>

Slide 22

Slide 22 text

Shuffle <1958, -21> <1959, +65> <1958, -34> <1959, +28> <1958, [-21, -34]> <1959, [+65, +28]>

Slide 23

Slide 23 text

Shuffle list(K2, V2) list(K2, list(V2))

Slide 24

Slide 24 text

Reduce REDUCE list(K2, list(V2)) list(K3, V3)

Slide 25

Slide 25 text

Reduce REDUCE <1958, [-21, -34]> <1959, [+65, +28]> <1958, -21> <1959, +65>

Slide 26

Slide 26 text

Map & Reduce REDUCE list(K2, list(V2)) list(K3, V3)) MAP list(K2, V2)

Slide 27

Slide 27 text

Map&Reduce      Job   

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Práctica 1 Mandando nuestro primer trabajo

Slide 31

Slide 31 text

MapReduce API   1. Blabla  2. BlablaMapper  3. BlablaReducer 

Slide 32

Slide 32 text

FlightsByCarrier   https://github.com/csuarez/seminario-mapreduce flights-by-carrier/

Slide 33

Slide 33 text

Year,Month,DayofMonth,DayOfWeek,DepTime,CRSDepTime,ArrTime,CRSArrTime,UniqueCarrier,FlightNum,TailNum,ActualElapsed Time,CRSElapsedTime,AirTime,ArrDelay,DepDelay,Origin,Dest,Distance,TaxiIn,TaxiOut,Cancelled,CancellationCode,Diverted,Carrie rDelay,WeatherDelay,NASDelay,SecurityDelay,LateAircraftDelay 1987,10,14,3,741,730,912,849,PS,1451,NA,91,79,NA,23,11,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,15,4,729,730,903,849,PS,1451,NA,94,79,NA,14,-1,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,17,6,741,730,918,849,PS,1451,NA,97,79,NA,29,11,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,18,7,729,730,847,849,PS,1451,NA,78,79,NA,-2,-1,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,19,1,749,730,922,849,PS,1451,NA,93,79,NA,33,19,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,21,3,728,730,848,849,PS,1451,NA,80,79,NA,-1,-2,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,22,4,728,730,852,849,PS,1451,NA,84,79,NA,3,-2,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,23,5,731,730,902,849,PS,1451,NA,91,79,NA,13,1,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,24,6,744,730,908,849,PS,1451,NA,84,79,NA,19,14,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,25,7,729,730,851,849,PS,1451,NA,82,79,NA,2,-1,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,26,1,735,730,904,849,PS,1451,NA,89,79,NA,15,5,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,28,3,741,725,919,855,PS,1451,NA,98,90,NA,24,16,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,29,4,742,725,906,855,PS,1451,NA,84,90,NA,11,17,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,31,6,726,725,848,855,PS,1451,NA,82,90,NA,-7,1,SAN,SFO,447,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,1,4,936,915,1035,1001,PS,1451,NA,59,46,NA,34,21,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,2,5,918,915,1017,1001,PS,1451,NA,59,46,NA,16,3,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,3,6,928,915,1037,1001,PS,1451,NA,69,46,NA,36,13,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,4,7,914,915,1003,1001,PS,1451,NA,49,46,NA,2,-1,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,5,1,1042,915,1129,1001,PS,1451,NA,47,46,NA,88,87,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,6,2,934,915,1024,1001,PS,1451,NA,50,46,NA,23,19,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,7,3,946,915,1037,1001,PS,1451,NA,51,46,NA,36,31,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,8,4,932,915,1033,1001,PS,1451,NA,61,46,NA,32,17,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,9,5,947,915,1036,1001,PS,1451,NA,49,46,NA,35,32,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,10,6,915,915,1022,1001,PS,1451,NA,67,46,NA,21,0,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,11,7,916,915,1006,1001,PS,1451,NA,50,46,NA,5,1,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,12,1,944,915,1027,1001,PS,1451,NA,43,46,NA,26,29,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,13,2,941,915,1036,1001,PS,1451,NA,55,46,NA,35,26,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,14,3,930,915,1029,1001,PS,1451,NA,59,46,NA,28,15,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,15,4,920,915,1023,1001,PS,1451,NA,63,46,NA,22,5,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,17,6,1009,915,1104,1001,PS,1451,NA,55,46,NA,63,54,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,18,7,915,915,1008,1001,PS,1451,NA,53,46,NA,7,0,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,19,1,940,915,1032,1001,PS,1451,NA,52,46,NA,31,25,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,21,3,913,915,1003,1001,PS,1451,NA,50,46,NA,2,-2,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA 1987,10,22,4,915,915,1017,1001,PS,1451,NA,62,46,NA,16,0,SFO,RNO,192,NA,NA,0,NA,0,NA,NA,NA,NA,NA

Slide 34

Slide 34 text

public class FlightsByCarrier { public static void main (String[] args) throws Exception { Job job = new Job(); job.setJarByClass(FlightsByCarrier.class); job.setJobName("FlightsByCarrier”); TextInputFormat.addInputPath(job, new Path(args[0])); TextOutputFormat.setOutputPath(job, new Path(args[1])); job.setInputFormatClass(TextInputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(FlightsByCarrierMapper.class); job.setReducerClass(FlightsByCarrierReducer.class); job.setOutputFormatClass(TextOutputFormat.class); job.addFileToClassPath(new Path("/user/root/opencsv-2.3.jar")); job.waitForCompletion(true); } }

Slide 35

Slide 35 text

//Mapper public class FlightsByCarrierMapper extends Mapper { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { if (key.get() > 0) { //Ignora la primera linea String[] lines = new CSVParser().parseLine(value.toString()); context.write(new Text(lines[8]), new IntWritable(1)); } } }

Slide 36

Slide 36 text

public class FlightsByCarrierReducer extends Reducer { @Override protected void reduce(Text token, Iterable counts, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable count : counts) { sum += count.get(); } context.write(token, new IntWritable(sum)); } }

Slide 37

Slide 37 text

$ git clone https://github.com/csuarez/seminario-mapreduce.git [...] $ tar xvzf 1987.tar.gz $ hdfs dfs –copyFromLocal lib/opencsv-2.3.jar /user/root $ hdfs dfs –copyFromLocal 1987.csv /user/root $ sh build.sh $ hadoop jar FlightsByCarrier.jar FlightsByCarrier /user/root/1987.csv /user/root/output/flightsCount $ hdfs dfs -cat /user/root/output/flightsCount/part-r-00000 Ejecución

Slide 38

Slide 38 text

FlightsByCarrier output INFO mapreduce.JobSubmitter: number of splits:2 ... INFO mapreduce.Job: map 0% reduce 0% INFO mapreduce.Job: map 22% reduce 0% INFO mapreduce.Job: map 41% reduce 0% INFO mapreduce.Job: map 83% reduce 0% INFO mapreduce.Job: map 100% reduce 0% INFO mapreduce.Job: map 100% reduce 100% ... Job Counters Launched map tasks=2 Launched reduce tasks=1 Rack-local map tasks=2 Total time spent by all maps in occupied slots (ms)=42442 Total time spent by all reduces in occupied slots (ms)=13465

Slide 39

Slide 39 text

MapReduce Cómo funciona

Slide 40

Slide 40 text

MapReduce v1

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Actores 1. 2. 3. 4.

Slide 43

Slide 43 text

Ejecución MapReduce       

Slide 44

Slide 44 text

Paso 1 Job Submission    

Slide 45

Slide 45 text

Paso 2 Job Initialization     

Slide 46

Slide 46 text

Paso 3 Task Assignment    

Slide 47

Slide 47 text

Paso 4 Task Execution   

Slide 48

Slide 48 text

Paso 5 Progress & Status    

Slide 49

Slide 49 text

Paso 6 Job Completion  

Slide 50

Slide 50 text

MapReduce v2 + YARN

Slide 51

Slide 51 text

¿Por qué YARN?     

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

YARN: Actores 1.  2.  3.  4.  5.

Slide 54

Slide 54 text

vs. MapReduce v1: Actores 1. 2. 3. 4. 1. YARN Resource Manager 2. YARN Node Manager 3. Application Master

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

MapReduce v1 API Processing FW Resource Manager Storage MR MapReduce v1 HDFS PIG HIVE HBASE

Slide 57

Slide 57 text

YARN API Processing FW Resource Manager Storage MR YARN HDFS PIG STORM MR v2 TEZ MPI

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Paso 1 Job Submission      

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Paso 2 Job Initialization       

Slide 62

Slide 62 text

Paso 2** Uber Mode     

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Paso 3 Task Assignment      

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Paso 4 Task Execution     

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Paso 5 Progress & Status     

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Exprimiendo MapReduce

Slide 71

Slide 71 text

Apache Pig

Slide 72

Slide 72 text

Apache Pig     

Slide 73

Slide 73 text

records = LOAD '1987.csv' USING PigStorage(',') AS (Year,Month,DayofMonth,DayOfWeek,DepTime,CRSDepTime,ArrTime,CRSArrTime,UniqueCarrier,FlightNum,Tail Num,ActualElapsedTime,CRSElapsedTime,AirTime,ArrDelay,DepDelay,Origin,Dest,Distance:int,TaxiIn,TaxiOut,Ca ncelled,CancellationCode,Diverted,CarrierDelay,WeatherDelay,NASDelay,DecurityDelay,LateAircraftDelay); milage_recs = GROUP records ALL; tot_miles = FOREACH milage_recs GENERATE SUM(records.Distance); STORE tot_miles INTO '/user/root/totalmiles'; https://github.com/csuarez/seminario-mapreduce $ cd pig-total-miles/ $ pig totalmiles.pig

Slide 74

Slide 74 text

Hadoop Streaming

Slide 75

Slide 75 text

$ hadoop jar $HADOOP_HOME/hadoop-streaming.jar \ -input myInputDirs \ -output myOutputDir \ -mapper myPythonScript.py \ -reducer /bin/wc \ -file myPythonScript.py

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Práctica 2 MapReduce Job

Slide 79

Slide 79 text

GreekGodCounter (Estándar)   https://github.com/csuarez/seminario-mapreduce greek-god-counter-standard/

Slide 80

Slide 80 text

private final static String[] gods = { "Zeus", "Hera", "Poseidón", "Dioniso", "Apolo", "Artemisa", "Hermes", "Atenea", "Ares", "Afrodita", "Hefesto", "Deméter” };

Slide 81

Slide 81 text

//Initializing the initial structure for (String god : gods) { godMap.put(god, 0); } try { //Reading input br = new BufferedReader(new FileReader(args[0])); String line = br.readLine(); while (line != null) { StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (godMap.containsKey(token)) { godMap.put(token, godMap.get(token) + 1); } } line = br.readLine(); } //Writing output Writer writer = new BufferedWriter(new FileWriter("gods.txt")); for (Entry entry : godMap.entrySet()) { writer.write(entry.getKey() + " = " + entry.getValue()); writer.write(System.lineSeparator()); } writer.close(); }

Slide 82

Slide 82 text

¡¡¡TAREA!!!

Slide 83

Slide 83 text

GreekGodCounter (MapReduce)   https://github.com/csuarez/seminario-mapreduce greek-god-counter-mapreduce/

Slide 84

Slide 84 text

import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.filecache.DistributedCache; public class GreekGodCounterMapReduce { public static void main (String[] args) throws Exception { Job job = new Job(); job.setJarByClass(GreekGodCounterMapReduce.class); job.setJobName("GreekGodCounterMapReduce"); TextInputFormat.addInputPath(job, new Path(args[0])); job.setInputFormatClass(TextInputFormat.class); job.setMapperClass(GreekGodCounterMapReduceMapper.class); job.setReducerClass(GreekGodCounterMapReduceReducer.class); TextOutputFormat.setOutputPath(job, new Path(args[1])); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.waitForCompletion(true); } }

Slide 85

Slide 85 text

import java.io.IOException; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.Mapper; import java.util.HashMap; import java.util.Map.Entry; import java.util.StringTokenizer; public class GreekGodCounterMapReduceMapper extends Mapper { private final static String[] gods = { "Zeus", "Hera", "Poseidón", "Dioniso", "Apolo", "Artemisa", "Hermes", "Atenea", "Ares", "Afrodita", "Hefesto", "Deméter" }; @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { } }

Slide 86

Slide 86 text

import java.io.IOException; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.Reducer; public class GreekGodCounterMapReduceReducer extends Reducer { @Override protected void reduce (Text token, Iterable counts, Context context) throws IOException, InterruptedException { } }

Slide 87

Slide 87 text

Recursos

Slide 88

Slide 88 text

Recursos

Slide 89

Slide 89 text

¡Gracias! ¿Alguna pregunta? [email protected]