Football Mejor y peor equipo
Se presentan los resultados de la liga premier 2001 y 20002 el programa obtiene el mejor y el peor equipo basado en los goles anotados y recibidos.
se utiliza la programacion MapReduce para resolverlo.
Este es el Mapper:
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ObtenerDiferenciaMapper extends Mapper<LongWritable, Text, LongWritable, Text>{
private Text diferenciaText = new Text();
protected void map(LongWritable key, Text value, Context context)
throws java.io.IOException, InterruptedException {
String[] dato = value.toString().split(",");
int temp = Integer.parseInt(dato[5]) - Integer.parseInt(dato[6]);
diferenciaText.set(dato[0] + "," + temp);
context.write(new LongWritable(1), diferenciaText);
}
}
este es el reducer:
package mx.com.sinapsis.ds.test.futbol;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class ObtenerMejorYPeorReducer extends
Reducer<LongWritable ,Text , Text, Text> {
SortedMap equiposPorcentaje = new TreeMap();
List estadisticaList = new ArrayList();
protected void reduce(LongWritable key, Iterable<Text> values, Context context)
throws java.io.IOException, InterruptedException {
for (Text value : values) {
String[] line = value.toString().split(",");
equiposPorcentaje.put(Integer.valueOf(line[1]), line[0]);
}
Iterator ents = equiposPorcentaje.entrySet().iterator();
while (ents.hasNext()) {
Entry thisEntry = (Entry) ents.next();
Object ky = thisEntry.getKey();
Object value = thisEntry.getValue();
}
Map map = new HashMap();
Integer llaveLast = Integer.valueOf(equiposPorcentaje.lastKey().toString());
Integer llaveFirst = Integer.valueOf(equiposPorcentaje.firstKey().toString());
map.put("Mejor ", llaveLast.toString() + " " + equiposPorcentaje.get(llaveLast));
map.put("Peor ", llaveFirst.toString() + " " + equiposPorcentaje.get(llaveFirst));
Text text = new Text();
Text equipo = new Text();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Entry thisEntry = (Entry) entries.next();
text.set(" " + thisEntry.getKey());
equipo.set(thisEntry.getValue().toString());
context.write(equipo , text);
}
}
}
La clase main:
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class ObtenerMejorYPeor {
public static void main(String[] args)
throws IOException, ClassNotFoundException, InterruptedException {
args = new String[]{"input","output"};
if (args.length != 2) {
System.err.println("Usage: MejoryPeor <input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(ObtenerMejorYPeor.class);
job.setJobName("Mejor y Peor");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(ObtenerDiferenciaMapper.class);
job.setReducerClass(ObtenerMejorYPeorReducer.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true);
}
}
Los resultados que arroja:
part-r-00000:
-34 Leicester Peor
43 Arsenal Mejor
este es el set de datos inicial:
Team,P,W,L,D,F,A,Pts
Arsenal,38,26,9,3,79,36,87
Liverpool,38,24,8,6,67,30,80
Manchester_U,38,24,5,9,87,45,77
Newcastle,38,21,8,9,74,52,71
Leeds,38,18,12,8,53,37,66
Chelsea,38,17,13,8,66,38,64
West_Ham,38,15,8,15,48,57,53
Aston_Villa,38,12,14,12,46,47,50
Tottenham,38,14,8,16,49,53,50
Blackburn,38,12,10,16,55,51,46
Southampton,38,12,9,17,46,54,45
Middlesbrough,38,12,9,17,35,47,45
Fulham,38,10,14,14,36,44,44
Charlton,38,10,14,14,38,49,44
Everton,38,11,10,17,45,57,43
Bolton,38,9,13,16,44,62,40
Sunderland,38,10,10,18,29,51,40
Ipswich,38,9,9,20,41,64,36
Derby,38,8,6,24,33,63,30
Leicester,38,5,13,20,30,64,28
Las columnas marcas como “F” y “A” contienen el número total de goles marcados a favor y en contra
- jgaribay's blog
- Inicie sesión o regístrese para enviar comentarios
Comentarios recientes
hace 5 días 11 horas
hace 5 días 11 horas
hace 5 días 12 horas
hace 25 semanas 1 día
hace 26 semanas 3 días
hace 33 semanas 1 día
hace 1 año 25 semanas
hace 2 años 37 semanas
hace 2 años 41 semanas
hace 2 años 49 semanas