Open MoustafaAMahmoud opened 3 years ago
Hi @moustafaalaa
This is my solution for the GPA
problem, please review if you have time 😃
Amgad 10 2020 First CS401 3 5 4
Ahmed 20 2020 First CS401 3 5 3
Ali 30 2020 First CS401 3 5 3.5
Mona 40 2020 First CS401 3 5 4.5
Amgad 10 2020 First CS402 3 5 4.5
Ahmed 20 2020 First CS402 3 5 2.5
Ali 30 2020 First CS402 3 5 3
Mona 40 2020 First CS402 3 5 4
Amgad 10 2020 First CS403 3 5 3.5
Ahmed 20 2020 First CS403 3 5 4.5
Ali 30 2020 First CS403 3 5 4
Mona 40 2020 First CS403 3 5 2
Ahmed 3.3333333
Ali 3.5
Amgad 4.0
Mona 3.5
--
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
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;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
if(args.length != 2){
System.err.println("GPA Calculator <Input-Path> <Output-Dir>");
System.exit(2);
}
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
boolean exists = fs.exists(new Path(args[1]));
if(exists) {
fs.delete(new Path(args[1]), true);
}
Job job = Job.getInstance(conf, "GPA Calculator");
job.setJarByClass(Main.class);
job.setMapperClass(MapClass.class);
job.setReducerClass(ReduceClass.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(ValuePair.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FloatWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class MapClass extends Mapper<Object, Text, Text, ValuePair>{
private final Text studentName = new Text();
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer record = new StringTokenizer(value.toString());
List<String> infoList = new ArrayList<>();
while (record.hasMoreTokens()){
infoList.add(record.nextToken());
}
studentName.set(infoList.get(0));
context.write(studentName, new ValuePair(Float.parseFloat(infoList.get(infoList.size()-3)), Float.parseFloat(infoList.get(infoList.size()-1))));
}
}
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReduceClass extends Reducer<Text, ValuePair, Text, FloatWritable> {
private ValuePair theValue = new ValuePair();
float first = 0;
float second = 0;
float sum = 0;
float sumCredits = 0;
float gpa = 0;
private List<Float> credits = new ArrayList<>();
private FloatWritable result = new FloatWritable();
@Override
protected void reduce(Text key, Iterable<ValuePair> values, Context context) throws IOException, InterruptedException {
while (values.iterator().hasNext()){
theValue = values.iterator().next();
first = theValue.getHours();
second = theValue.getScore();
credits.add(first);
sum += (first * second);
}
for(Float d : credits)
sumCredits += d;
gpa = sum / sumCredits;
result.set(gpa);
credits.clear();
first = 0;
second = 0;
sum = 0;
sumCredits = 0;
gpa = 0;
context.write(key, result);
}
}
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;
public class ValuePair implements WritableComparable<ValuePair> {
private FloatWritable hours;
private FloatWritable score;
public ValuePair(FloatWritable hours, FloatWritable score) {
set(hours, score);
}
public ValuePair() {
set(new FloatWritable(), new FloatWritable());
}
public ValuePair(float hours, float score) {
set(new FloatWritable(hours), new FloatWritable(score));
}
public void set(FloatWritable hours, FloatWritable score) {
this.hours = hours;
this.score = score;
}
@Override
public void readFields(DataInput in) throws IOException {
hours.readFields(in);
score.readFields(in);
}
@Override
public void write(DataOutput out) throws IOException {
hours.write(out);
score.write(out);
}
public float getHours() {
return hours.get();
}
public float getScore() {
return score.get();
}
@Override
public String toString() {
return hours + " " + score;
}
@Override
public int compareTo(ValuePair tp) {
int cmp = hours.compareTo(tp.hours);
if (cmp != 0) {
return cmp;
}
return score.compareTo(tp.score);
}
@Override
public int hashCode(){
return Objects.hash(hours.hashCode() + score.hashCode());
}
@Override
public boolean equals(Object o)
{
if(o instanceof ValuePair)
{
ValuePair tp = (ValuePair) o;
return hours.equals(tp.hours) && score.equals(tp.score);
}
return false;
}
}
Thanks.
This issue is an initial thread discussion about
GPA Calculator
. This thread continues the initial conversation on our Youtube account.The current information provided are:
Input columns
:Transformation Logic
: Not clear yet.Output
: GPA value